summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/home/ham/multi/fliegen-couter.nix
blob: 5b8abb2ff9f0a4cc04e2b73f6dffc85b15dc3e92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# uses:
#  sensor.btn1_click
#  sensor.btn2_click
let
  hlib = import ../lib;
  fly_swat = for: btn: method: incr: {
    alias = "Increment ${method} for ${for}";
    trigger = {
      platform = "state";
      entity_id = "sensor.${btn}_click";
      to = method;
    };
    action = builtins.genList (cnt: {
      service = "counter.increment";
      data.entity_id = "counter.${for}_fliegen";
    }) incr;
  };
in
{
  services.home-assistant.config =
  {
    counter = {
      felix_fliegen = {};
      misa_fliegen = {};
    };
    automation = [
      (fly_swat "misa"  "btn1" "single" 1)
      (fly_swat "misa"  "btn1" "double" 2)
      (fly_swat "misa"  "btn1" "triple" 3)
      (fly_swat "felix"  "btn2" "single" 1)
      (fly_swat "felix"  "btn2" "double" 2)
      (fly_swat "felix"  "btn2" "triple" 3)
      {
        alias = "Send Fly Counter Update";
        trigger = [
          {
            platform = "state";
            entity_id = "counter.felix_fliegen";
          }
          {
            platform = "state";
            entity_id = "counter.misa_fliegen";
            #above = -1;
          }
        ];
        action = {
            service = "mqtt.publish";
            data_template = { # gauge-style
              payload = "{{ trigger.to_state.state }}";
              topic = "${hlib.prefix}/flycounter/{{ trigger.to_state.object_id }}";
            };
        };
      }
      {
        alias = "Reset Fly counters on midnight";
        trigger = {
          platform = "time";
          at = "01:00:00";
        };
        action = [
          { service = "counter.reset";
            data.entity_id = "counter.misa_fliegen";
          }
          { service = "counter.reset";
            data.entity_id = "counter.felix_fliegen";
          }
        ];
      }
    ];
  };
}