-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgate.yaml
174 lines (156 loc) · 3.68 KB
/
gate.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
esphome:
name: gate
platform: ESP8266
board: d1_mini_pro
on_boot:
then:
# Set/get initial states
- binary_sensor.template.publish:
id: automation_safety
state: OFF
- binary_sensor.template.publish:
id: heading_comparison
state: OFF
- component.update: battery_level
web_server:
port: 80
logger:
level: NONE
wifi:
networks:
- ssid: SSID1
password: PSK
# Mobile Hotspot for troubleshooting
- ssid: SSID2
password: PSK
ota:
safe_mode: True
password: esphome_recovery
i2c:
sda: D2
scl: D1
scan: False
script:
- id: gate_toggle
then:
if:
condition:
and:
# Make sure we aren't triggering too rapidly
- binary_sensor.is_off: automation_safety
# Make sure gate is closed or else we won't trigger
- binary_sensor.is_off: gate_status
then:
- binary_sensor.template.publish:
id: automation_safety
state: ON
- switch.toggle: gate
- delay: 120s
- binary_sensor.template.publish:
id: automation_safety
state: OFF
mqtt:
broker: SERVER
username: gate
password: PASS
log_topic:
on_message:
- topic: cmd/gate
then:
- switch.toggle: gate
sensor:
- platform: qmc5883l
address: 0x0D
heading:
id: heading
data_rate: 10Hz
range: 200uT
oversampling: 512x
update_interval: 1s
# Template sensor to keep average of last 15 heading
# readings so we can detect significant change
- platform: template
id: heading_average
update_interval: 1s
unit_of_measurement: "°"
lambda: return id(heading).state;
filters:
- sliding_window_moving_average:
window_size: 15
send_every: 1
on_value:
then:
if:
condition:
and:
- for:
time: 3s
condition:
binary_sensor.is_on: heading_comparison
# This is intentionally duplicated
- binary_sensor.is_off: automation_safety
then:
- script.execute: gate_toggle
- platform: adc
pin: A0
id: battery_level
name: "Gate Battery Voltage"
update_interval: 600s
accuracy_decimals: 4
filters:
- multiply: 15.43
- platform: uptime
name: "Uptime"
binary_sensor:
# Sensor to keep script from triggering in rapid succession
- platform: template
name: "Automation Safety"
id: automation_safety
# This is what is used to trigger the automation
- platform: template
name: Heading Comparison Trigger
id: heading_comparison
# Set our trigger to true if heading varies from avg more than 2.5deg
lambda: |-
if (id(heading).state > id(heading_average).state + 2.5) {
return true;
}
if (id(heading).state < id(heading_average).state - 2.5) {
return true;
}
else {
return false;
}
# Magnet sensor to get real-world state
- platform: gpio
pin:
number: D7
mode: INPUT_PULLUP
name: Gate Status
id: gate_status
device_class: door
filters:
- delayed_on: 1s
- delayed_off: 1s
switch:
- platform: restart
name: "Reboot"
# Actual GPIO output
- platform: gpio
id: relay
pin:
number: D8
inverted: False
# Switch which toggles the relay on and off
- platform: template
id: gate
name: "Gate Remote"
icon: "mdi:gate"
turn_on_action:
- switch.turn_on: relay
- delay: 500ms
- switch.turn_off: relay
text_sensor:
- platform: wifi_info
ip_address:
name: "IP Address"