-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto-light.yaml
343 lines (330 loc) · 11 KB
/
auto-light.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
blueprint:
name: Basic Light Control
description: Control an area's lights based on an occupancy sensor and the sun's position.
domain: automation
input:
target_lights:
name: Target Lights
description: |
The target lights that should be controlled by the automation.
selector:
target:
entity:
- domain: light
occupancy_sensor:
name: Occupancy Sensor
description: |
If occupancy is detected by the given sensor, the lights will be set brighter.
selector:
entity:
filter:
- domain: binary_sensor
toggle_switch:
name: Toggle Switch
description: |
The loop created by this automation will only impact the lights if the switch is on.
selector:
entity:
filter:
- domain: input_boolean
max_brightness:
name: Maximum Brightness
description: |
The peak brightness of the lights when sun is fully down and the room is
occupied.
default: 80
selector:
number:
min: 2
max: 100
unit_of_measurement: "%"
mode: slider
step: 1
nightmode_switch:
name: Night Mode Switch
description: |
If the given switch is on, all brightness calculations will use the nightmode max brightness in place of the standard maximum brightness.
selector:
entity:
filter:
- domain: input_boolean
nightmode_max_brightness:
name: Maximum Brightness (Night Mode)
description: |
When the nightmode switch is on, the peak brightness of the lights will be this value.
default: 10
selector:
number:
min: 1
max: 100
unit_of_measurement: "%"
mode: slider
step: 1
main_light_scale:
name: Main Light Scale
description: |
The peak brightness of the lights with Main in their friendly name will
be scaled by this value.
default: 50
selector:
number:
min: 0
max: 100
unit_of_measurement: "%"
mode: slider
step: 1
vacancy_scale:
name: Vacancy Scale
description: |
The peak brightness of the lights will be scaled by this value when the
motion sensor determines the room to be unoccupied.
default: 20
selector:
number:
min: 0
max: 100
unit_of_measurement: "%"
mode: slider
step: 1
elevation_on:
name: Elevation On
description: |
The elevation of the sun at which the lights will be at their maximum brightness.
default: -1
selector:
number:
min: -90
max: 0
unit_of_measurement: "°"
mode: box
step: 1
elevation_off:
name: Elevation Off
description: |
The elevation of the sun at which the lights will be at their minimum brightness (off).
default: 4
selector:
number:
min: 1
max: 90
unit_of_measurement: "°"
mode: box
step: 1
transition_time:
name: Transition Time
description: |
How many seconds should the light change brightness/temp over.
default: 2
selector:
number:
min: 0
max: 10
mode: slider
step: 1
min_color_temp:
name: Minimum Color Temperature
description: The coolest color your bulbs will be set to.
default: 250
selector:
color_temp:
min_mireds: 153
max_mireds: 500
max_color_temp:
name: Maximum Color Temperature
description: The warmest color your bulbs will be set to.
default: 454
selector:
color_temp:
min_mireds: 153
max_mireds: 500
variables:
target_lights_input: !input target_lights
target_lights: >-
{%- set ns = namespace( lights = [] ) -%}
{%- if 'entity_id' in target_lights_input -%}
{%- set entity_ids = target_lights_input.entity_id -%}
{%- if entity_ids is string -%}
{%- set entity_ids = [entity_ids] -%}
{%- endif -%}
{%- for light_entity in expand(entity_ids | select('match', 'light')) -%}
{%- set ns.lights = ns.lights + [light_entity.entity_id] -%}
{%- endfor -%}
{%- endif -%}
{%- if 'device_id' in target_lights_input -%}
{%- set device_ids = target_lights_input.device_id -%}
{%- if device_ids is string -%}
{%- set device_ids = [device_ids] -%}
{%- endif -%}
{%- for device in device_ids -%}
{%- for light_entity in expand(device_entities(device) | select('match', 'light')) | list -%}
{%- if not light_entity.entity_id in ns.lights -%}
{%- set ns.lights = ns.lights + [light_entity.entity_id] -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{%- if 'area_id' in target_lights_input -%}
{%- set area_ids = target_lights_input.area_id -%}
{%- if area_ids is string -%}
{%- set area_ids = [area_ids] -%}
{%- endif -%}
{%- for area_id in area_ids -%}
{%- for light_entity in expand(area_entities(area_id ) | select('match', 'light')) | list -%}
{%- if not light_entity.entity_id in ns.lights -%}
{%- set ns.lights = ns.lights + [light_entity.entity_id] -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endif -%}
{{ ns.lights }}
toggle_switch: !input toggle_switch
nightmode_switch: !input nightmode_switch
transition_time: !input transition_time
max_brightness_input: !input max_brightness
nightmode_max_brightness_input: !input nightmode_max_brightness
max_brightness: >-
{%- if states[nightmode_switch].state == 'on' -%}
{{ nightmode_max_brightness_input | float }}
{%- else -%}
{{ max_brightness_input | float }}
{%- endif -%}
main_light_scale_input: !input main_light_scale
main_light_scale: "{{ main_light_scale_input | float | multiply(0.01) }}"
occupancy_sensor_input: !input occupancy_sensor
vacancy_scale_input: !input vacancy_scale
vacancy_scale: >-
{%- if states[occupancy_sensor_input].state == 'on' -%}
{{ 1 | int }}
{%- else -%}
{{ vacancy_scale_input | float | multiply(0.01) }}
{%- endif -%}
min_color_temp: !input min_color_temp
max_color_temp: !input max_color_temp
light_temperature: >-
{{ [([((1000000/(4791.67 - 3290.66/(1 + 0.222 * ([([0,state_attr('sun.sun', 'elevation')]|max),90]|min**0.81))))|int),min_color_temp]|max),max_color_temp]|min }}
elevation_on: !input elevation_on
elevation_off: !input elevation_off
elevation_range: "{{ elevation_off - elevation_on }}"
sun_elevation: "{{ state_attr('sun.sun','elevation') }}"
sun_scale: "{{ 1 - ((sun_elevation - elevation_on) / (elevation_range)) }}"
standard_brightness: >-
{%- if sun_elevation < elevation_on -%}
{{ max_brightness | multiply(vacancy_scale) | int | string }}
{%- elif sun_elevation > elevation_off -%}
{{ 0 | int | string }}
{%- else -%}
{{ max_brightness | multiply(sun_scale) | multiply(vacancy_scale) | int | string }}
{%- endif -%}
main_light_brightness: "{{ standard_brightness | multiply(main_light_scale) | int | string }}"
standard_lights: >-
{%- set ns = namespace( lights = [] ) -%}
{%- for light_entity in (target_lights | list) -%}
{%- if not 'Main' in state_attr(light_entity, 'friendly_name') -%}
{%- if not 'color_temp' in state_attr(light_entity, 'supported_color_modes') -%}
{%- set ns.lights = ns.lights + [light_entity] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.lights }}
standard_lights_with_color: >-
{%- set ns = namespace( lights = [] ) -%}
{%- for light_entity in (target_lights | list) -%}
{%- if not 'Main' in state_attr(light_entity, 'friendly_name') -%}
{%- if 'color_temp' in state_attr(light_entity, 'supported_color_modes') -%}
{%- set ns.lights = ns.lights + [light_entity] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.lights }}
main_lights: >-
{%- set ns = namespace( lights = [] ) -%}
{%- for light_entity in (target_lights | list) -%}
{%- if 'Main' in state_attr(light_entity, 'friendly_name') -%}
{%- if not 'color_temp' in state_attr(light_entity, 'supported_color_modes') -%}
{%- set ns.lights = ns.lights + [light_entity] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.lights }}
main_lights_with_color: >-
{%- set ns = namespace( lights = [] ) -%}
{%- for light_entity in (target_lights | list) -%}
{%- if 'Main' in state_attr(light_entity, 'friendly_name') -%}
{%- if 'color_temp' in state_attr(light_entity, 'supported_color_modes') -%}
{%- set ns.lights = ns.lights + [light_entity] -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ ns.lights }}
trigger:
- platform: state
entity_id: sun.sun
attribute: elevation
- platform: state
entity_id:
- !input occupancy_sensor
from: null
to: null
- platform: state
entity_id:
- !input toggle_switch
from: null
to: null
- platform: state
entity_id:
- !input nightmode_switch
from: null
to: null
condition:
- condition: state
entity_id: !input toggle_switch
state: "on"
action:
parallel:
- service: system_log.write
data:
level: warning
logger: homeassistant.components.blueprint.basic_light_control
message: "
DEBUG:\n
occupancy sensor input: {{ occupancy_sensor_input }}\n
occupancy sensor state: {{ states[occupancy_sensor_input].state }}\n
sun elevation: {{ sun_elevation }}\n
SCALE FACTORS:\n
sun: {{ sun_scale }}\n
main light: {{ main_light_scale }}\n
vacancy: {{ vacancy_scale }}\n
BRIGHTNESS:\n
standard: {{ standard_brightness }}%\n
main: {{ main_light_brightness }}%\n
LIGHTS\n
{{ target_lights_input }}\n
{{ target_lights }}\n
"
- service: light.turn_on
target:
entity_id: "{{ standard_lights }}"
data:
transition: "{{ transition_time }}"
brightness_pct: "{{ standard_brightness }}"
- service: light.turn_on
target:
entity_id: "{{ standard_lights_with_color }}"
data:
transition: "{{ transition_time }}"
brightness_pct: "{{ standard_brightness }}"
color_temp: "{{ light_temperature }}"
- service: light.turn_on
target:
entity_id: "{{ main_lights }}"
data:
transition: "{{ transition_time }}"
brightness_pct: "{{ main_light_brightness }}"
- service: light.turn_on
target:
entity_id: "{{ main_lights_with_color }}"
data:
transition: "{{ transition_time }}"
brightness_pct: "{{ main_light_brightness }}"
color_temp: "{{ light_temperature }}"
mode: parallel