-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatermeter.yaml_old
416 lines (402 loc) · 14.2 KB
/
watermeter.yaml_old
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
substitutions:
device_name: watermeter
friendly_name: Water Meter
# Higher value gives lower watt readout
esphome:
name: '${device_name}'
esp8266:
board: nodemcuv2
restore_from_flash: true
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
use_address: 10.0.30.105
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: ${device_name} Hotspot
password: !secret esphome_fallback_pass
# Enable logging
logger:
# Enable Home Assistant API
api:
# Enable OTA updates
ota:
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));
time:
- platform: homeassistant
id: homeassistant_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));
sensor:
- platform: homeassistant
name: "HA Input Number Daily Water Consumption"
id: ha_input_number_daily_consumption
entity_id: input_number.water_meter_daily_consumption
- platform: homeassistant
name: "HA Input Number Weekly Water Consumption"
id: ha_input_number_weekly_consumption
entity_id: input_number.water_meter_weekly_consumption
- platform: homeassistant
name: "HA Input Number Monthly Water Consumption"
id: ha_input_number_monthly_consumption
entity_id: input_number.water_meter_monthly_consumption
- platform: homeassistant
name: "HA Input Number Yearly Water Consumption"
id: ha_input_number_yearly_consumption
entity_id: input_number.water_meter_yearly_consumption
- platform: homeassistant
name: "HA Input Number Total Water Consumption"
id: ha_input_number_total_consumption
entity_id: input_number.water_meter_total_consumption
# - platform: pulse_counter
# id: water_liter_counter
# pin: D1
# name: "${friendly_name} Liter Counter"
# device_class: water
# update_interval: 2sec
# internal_filter: 10us
# unit_of_measurement: "L"
# accuracy_decimals: 2
# icon: "mdi:water"
# force_update: True
# filters:
# - or:
# - throttle_average: 60s
# - delta: 1.0
- platform: pulse_counter
id: water_pulse_counter_raw
pin: D1
internal: true
update_interval: 1sec
internal_filter: 10us
unit_of_measurement: "L/min"
icon: "mdi:water"
- platform: copy
source_id: water_pulse_counter_raw
id: water_pulse_counter
name: "${friendly_name} water consumption"
# update_interval: 1sec
# internal_filter: 10us
unit_of_measurement: "L/min"
force_update: true
accuracy_decimals: 0
icon: "mdi:water"
filters:
# Divide by 60
- multiply: 0.0167
- lambda: return abs(x);
- or:
- throttle_average: 60s
- delta: 1
# - timeout:
# timeout: 60s
# value: 0
- 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"
- 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"
#195 754
binary_sensor:
# Nabijheidssensor
- platform: gpio
id: water_pulse
pin: D1
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
- platform: status
name: "Board connected"
id: connection
on_state:
then:
- wait_until:
condition:
binary_sensor.is_on: connection
- wait_until:
condition:
api.connected:
- wait_until:
condition:
time.has_time:
- wait_until:
condition:
sensor.in_range:
id: ha_input_number_total_consumption
above: 100000
below: 2000001
- lambda: |-
ESP_LOGD("INFO", "Daily pulse counter has value %d", id(daily_counter_pulses));
ESP_LOGD("INFO", "Weekly pulse counter has value %d", id(weekly_counter_pulses));
ESP_LOGD("INFO", "Monthly pulse counter has value %d", id(monthly_counter_pulses));
ESP_LOGD("INFO", "Yearly pulse counter has value %d", id(yearly_counter_pulses));
ESP_LOGD("INFO", "Total pulse counter has value %d", id(main_counter_pulses));
ESP_LOGD("INFO", "HA Daily pulse counter has value %d", id(ha_input_number_daily_consumption));
ESP_LOGD("INFO", "HA Weekly pulse counter has value %d", id(ha_input_number_weekly_consumption));
ESP_LOGD("INFO", "HA Monthly pulse counter has value %d", id(ha_input_number_monthly_consumption));
ESP_LOGD("INFO", "HA Yearly pulse counter has value %d", id(ha_input_number_yearly_consumption));
ESP_LOGD("INFO", "HA Total pulse counter has value %d", id(ha_input_number_total_consumption));
- delay: 5s
- lambda: |-
id(daily_counter_pulses) = id(ha_input_number_daily_consumption).state;
ESP_LOGD("INFO", "Set daily pulse counter to %d", id(daily_counter_pulses));
id(weekly_counter_pulses) = id(ha_input_number_weekly_consumption).state;
ESP_LOGD("INFO", "Set weekly pulse counter to %d", id(weekly_counter_pulses));
id(monthly_counter_pulses) = id(ha_input_number_monthly_consumption).state;
ESP_LOGD("INFO", "Set monthly pulse counter to %d", id(monthly_counter_pulses));
id(yearly_counter_pulses) = id(ha_input_number_yearly_consumption).state;
ESP_LOGD("INFO", "Set yearly pulse counter to %d", id(yearly_counter_pulses));
id(main_counter_pulses) = id(ha_input_number_total_consumption).state;
ESP_LOGD("INFO", "Set total pulse counter to %d", id(main_counter_pulses));
- delay: 5s
- script.execute: publish_states
# on_state:
# then:
# - if:
# condition:
# binary_sensor.is_on: connection
# then:
# - wait_until:
# condition:
# api.connected:
# - delay: 5s
# - lambda: |-
# id(daily_counter_pulses) = id(ha_input_number_daily_consumption).state;
# ESP_LOGD("INFO", "Set daily pulse counter to %d", id(daily_counter_pulses));
# id(weekly_counter_pulses) = id(ha_input_number_weekly_consumption).state;
# ESP_LOGD("INFO", "Set weekly pulse counter to %d", id(weekly_counter_pulses));
# id(monthly_counter_pulses) = id(ha_input_number_monthly_consumption).state;
# ESP_LOGD("INFO", "Set monthly pulse counter to %d", id(monthly_counter_pulses));
# id(yearly_counter_pulses) = id(ha_input_number_yearly_consumption).state;
# ESP_LOGD("INFO", "Set yearly pulse counter to %d", id(yearly_counter_pulses));
# id(main_counter_pulses) = id(ha_input_number_total_consumption).state;
# ESP_LOGD("INFO", "Set total pulse counter to %d", id(main_counter_pulses));
# - globals.set:
# id: daily_counter_pulses
# value: !lambda |-
# return id(ha_input_number_daily_consumption).state;
# - globals.set:
# id: weekly_counter_pulses
# value: !lambda |-
# return id(ha_input_number_weekly_consumption).state;
# - globals.set:
# id: monthly_counter_pulses
# value: !lambda |-
# return id(ha_input_number_monthly_consumption).state;
# - globals.set:
# id: yearly_counter_pulses
# value: !lambda |-
# return id(ha_input_number_yearly_consumption).state;
# - globals.set:
# id: main_counter_pulses
# value: !lambda |-
# return id(ha_input_number_total_consumption).state;
switch:
- platform: template
name: "${friendly_name} reset main counter button"
icon: "mdi:restart"
turn_on_action:
- script.execute: reset_main_counter
- platform: template
name: "${friendly_name} reset secondary counter button"
icon: "mdi:restart"
turn_on_action:
- script.execute: reset_secondary_counter
- platform: restart
name: "${friendly_name} restart"
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, 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);
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'
- id: higher_resistance
type: float
restore_value: yes
initial_value: '0'