-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrabbit-hutch.yaml
264 lines (238 loc) · 5.76 KB
/
rabbit-hutch.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
esphome:
name: rabbit-hutch
esp32:
board: esp32doit-devkit-v1
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
password: "12345678"
# ota:
# password: "12345678"
wifi:
ssid: "12345678"
password: "12345678"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Rabbit-Hutch Fallback Hotspot"
password: "12345678"
captive_portal:
web_server:
port: 80
text_sensor:
- platform: wifi_info
ip_address:
name: "Sensor IP address"
ssid:
name: "Sensor SSID"
- platform: version
name: "Sensor ESPHome Version"
# state machine implementation
external_components:
- source:
type: git
url: https://github.com/muxa/esphome-state-machine
number:
- platform: template
id: nr_inhabitants
initial_value: !lambda |-
return id(nr_rabbits).state;
min_value: -1.0
max_value: !lambda |-
return id(nr_rabbits).state + 1.0;
step: 1
optimistic: true
on_value:
then:
- logger.log: value changed
on_value_range:
- above: !lambda |-
return id(nr_rabbit)state + 0.5;
then:
- state_machine.transition: ERROR
- logger.log: Overflow Error! Manual recount necessary.
- below: -0.5
then:
- state_machine.transition: ERROR
- logger.log: Underflow Error! Manual recount necessary.
state_machine:
- name: Rabit hutch as state machine
id: huch_state
states:
- "CLOSED"
- "OPEN"
- "WAITING"
- "ERROR"
inputs:
- name: MANUAL_OPEN
transitions:
- CLOSED -> OPEN
on_input:
- cover.open: hutch_door
- logger.log: Manual open
- name: MANUAL_CLOSE
transitions:
- ERROR -> CLOSED
- OPEN -> CLOSED
on_input:
- number.set:
id: nr_inhabitants
value: !lambda |-
return id(nr_rabits).state;
- cover.close: huch_door
- logger.log: Manual close
- name: TIME_CLOSE
transitions:
- OPEN -> WAITING
- name: TIME_OPEN
transitions:
- CLOSED -> OPEN
on_input:
- cover.open: hutch_door
- logger.log: Time open
- name: ENTER
transitions:
- OPEN -> OPEN
- on_transition:
- number.increment:
id: nr_inhabitants
cycle: false
- WAITING -> WAITING
- on_transition:
- number.increment:
id: nr_inhabitants
cycle: false
- then:
- if:
condition:
lambda: 'return id(nr_inhabitants).state = it(nr_rabits).state;'
then:
- state_machine.transition: CLOSE
- name: CLOSE
transitions:
- WAITING -> CLOSED
on_input:
- number.set:
id: nr_inhabitants
value: !lambda |-
return id(nr_rabits).state;
- cover.close: huch_door
- logger.log: All in! Closing
- name: EXIT
transitions:
- OPEN -> OPEN
- WAITING -> WAITING
on_imput:
- number.decrement:
id: inhabitants
cycle: false
switch:
- platform: gpio
name: "Hutch Door Open Switch"
id: door_open_switch
pin:
number: GPIO16
mode:
input: true
pullup: true
on_turn_on:
- state_machine.transition: MANUAL_OPEN
- platform: gpio
name: "Hutch Door Close Switch"
id: door_close_switch
pin:
number: GPIO17
mode:
input: true
pullup: true
on_turn_on:
- state_machine.transition: MANUAL_CLOSE
output:
- platform: gpio
name: "Hutch Door Motor Close"
id: motor_close
pin: GPIO33
- platform: gpio
name: "Hutch Door Motor Open"
id: motor_open
pin: GPIO32
cover:
- platform: time_based
name: "Rabbit Hutch Door"
id: hutch_door
open_action:
- output.turn_on: motor_open
open_duration: 10s
close_action:
- output.turn_on: motor_close
close_duration: 10s
stop_action:
- output.turn_off: motor_close
- output.turn_off: motor_open
- state_machine.transition: ERROR
i2c:
sda: 21
scl: 22
scan: false
id: bus_a
# frequency: 100kHz
apds9960:
address: 0x39
update_interval: 60s
gesture_led_drive: 50mA
sensor:
- platform: template
name: "Number of rabits"
id: nr_rabits
lambda: |-
return 4.0; # your constant value here
- platform: template
name: "Rabbits in the Hutch"
id: rabbit_sensor
lambda: |-
{return id(inhabitants).state;}
binary_sensor:
- platform: apds9960
# Rabbit enters the hutch
direction: UP
name: "Rabbit has entered the hutch"
on_press:
- number.increment:
id: inhabitants
cycle: false
- state_machine.transition: ENTER
- platform: apds9960
direction: DOWN
name: "Rabbit has left the hutch"
on_press:
- number.decrement:
id: inhabitants
cycle: false
- state_machine.transition: EXIT
time:
- platform: sntp
id: sntp_time
timezone: Europe/Sofia
servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
- 2.pool.ntp.org
sun:
latitude: 48.09009°
longitude: 11.96109°
on_sunrise:
- then:
- logger.log: Good morning! I'll open the huch door!
- state_machine.transition: TIME_OPEN
on_sunset:
then:
- logger.log: Good evening! I'll close the huch door!
- state_machine.transition: TIME_CLOSE
# display:
# - platform: lcd_pcf8574
# dimensions: 16x2
# address: 0x27
# lambda: |-
# it.print("Hello World!");