I’m trying to create a script to control a group of lights: if any of them are on, turn the whole group off, otherwise turn the whole group on. This is my first attempt at it
group_toggle:
alias: Toggle all lights on or off
sequence:
- service_template: >
{%- set initialstate = states(lightgroup) -%}
{%- if initialstate == 'on' -%}
light.turn_off
{%- else -%}
light.turn_on
{%- endif -%}
data_template:
entity_id: "{{ lightgroup }}"
transition: 3
kelvin: 3000
brightness: 200
This works to turn the group on, but fails to turn the group off because kelvin
and brightness
aren’t accepted by the light.turn_off
service. So I’ve been trying to work out how to exclude those attributes based on the value of the initialstate
variable.
I’ve tried a few variations on just removing the entire attribute like this:
data_template:
entity_id: "{{ lightgroup }}"
{%- if initialstate == 'off' -%}
kelvin: 3000
{%- endif -%}
but they all resulted in parse errors and the script failing to load. My next attempt was trying to set the attribute to an empty value like this:
data_template:
entity_id: "{{ lightgroup }}"
kelvin: >
{%- if initialstate == 'off' -%}
3000
{%- endif -%}
Again, this works fine when the group is being turned on, but when it’s being turned off the validator still complains about the kelvin key being present, even though it’s an empty value.
I’m running out of ideas. Is what I want to do possible here?