I have a small problem with a script I’m working on and I’m clearly missing something so I wondered if someone would see the errors of my ways.
I’m trying to set the light level of different light in script. I will eventually call that script in an automation based on time to decrease the lights in the late afternoon. The problem I’m facing is that I want to make sure a light stays off if it was off in the first place. To try and do this I assigned the state of the lights in variables and I tried to use condition to then check if the state of the light changed since I applied the scene and if so toggle the light back to its original state. That last part (the check) is not working right now and this is were I’m scratching my head now.
Here is my current script:
lumiere_soiree:
alias: Lumière de soirée
icon: mdi:test-tube
mode: single
variables:
cuisine_state: {{ states('light.cuisine') }}
salleDeBain_state: {{ states('light.salle_de_bain') }}
chambre_state: {{ states('light.chambre') }}
sequence:
# Activate the scene
- scene: scene.test_scene
# reset the state of the kitchen light
- choose:
- conditions: not
- condition: state
entity_id: light.cuisine
state: "{{ cuisine_state }}"
sequence:
- service: light.toggle
target:
entity_id: light.cuisine
If that’s really the whole thing, then you don’t need the choose: and can do this:
sequence:
# Activate the scene
- scene: scene.test_scene
# reset the state of the kitchen light
- condition: template
value_template: "{{ not is_state('light.cuisine', cuisine_state }}"
- service: light.toggle
target:
entity_id: light.cuisine
Yes, three choose: blocks is one approach. Another is using a template that generates a list of entities that you can then use in the target: block, but that would require understanding more about what you’re trying to do, and would be more complicated.
ok so basically at 7pm every night I want la list of light to be dimmed (which the scene does).
Now the requirements for my script are:
the light level must be decreased whether the light is on or off when the script is triggered
the state of the light must remain the same after the script as run
Limitation:
the light dimmer I have are TP-Link casa and it’s not possible to change the light level value without turning them on.
I’m fairly new to Home Assistant and the yaml + templating syntax, but have a fair background in programming, the complexity is not the challenge, but just being aware of the options in the syntax. If there is a way to do a for loop with templating, I’m all for it and especially learning it for futur uses.
The solution I was thinking of (in pseudo-code)
activate scene.lumiere_soiree
if (people_home.count > 0)
{
activate string_light_1
}
foreach (dimmer in my_dimmer_list)
{
if (currentState != OriginalState)
{
dimmer.turnOff // I switch to turn off for clarity since ativating the since will turn on the light
}
Now my current solution is:
lumiere_soiree:
alias: Lumière de soirée
icon: mdi:weather-sunset-down
mode: single
variables:
cuisine_state: '{{ states(''light.cuisine'') }}'
salleDeBain_state: '{{ states(''light.salle_de_bain'') }}'
chambre_state: '{{ states(''light.chambre'') }}'
sequence:
# Activate the scene
- scene: scene.test_scene
# Check to activate the string light if someone is home
- choose:
- conditions:
- condition: state
entity_id: group.family
state: Home
sequence:
- type: turn_on
device_id: 1eaa1cb4fac395ab403b11ab78dd509b
entity_id: switch.party_2
domain: switch
# Check if any light must be turn of
- choose:
- conditions:
- condition: template
value_template: '{{ not is_state(''light.cuisine'', cuisine_state) }}'
sequence:
- service: light.toggle
target:
entity_id: light.cuisine
- choose:
- conditions:
- condition: template
value_template: '{{ not is_state(''light.salle_de_bain'', salleDeBain_state)
}}'
sequence:
- service: light.toggle
target:
entity_id: light.salle_de_bain
- choose:
- conditions:
- condition: template
value_template: '{{ not is_state(''light.chambre'', chambre_state) }}'
sequence:
- service: light.toggle
target:
entity_id: light.chambre
Not that it matters any more but in your first example, each variable’s template was missing outer quotes.
You can condense the code by employing Template Conditions in shorthand notation. Also, the state value for the group is home not Home. An entity’s state value is what you see in Developer Tools > States and not in the Lovelace UI.
lumiere_soiree:
alias: Lumière de soirée
icon: mdi:weather-sunset-down
mode: single
variables:
cuisine_state: '{{ states(''light.cuisine'') }}'
salleDeBain_state: '{{ states(''light.salle_de_bain'') }}'
chambre_state: '{{ states(''light.chambre'') }}'
sequence:
# Activate the scene
- scene: scene.test_scene
# Check to activate the string light if someone is home
- choose:
- conditions: '{{ is_state(''group.family'', ''home'') }}'
sequence:
- service: switch.turn_on
target:
entity_id: switch.party_2
# Check if any light must be turn of
- choose:
- conditions: '{{ states(''light.cuisine'') != cuisine_state }}'
sequence:
- service: light.toggle
target:
entity_id: light.cuisine
- choose:
- conditions: '{{ states(''light.salle_de_bain'') != salleDeBain_state }}'
sequence:
- service: light.toggle
target:
entity_id: light.salle_de_bain
- choose:
- conditions: '{{ states(''light.chambre'') != chambre_state }}'
sequence:
- service: light.toggle
target:
entity_id: light.chambre