-
Notifications
You must be signed in to change notification settings - Fork 10
/
watermeter.yaml
316 lines (281 loc) · 8.43 KB
/
watermeter.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
substitutions:
name: watermeter
friendly_name: "Water meter"
pulse_gpio: GPIO18
esphome:
name: $name
platform: ESP32
board: esp32dev
on_boot:
priority: -10
then:
- script.execute: publish_states
<<: !include network.yaml
captive_portal:
# Enable Home Assistant API
api:
password: !secret password
services:
- service: reset_main_counter
then:
- script.execute: reset_main_counter
- service: reset_secondary_counter
then:
- script.execute: reset_secondary_counter
ota:
password: !secret password
# Documentations:
# https://github.com/esphome/issues/issues/664
# https://esphome.io/components/sensor/integration.html
# https://community.home-assistant.io/t/using-esphome-to-build-a-water-flow-rate-meter/119380/64
globals:
- id: main_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: secondary_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: daily_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: weekly_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: monthly_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: yearly_counter_pulses
type: int
restore_value: yes
initial_value: '0'
- id: event_quantity
type: int
restore_value: no
initial_value: '0'
- id: last_event_quantity
type: int
restore_value: no
initial_value: '0'
- id: event_counter
type: int
restore_value: no
initial_value: '0'
- id: current_event_quantity
type: int
restore_value: no
initial_value: '0'
script:
- id: reset_main_counter
then:
- lambda: |-
id(main_counter_pulses) = 0;
id(water_main_consumption).publish_state(id(main_counter_pulses));
- id: reset_secondary_counter
then:
- lambda: |-
id(secondary_counter_pulses) = 0;
id(water_secondary_consumption).publish_state(id(secondary_counter_pulses));
- id: publish_states
then:
- lambda: |-
id(water_main_consumption).publish_state(id(main_counter_pulses));
id(water_secondary_consumption).publish_state(id(secondary_counter_pulses));
id(water_daily_consumption).publish_state(id(daily_counter_pulses));
id(water_weekly_consumption).publish_state(id(weekly_counter_pulses));
id(water_monthly_consumption).publish_state(id(monthly_counter_pulses));
id(water_yearly_consumption).publish_state(id(yearly_counter_pulses));
id(current_water_consumption).publish_state(id(event_quantity));
sensor:
- platform: uptime
name: "${friendly_name} uptime"
- platform: wifi_signal
name: "${friendly_name} WiFi signal"
- platform: pulse_meter
id: water_pulse_counter
name: "${friendly_name} water consumption"
pin:
number: $pulse_gpio
allow_other_uses: true
internal_filter: 100ms
unit_of_measurement: "L/min"
accuracy_decimals: 2
timeout: 30s
icon: "mdi:water"
# Value calculated are not consistent
#- platform: integration
# id: water_integration_sensor
# name: "${friendly_name} integrated water consumption"
# sensor: water_pulse_counter
# time_unit: min
# unit_of_measurement: "L"
# accuracy_decimals: 0
# icon: "mdi:water"
- platform: template
id: water_main_consumption
name: "${friendly_name} main water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: water_secondary_consumption
name: "${friendly_name} secondary water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: water_daily_consumption
name: "${friendly_name} daily water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
device_class: water
state_class: total_increasing
- platform: template
id: water_weekly_consumption
name: "${friendly_name} weekly water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: water_monthly_consumption
name: "${friendly_name} monthly water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: water_yearly_consumption
name: "${friendly_name} yearly water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: current_water_consumption
name: "${friendly_name} current water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
- platform: template
id: last_water_consumption
name: "${friendly_name} last water consumption"
unit_of_measurement: "L"
accuracy_decimals: 0
icon: "mdi:water"
binary_sensor:
# TCRT5000 pulse counter
- platform: gpio
id: water_pulse
pin:
number: $pulse_gpio
allow_other_uses: true
internal: true
filters:
- delayed_on_off: 50ms
- lambda: |-
id(main_counter_pulses) += x;
id(secondary_counter_pulses) += x;
id(daily_counter_pulses) += x;
id(weekly_counter_pulses) += x;
id(monthly_counter_pulses) += x;
id(yearly_counter_pulses) += x;
id(event_quantity) += x;
return x;
on_state:
- script.execute: publish_states
switch:
- platform: template
name: "${friendly_name} reset main counter"
icon: "mdi:restart"
turn_on_action:
- script.execute: reset_main_counter
- platform: template
name: "${friendly_name} reset secondary counter"
icon: "mdi:restart"
turn_on_action:
- script.execute: reset_secondary_counter
- platform: restart
name: "${friendly_name} restart"
time:
- platform: sntp
on_time:
- seconds: 0
minutes: 0
hours: 0
then:
- globals.set:
id: daily_counter_pulses
value: '0'
- lambda: id(water_daily_consumption).publish_state(id(daily_counter_pulses));
- seconds: 0
minutes: 0
hours: 0
days_of_week: MON
then:
- globals.set:
id: weekly_counter_pulses
value: '0'
- lambda: id(water_weekly_consumption).publish_state(id(weekly_counter_pulses));
- seconds: 0
minutes: 0
hours: 0
days_of_month: 1
then:
- globals.set:
id: monthly_counter_pulses
value: '0'
- lambda: id(water_monthly_consumption).publish_state(id(monthly_counter_pulses));
- seconds: 0
minutes: 0
hours: 0
days_of_month: 1
months: JAN
then:
- globals.set:
id: yearly_counter_pulses
value: '0'
- lambda: id(water_yearly_consumption).publish_state(id(yearly_counter_pulses));
interval:
# Save the last consumption
#
# An event is published when a water flow (>= 1L / 15 seconds) is
# detected and followed by a stop of consumption for a defined time.
- interval: 15sec
then:
- lambda: |-
if (id(event_quantity) != id(last_event_quantity)) {
// Water continues to flow
// Reset event counter
id(event_counter) = 0;
} else {
// Water no longer flows
if (id(event_quantity)) {
// 4 * 15 * 5 = 5min
if (id(event_counter) < 4 * 5) {
// Timeout is not reaches
id(event_counter)++;
} else {
// Timeout is reaches
id(last_water_consumption).publish_state(id(event_quantity));
// Send event to Home Assistant
api::HomeAssistantServiceCallAction<> *api;
api = new api::HomeAssistantServiceCallAction<>(api_apiserver_id, true);
// Event id length limit is 32 characters
api->set_service("esphome.last_consumption_changes");
api->play();
id(event_quantity) = 0;
}
}
}
id(last_event_quantity) = id(event_quantity);
# Track the current consumption
- interval: 2sec
then:
- lambda: |-
if (id(event_quantity) != id(current_event_quantity)) {
id(current_water_consumption).publish_state(id(event_quantity));
}
id(current_event_quantity) = id(event_quantity);