summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/home/ham/automation/giesskanne.nix
blob: b11fd9d52e3744b2111444c5a7b89cc420333239 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# uses:
#  switch.crafting_giesskanne_relay
let
  cam = {
    name = "chilicam";
    camera = "camera.espcam_02";
    light = "light.espcam_02_light";
    seconds = 90; # default shutoff to protect the LED from burning out
  };
  seconds = 70; # time for giesskanne
  pump = "switch.arbeitszimmer_giesskanne_relay";
  # sensor = "sensor.statistics_for_sensor_crafting_brotbox_soil_moisture";
in
{
  services.home-assistant.config =
  {
    #sensor = map ( entity_id: {
    #    platform = "statistics";
    #    name = "Statistics for ${entity_id}";
    #    inherit entity_id;
    #    max_age.minutes = "60";
    #    sampling_size = 1000;
    #  }) [ "sensor.crafting_brotbox_soil_moisture" ];

    automation =
    [

      ##### brotbox
      { alias = "Water the plant for ${toString seconds} seconds";
        trigger = [
          { # trigger at 23:15 no matter what
            # TODO: retry or run only if switch.wasser is available
            platform = "time";
            at = "23:15:00";
          }
        ];
        action =
        [
          { # take a snapshot before watering
            service = "homeassistant.turn_on";
            entity_id =  [ cam.light ];
          }
          { # TODO: we could also create a recording with camera.record
            service = "camera.snapshot";
            data = {
              entity_id = cam.camera;
              # TODO: create /var/lib/hass/cam/ - now being done manually
              filename = "/var/lib/hass/cam/${cam.name}_{{ now().strftime('%Y%m%d-%H%M%S') }}.jpg";
            };
          }

          { # now turn on the pumping services
            # i do not start hte pump and light before the snapshot because i do
            # not know how long it takes (do not want to water the plants for too long)
            service = "homeassistant.turn_on";
            entity_id =  [ pump ];
          }
          { delay.seconds = seconds; }
          {
            service = "homeassistant.turn_off";
            entity_id =  [ pump cam.light ];
          }
        ];
      }
      { alias = "Always turn off the light after ${toString (cam.seconds)}s";
        trigger = [
          {
            platform = "state";
            entity_id = cam.light;
            to = "on";
            for.seconds = cam.seconds;
          }
        ];
        action =
        [
          {
            service = "homeassistant.turn_off";
            entity_id =  [ pump cam.light ];
          }
        ];
      }

      { alias = "Always turn off water after ${toString (seconds * 2)}s";
        trigger = [
          {
            platform = "state";
            entity_id = pump;
            to = "on";
            for.seconds = seconds*2;
          }
        ];
        action =
        [
          {
            service = "homeassistant.turn_off";
            entity_id =  [ pump cam.light ];
          }
        ];
      }
    ];
  };
}