Hi!
Here is a long answer to a short question:
I use input_boolean to choose if an automation should be active. Typically I want notifications if the lighting changes or if the outdoor lighting should be activated by timer or sunset.
alias: 'Wake up!'
trigger:
- platform: time
at: '06:30:00'
condition:
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
- condition: state
entity_id: input_boolean.common_lights_schedule
state: 'on'
action:
...
I use input_selects for choosing states, typically for example “22 degrees inside” or “Relaxed lighting”.
But since I also want to change lighting from automations, scripts and manual switches - and I want the state to be saved, so whenever the input_select’s value changes, I save the chosen state in a variable and trigger the actual change from there.
For example (lighting):
A change in the value of a input_select.lights01
triggers an automation.
The automation sets a new value in the variable.light_state
, then sets the input_select back to Välj
(that’s Swedish for “Select” ).
alias: 'Light trigger'
trigger:
platform: state
entity_id: input_select.lights01
condition:
condition: template
value_template: "{{ states.input_select.lights01.state != 'Välj' }}"
action:
- service: variable.set_variable
data:
variable: light_state
value_template: '{{ states("input_select.lights01") }}'
- service: input_select.select_option
data:
entity_id: input_select.lights01
option: 'Välj'
- service: script.light_sequence
The change in variable.light_state
triggers an automation that runs a script that checks the current state of variable.light_state
:
light_sequence:
sequence:
- service: script.set_light_off
- service: script.set_light_bright
- service: script.set_light_wakeup
...
set_light_off:
sequence:
- condition: state
entity_id: variable.light_state
state: !secret light_off
- service: script.light_off
set_light_bright:
sequence:
- condition: state
entity_id: variable.light_state
state: !secret light_bright
- service: script.light_bright
...
…which triggers the script:
light_wakeup:
alias: 'Wake up!'
sequence:
- service: script.tradfri_weekday
- service: light.turn_on
data:
entity_id: group.lights_dim_grp
transition: 1
brightness_pct: 90
- service: switch.turn_on
data:
entity_id:
- group.lights_switch_grp
- group.lights_basement_grp
- service: variable.set_variable
data:
variable: light_state
value: !secret light_wakeup
- service: notify.email
data:
title: 'Lighting changed'
message: !secret light_wakeup
I use parameters like !secret light_off
instead of the actual names not because the’re secret, but to make sure I don’t do any typos and to be able to change their values at one place.
Why this complex setup?
-
To make sure persistence works. HASS-Persistence automatically restores the values of my variables. And I have a startup script for re-setting the lighting, alarm and climate.
This script is the same script that the input_select triggers at change (see above) script.light_sequence
.
-
It’s very easy to set a new value to the variable.light_state
from other scripts and automations. And with this setup, I don’t run into the problem where the input_select.lights01
would show incorrect value which could happen if I ran scripts that changed the lighting without changing the value of the input_select.lights01
. The input_select.lights01
always shows “Select”. And it’s easy to show current status.
Easy for the other family members to understand
-
I’m sure there are more elegant ways to accomplish this, but for me the structure makes it quite easy to have clean, short scripts and automations. It might seem complex, but I think it’s easy to follow and easy to duplicate for each purpose.