-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline-shelly.yaml
114 lines (110 loc) · 4.23 KB
/
offline-shelly.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
# MARK: Last known state
switch:
- name: "${prefix} last known state"
restore_mode: RESTORE_DEFAULT_OFF
id: ${prefix}_last_known_state
internal: false
optimistic: true
platform: template
- id: !extend ${relay_id}
on_turn_off:
then:
switch.turn_off: ${prefix}_last_known_state
# MARK: HASS Entity
text_sensor:
- platform: homeassistant
id: ${prefix}_zigbee_device
entity_id: ${zigbee_entity}
name: ${prefix} zigbee device state
internal: false
on_value:
then:
- logger.log: "Update hass state for ${prefix}"
- if:
# Only update last known state if current state is not unavailable
condition:
lambda: 'return x != "unavailable";'
then:
# update last known state with current known state
if:
condition:
lambda: 'return x == "on";'
then:
switch.turn_on: ${prefix}_last_known_state
else:
switch.turn_off: ${prefix}_last_known_state
script:
# MARK: Offline toggle
- id: ${prefix}_offline_toggle
mode: queued
then:
- if:
condition:
lambda: 'return id(${prefix}_last_known_state).state == true;'
then:
# Last state == on
- logger.log: "Last state == 'on' for ${prefix}"
- switch.turn_off: ${relay_id}
- switch.turn_off: ${prefix}_last_known_state
else:
# Last state == off
- logger.log: "Last state == 'off' for ${prefix}"
- switch.turn_off: ${relay_id}
- delay: 1s
- switch.turn_on: ${relay_id}
- switch.turn_on: ${prefix}_last_known_state
# MARK: normal toggle
- id: ${prefix}_toggle
mode: queued
then:
- if:
condition:
api.connected:
else:
- logger.log: "api not connected. Go offline route for ${prefix}"
- script.execute: ${prefix}_offline_toggle
then:
if:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "unavailable";'
then:
- logger.log: "Zigbee state for ${prefix} is 'unavailable'. Go offline route."
- script.execute: ${prefix}_offline_toggle
else:
if:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "on";'
# is on
then:
- logger.log: "Go on route for ${prefix}. Try to turn off."
- homeassistant.action:
action: homeassistant.turn_off
data:
entity_id: ${zigbee_entity}
- wait_until:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "off";'
timeout: 2s
- if:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "on" || id(${prefix}_zigbee_device).state == "unavailable";'
then:
- logger.log: "State for ${prefix} didn't change. Go offline route"
- script.execute: ${prefix}_offline_toggle
# is off
else:
- logger.log: "Go off route for ${prefix}. Try to turn on."
- homeassistant.action:
action: homeassistant.turn_on
data:
entity_id: ${zigbee_entity}
- wait_until:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "on";'
timeout: 2s
- if:
condition:
lambda: 'return id(${prefix}_zigbee_device).state == "off" || id(${prefix}_zigbee_device).state == "unavailable";'
then:
- logger.log: "State for ${prefix} didn't change. Go offline route"
- script.execute: ${prefix}_offline_toggle