-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_ezo_ph.yaml
348 lines (307 loc) · 9.89 KB
/
as_ezo_ph.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
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
substitutions:
include_dependencies_as_ezo_ph: "defined"
include_dependency_1: ${include_dependencies_as_ezo_rtd}
############################################################
##
## INITIALIZE SENSOR STATES FROM LAST BOOT
##
############################################################
esphome:
on_boot:
priority: -200 # Highest priority, ensures enable pin to be turned off first
then:
- lambda: |-
id(${device_name}_ph).set_sensor_type(esphome::ezo::SENSOR_TYPE_PH);
/***************************************************************
* enable/disable sensors depending on last boot sensor states
***************************************************************/
//always on, since turning off leads to problems with i2c communication sometimes
//id(${device_name}_ph_enable_pin).turn_off();
id(${device_name}_ph_enable).turn_on();
//if(id(ph_enabled))
// id(${device_name}_ph_enable).turn_on();
//else
// id(${device_name}_ph_enable).turn_off();
/***************************************************************
* enable/disable temp compensation depending on last boot states
***************************************************************/
if(id(ph_tempcomp_enabled))
id(${device_name}_ph_tempcomp).turn_on();
else
id(${device_name}_ph_tempcomp).turn_off();
/***************************************************************
* enable/disable value sending
***************************************************************/
if(id(ph_reading_enabled))
id(${device_name}_ph_enable_reading_values).turn_on();
else
id(${device_name}_ph_enable_reading_values).turn_off();
sensor:
# SENSOR DEFINITION
- platform: custom_ezo
id: ${device_name}_ph
name: ${device_name_upper}.ph
address: 99
accuracy_decimals: 2
unit_of_measurement: "pH"
update_interval: 0.1s
internal: ${entities_internal}
filters:
- sliding_window_moving_average:
window_size: ${sliding_window_size_ph}
send_every: ${sliding_window_send_every_ph}
globals:
# PERMANENT GLOBALS FOR STATE STORAGE
- id: ph_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: ph_reading_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: ph_tempcomp_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: ph_tempcomp_timestamp_0
type: int
restore_value: no
initial_value: '0'
output:
# ENABLE PIN
- platform: gpio
pin: ${enable_ph_pin}
id: ${device_name}_ph_enable_pin
switch:
# TEMPERATURE COMPENSATION
- platform: template
name: ${device_name_upper}.ph.tempcomp
id: ${device_name}_ph_tempcomp
lambda: |-
return id(ph_tempcomp_enabled);
turn_on_action:
- lambda: |-
id(ph_tempcomp_enabled) = true;
id(ph_tempcomp_timestamp_0) = millis();
turn_off_action:
- lambda: |-
id(ph_tempcomp_enabled) = false;
# CALIBRATION MODE
- platform: template
name: ${device_name_upper}.ph.cal.activate
id: ${device_name}_ph_cal_activate
lambda: |-
return id(${device_name}_ph).calibration_mode_active();
turn_on_action:
- lambda: |-
id(${device_name}_ph).activate_calibration();
turn_off_action:
- lambda: |-
id(${device_name}_ph).deactivate_calibration();
internal: ${entities_internal}
# ENABLE/DISABLE SENSOR
- platform: template
id: ${device_name}_ph_enable
name: ${device_name_upper}.ph.enable
internal: "true"
lambda: |-
return id(ph_enabled);
turn_on_action:
- lambda: |-
id(${device_name}_ph).enable_sensor();
- globals.set:
id: ph_enabled
value: "true"
- output.turn_off: ${device_name}_ph_enable_pin
- switch.turn_on: ${device_name}_rtd_enable
turn_off_action:
- lambda: |-
id(${device_name}_ph).disable_sensor();
- globals.set:
id: ph_enabled
value: "false"
- output.turn_on: ${device_name}_ph_enable_pin
# ENABLE / DISABLE VALUE SENDING
- platform: template
id: ${device_name}_ph_enable_reading_values
name: ${device_name_upper}.ph.enable_reading_values
lambda: |-
return id(${device_name}_ph).sending_values();
turn_on_action:
- lambda: |-
id(${device_name}_ph).enable_sending_values();
id(ph_reading_enabled) = true;
turn_off_action:
- lambda: |-
id(${device_name}_ph).disable_sending_values();
id(ph_reading_enabled) = false;
button:
# CALIBRATION METHODS
- platform: template
name: ${device_name_upper}.ph.cal.check
id: ${device_name}_ph_cal_check
on_press:
lambda: |-
id(${device_name}_ph).start_calibration_check();
internal: ${entities_internal}
- platform: template
name: ${device_name_upper}.ph.cal.clear
id: ${device_name}_ph_cal_clear
on_press:
lambda: |-
id(${device_name}_ph).start_calibration_clear();
internal: ${entities_internal}
- platform: template
name: ${device_name_upper}.ph.cal.mid
id: ${device_name}_ph_cal_mid
on_press:
lambda: |-
id(${device_name}_ph).start_calibration_ph_mid();
internal: ${entities_internal}
- platform: template
name: ${device_name_upper}.ph.cal.high
id: ${device_name}_ph_cal_high
on_press:
lambda: |-
id(${device_name}_ph).start_calibration_ph_high();
internal: ${entities_internal}
- platform: template
name: ${device_name_upper}.ph.cal.low
id: ${device_name}_ph_cal_low
on_press:
lambda: |-
id(${device_name}_ph).start_calibration_ph_low();
internal: ${entities_internal}
# FACTORY RESET
- platform: template
name: ${device_name_upper}.ph.factory_reset
id: ${device_name}_ph_factory_reset
on_press:
lambda: |-
id(${device_name}_ph).start_factory_reset();
internal: ${entities_internal}
number:
# SAMPLE INTERVAL TIME IN [MS]
- platform: template
min_value: ${min_sample_interval_ph}
max_value: ${max_sample_interval_ph}
step: "1"
id: ${device_name}_ph_sample_interval
name: ${device_name}.ph.sample_interval
optimistic: true
mode: box
restore_value : true
initial_value: ${min_sample_interval_ph}
update_interval: "10s"
unit_of_measurement: "ms"
on_value:
- lambda: |-
id(${device_name}_ph).set_sample_interval(x);
# TEMPCOMP INTERVAL TIME IN [MS]
- platform: template
min_value: ${min_sample_interval_ph_tempcomp}
max_value: ${max_sample_interval_ph_tempcomp}
step: "1"
id: ${device_name}_ph_tempcomp_sample_interval
name: ${device_name}.ph.tempcomp.sample_interval
optimistic: true
mode: box
restore_value : true
initial_value: ${min_sample_interval_ph_tempcomp}
update_interval: "10s"
unit_of_measurement: "ms"
# CALIBRATION POINTS
- platform: template
min_value: "0"
max_value: "14"
step: "0.01"
id: ${device_name}_ph_cal_mid_value
name: ${device_name}.ph.cal.mid_value
optimistic: true
mode: box
restore_value : true
initial_value: "7.0"
update_interval: "10s"
unit_of_measurement: "mol/l"
on_value:
- lambda: |-
id(${device_name}_ph).set_ph_cal_mid_value(x);
internal: ${entities_internal}
- platform: template
min_value: "0"
max_value: "14"
step: "0.01"
id: ${device_name}_ph_cal_high_value
name: ${device_name}.ph.cal.high_value
optimistic: true
mode: box
restore_value : true
initial_value: "10.0"
update_interval: "10s"
unit_of_measurement: "mol/l"
on_value:
- lambda: |-
id(${device_name}_ph).set_ph_cal_high_value(x);
internal: ${entities_internal}
- platform: template
min_value: "0"
max_value: "14"
step: "0.01"
id: ${device_name}_ph_cal_low_value
name: ${device_name}.ph.cal.low_value
optimistic: true
mode: box
restore_value : true
initial_value: "4.0"
update_interval: "10s"
unit_of_measurement: "mol/l"
on_value:
- lambda: |-
id(${device_name}_ph).set_ph_cal_low_value(x);
internal: ${entities_internal}
binary_sensor:
# ERROR CHECKING SENSOR
#- platform: template
# name: "${device_name_upper}.ph.error"
# id: ${device_name}_ph_error
# lambda: |-
# if (id(${device_name}_ph).retry_counter() >= ${max_retry_attempts})
# {
# return true;
# }else
# {
# return false;
# }
# on_press:
# then:
# if:
# condition:
# lambda: |-
# return id(rtd_enabled);
# then:
# - switch.turn_off: ${device_name}_ph_enable
# - delay: 2s
# - switch.turn_on: ${device_name}_ph_enable
interval:
# TEMPCOMP SET INTERVAL
- interval: 1s
then:
lambda: |-
if(id(ph_tempcomp_enabled))
{
int time_now = millis();
// integer overflow protection -> restart waiting timer -> (TODO: maybe implement exact value reset via max integer)
if (time_now < id(ph_tempcomp_timestamp_0))
id(ph_tempcomp_timestamp_0) = time_now;
// check if we should send a sample
if (time_now - id(ph_tempcomp_timestamp_0) >= id(${device_name}_ph_tempcomp_sample_interval).state)
{
id(${device_name}_ph).set_tempcomp_value(id(${device_name}_rtd).state);
id(ph_tempcomp_timestamp_0) = time_now;
}
}
else
{
id(${device_name}_ph).set_tempcomp_value(${default_temp_comp_value});
}