-
Notifications
You must be signed in to change notification settings - Fork 0
/
catfeeder-node.yaml
483 lines (464 loc) · 13.2 KB
/
catfeeder-node.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
esphome:
name: catfeeder-node
friendly_name: catfeeder-node
esp32:
board: nodemcu-32s
framework:
type: arduino
# Enable logging
logger:
level: INFO
# Enable Home Assistant API
api:
services:
- service: feed_cat
then:
- script.execute:
id: feed_script
force: true
servings_current: 1
web_server:
port: 80
ota:
password: "c77a50f9d929caf56146ed0e31b0d93b"
platform: esphome
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Catfeeder-Node Fallback Hotspot"
password: !secret wifi_fallback_password
captive_portal:
output:
- platform: ledc
pin: GPIO23
# frequency: 1000 Hz
id: pwm_output
- platform: ledc
pin: GPIO22
# frequency: 1000 Hz
id: pwm_output2
# - platform: ledc
# pin: GPIO4
# frequency: 1000 Hz
# id: motor_enable
fan:
- platform: hbridge
id: serving_motor
name: "Cat Feeder"
pin_a: pwm_output
pin_b: pwm_output2
# enable_pin: motor_enable
# decay_mode: slow
# internal: True
restore_mode: ALWAYS_OFF
on_turn_on:
- fan.turn_on:
id: serving_motor
speed: 100
binary_sensor:
- platform: gpio
pin:
number: GPIO15
mode:
input: true
pullup: true
inverted: true
id: feed_clicker
name: "Feed Clicker"
internal: True
switch:
- platform: restart
name: "Cat Feeder Restart"
- platform: template
name: "Skip Next Meal"
id: skip_next_meal
optimistic: True
restore_mode: RESTORE_DEFAULT_OFF
- platform: template
name: "Calculate servings by refill"
id: calculate_by_refill
optimistic: True
number:
- platform: template
id: max_servings_per_day
name: "Max Servings per day"
optimistic: true
min_value: 1
max_value: 20
restore_value: True
initial_value: 12
step: 1
- platform: template
name: "Grams per day"
id: grams_per_day
optimistic: true
min_value: 60
max_value: 130
restore_value: True
initial_value: 115
step: 5
device_class: "weight"
- platform: template
name: "Grams per serving"
id: grams_per_serving
optimistic: true
min_value: 5
max_value: 20
restore_value: True
initial_value: 7
step: 0.1
device_class: "weight"
- platform: template
name: "Refill size"
id: refill_size
optimistic: true
min_value: 0
max_value: 4000
restore_value: False
initial_value: 0
step: 10
device_class: "weight"
- platform: template
name: "Grams per serving (Calculated)"
id: grams_per_serving_calculated
optimistic: true
internal: True
min_value: 0
max_value: 100
restore_value: True
initial_value: 0
step: 0.1
device_class: "weight"
- platform: template
name: "Last Refill size"
id: last_refill_size
optimistic: true
internal: True
min_value: 0
max_value: 4000
restore_value: True
step: 10
device_class: "weight"
- platform: template
name: "Servings today"
id: servings_today
optimistic: true
min_value: 0
max_value: 100
restore_value: True
initial_value: 0
step: 1
internal: true
on_value:
then:
- sensor.template.publish:
id: servings_today_sensor
state: !lambda 'return x;'
- platform: template
name: "Servings since refill"
id: servings_since_refill
optimistic: true
min_value: 0
max_value: 1000
restore_value: True
initial_value: 0
step: 1
internal: True
on_value:
then:
- sensor.template.publish:
id: servings_since_refill_sensor
state: !lambda 'return x;'
- platform: template
name: "Servings total"
id: servings_total
optimistic: true
min_value: 0
max_value: 1000000
restore_value: True
initial_value: 0
step: 1
internal: true
on_value:
then:
- sensor.template.publish:
id: servings_total_sensor
state: !lambda 'return x;'
- platform: template
name: "Active servings"
id: active_servings
optimistic: true
min_value: 0
max_value: 10
step: 1
internal: true
on_value:
then:
- sensor.template.publish:
id: active_servings_sensor
state: !lambda return x;
- platform: template
name: "Bonus treats internal"
id: bonus_treats_internal
optimistic: true
min_value: 0
max_value: 10
restore_value: True
step: 1
internal: true
script:
- id: feed_script
mode: restart
parameters:
servings_current: int
force: bool
then:
- if:
condition:
- or:
- lambda: !lambda return force;
- and:
- lambda: !lambda return int(id(servings_today).state) < int(id(max_servings_per_day).state);
- switch.is_off: skip_next_meal
then:
- number.set:
id: active_servings
value: !lambda 'return servings_current;'
- while:
condition:
lambda: |-
return id(active_servings).state > 0;
then:
- if:
condition:
- lambda: !lambda return int(id(servings_today).state) > int(id(max_servings_per_day).state);
then:
- logger.log:
format: "Max servigns reached= %f"
args: [ 'id(servings_today).state' ]
level: INFO
- number.decrement: active_servings
else:
- logger.log:
format: "Feeding meal active= %f, total= %f, skip=%d, max=%f, today=%f"
args: [ 'id(active_servings).state', 'id(servings).state', 'id(skip_next_meal).state', 'id(max_servings_per_day).state', 'id(servings_today).state']
level: INFO
- sensor.template.publish:
id: active_servings_sensor
state: !lambda 'return id(active_servings).state;'
- fan.turn_on:
id: serving_motor
- if:
condition:
binary_sensor.is_on: feed_clicker
then:
- wait_until:
binary_sensor.is_off: feed_clicker
- wait_until:
binary_sensor.is_on: feed_clicker
- wait_until:
binary_sensor.is_off: feed_clicker
- fan.turn_off: serving_motor
else:
- wait_until:
binary_sensor.is_on: feed_clicker
- wait_until:
binary_sensor.is_off: feed_clicker
- fan.turn_off: serving_motor
- number.increment: servings_today
- number.increment: servings_since_refill
- number.increment: servings_total
- number.decrement: active_servings
- text_sensor.template.publish:
id: last_feed
state: !lambda |-
auto timeSting = id(sntp_time).now().strftime("%c");
return timeSting;
- delay: 300s
- number.to_min: active_servings
- if:
condition:
lambda: !lambda return !force;
then:
- logger.log: "Resetting skip next meal"
- switch.template.publish:
id: skip_next_meal
state: OFF
button:
- platform: template
name: Reset Refill
on_press:
then:
- number.set:
id: last_refill_size
value: 0
- number.set:
id: refill_size
value: 0
- number.set:
id: grams_per_serving_calculated
value: 0
- platform: template
name: Save Refill
on_press:
- if:
condition:
lambda: 'return id(servings_since_refill).state > 0;'
then:
- number.set:
id: last_refill_size
value: !lambda 'return id(refill_size).state;'
- number.set:
id: grams_per_serving_calculated
value: !lambda 'return id(last_refill_size).state / id(servings_since_refill).state;'
- number.set:
id: servings_since_refill
value: !lambda 'return 0;'
- platform: template
name: Feed Single
id: feed_single
on_press:
then:
- script.execute:
id: feed_script
force: true
servings_current: 1
- number.increment: bonus_treats_internal
- platform: template
name: Feed Meal
id: feed_meal
on_press:
then:
- switch.template.publish:
id: skip_next_meal
state: Off
- script.execute:
id: feed_script
force: true
servings_current: !lambda 'return id(servings).state;'
- switch.template.publish:
id: skip_next_meal
state: On
time:
- platform: sntp
id: sntp_time
on_time:
# Every morning on weekdays
- seconds: 0
minutes: 0
hours: 0
then:
- number.to_min: servings_today
- number.to_min: bonus_treats_internal
- platform: sntp
on_time:
- hours: 7,16,21
seconds: 0
minutes: 0
then:
- script.execute:
id: feed_script
force: false
servings_current: !lambda 'return id(servings).state;'
text_sensor:
- platform: template
name: "Last feed"
id: last_feed
- platform: template
name: "Feed schedule"
lambda: !lambda |-
return {"07:00, 16:00, 21:00"};
- platform: template
name: "Next feed"
update_interval: 1s
lambda: !lambda |-
int current_hour = id(sntp_time).now().hour;
std::vector<int> ordered_list = {7, 16, 21};
bool skip_next_higher = id(skip_next_meal).state;
int next_hour = 0;
bool found = false;
for (size_t i = 0; i < ordered_list.size(); i++) {
if (ordered_list[i] > current_hour) {
if (skip_next_higher) {
if (i + 1 < ordered_list.size()) {
next_hour = ordered_list[i + 1];
} else {
next_hour = ordered_list[0]; // Wrap around to 0
}
} else {
next_hour = ordered_list[i];
}
found = true;
break;
}
}
if (!found) {
// Set next_hour based on some default logic since no higher hour was found
// For example, setting it to the first hour in the ordered_list
next_hour = ordered_list[0];
}
std::string period = (next_hour >= 12) ? "PM" : "AM";
int hour12 = (next_hour > 12) ? next_hour - 12 : next_hour;
if (hour12 == 0) {
hour12 = 12;
}
return std::to_string(hour12) + ":00 " + period;
sensor:
- platform: template
name: "Active Servings"
id: active_servings_sensor
state_class: "measurement"
- platform: template
name: "Servings per day"
id: servings_per_day
state_class: "measurement"
update_interval: 1s
lambda: !lambda |-
return id(grams_per_day).state / id(grams_per_serving).state;
- platform: template
name: "Servings per day (calculated)"
id: servings_per_day_calculated
state_class: "measurement"
lambda: !lambda |-
return id(grams_per_day).state / id(grams_per_serving_calculated).state;
- platform: template
name: "Servings per meal"
id: servings
state_class: "measurement"
update_interval: 1s
lambda: !lambda |-
float feed_count = 3;
if(id(grams_per_serving_calculated).has_state() && id(grams_per_serving_calculated).state > 0 && id(calculate_by_refill).state) {
return floor(id(servings_per_day_calculated).state / feed_count);
}
return floor(id(servings_per_day).state / feed_count);
- platform: template
name: "Servings today"
id: servings_today_sensor
state_class: "total_increasing"
- platform: template
id: servings_total_sensor
name: "Servings total"
state_class: "total"
- platform: template
id: servings_since_refill_sensor
name: "Servings Since Refill"
state_class: "total_increasing"
- platform: template
id: last_refill_size_sensor
name: "Last refill size"
lambda: !lambda |-
return id(last_refill_size).state;
- platform: template
id: grams_per_serving_calculated_sensor
name: "Grams per Serving (Calculated)"
lambda: !lambda |-
return id(grams_per_serving_calculated).state;
- platform: template
name: "Bonus treats today"
id: bonus_treats
state_class: "measurement"
lambda: !lambda |-
return id(bonus_treats_internal).state;