-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript_rain_water_tank_sensors_action.yaml
103 lines (91 loc) · 3.13 KB
/
script_rain_water_tank_sensors_action.yaml
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
103
# SPDX-License-Identifier: Apache-1.0
# Copyright (c) 2023 Ilia Sotnikov
---
!lambda |-
// Define preprocessor macro after entity triggered the script for
// conditionals
#include "esphome/core/log.h"
#define TRIGGERED_BY_${triggered_by}
// No further manipulations if winter mode is enabled
if (id(winter_mode).state) {
ESP_LOGI(
"${triggered_by}", "Winter mode active, ignoring further actions"
);
return;
}
// Handle rain detected or water tank is empty
if (id(${rain_sensor_id}).state || id(${water_tank_empty_id}).state) {
ESP_LOGI(
"${triggered_by}",
"%s sprinklers and putting them in standby"
" (rain sensor state: %s, water tank empty state: %s)",
#ifdef TRIGGERED_BY_${rain_sensor_id}
"Stopping"
#else
"Pausing"
#endif
, ONOFF(id(${rain_sensor_id}).state)
, ONOFF(id(${water_tank_empty_id}).state)
);
// Explicitly shutdown any running operation in case of rain, also turn on
// LED indicating the condition
#ifdef TRIGGERED_BY_${rain_sensor_id}
#ifdef HAS_RGB_LED
auto call = ${led_id}->turn_on();
call.perform();
#endif
id(flowerbed_sprinklers).shutdown();
id(lawn_sprinklers).shutdown();
#endif
// Pause any active operation if water tank is empty, so it could resume
// once it becomes full
// NOTE: `pause()` should be called before activating controller's standby
// switch, since it internally calls `shutdown` thus active valve will be
// lost
#ifdef TRIGGERED_BY_${water_tank_empty_id}
id(flowerbed_sprinklers).pause();
id(lawn_sprinklers).pause();
#endif
// Put sprinklers into standby mode
id(flowerbed_sprinklers_standby_switch).turn_on();
id(lawn_sprinklers_standby_switch).turn_on();
#ifdef HAS_DISPLAY
if (${display_id}->is_ready()) {
${display_id}->update();
}
#endif
// Any active condition above is terminating, i.e. if one sensor is active
// stopping/pausing won't be reverted by processing another sensor being
// inactive
return;
}
ESP_LOGI(
"${triggered_by}", "Putting sprinklers out of standby"
);
// Move sprinklers out of standby mode
id(flowerbed_sprinklers_standby_switch).turn_off();
id(lawn_sprinklers_standby_switch).turn_off();
// Turn off LED indicating that rain is detected
#ifdef TRIGGERED_BY_${rain_sensor_id}
#ifdef HAS_RGB_LED
auto call = ${led_id}->turn_off();
call.perform();
#endif
#endif
// Resume any operation if any once water tank is full
// NOTE: should be called after moving controller out of standby otherwise
// `resume()` will do nothing
#ifdef TRIGGERED_BY_${water_tank_empty_id}
ESP_LOGI("${triggered_by}", "Resuming sprinklers");
id(flowerbed_sprinklers).resume();
id(lawn_sprinklers).resume();
#endif
#ifdef HAS_DISPLAY
if (${display_id}->is_ready()) {
${display_id}->update();
}
#endif
// Preprocessor macros lack scope so undefine the macro for another script
// instance to have proper conditionals (i.e. prevent wrongly evaluating
// macro for another triggering entity has own macro defined)
#undef TRIGGERED_BY_${triggered_by}