toggle_ikea_light:
sequence:
choose:
# Check whether the light is off and turn it on
conditions:
- condition: template
value_template: "{{ is_state(entity, 'off') }}"
sequence:
# Set the brightness of the lights.
- service: light.turn_on
data_template:
entity_id: "{{ entity }}"
brightness_pct: "{{ brightness_pct }}"
transition: 0.1
- delay: '00:00:00.1'
# Set the color temperature.
- service: light.turn_on
data_template:
entity_id: "{{ entity }}"
color_temp: "{{ color_temp }}"
transition: 0.1
default:
# Turn off the light if it was on
- service: light.turn_off
data_template:
entity_id: "{{ entity }}"
For future reference, the Solution tag is assigned to the first post that suggests the solution to the problem (only one post in the thread can have the Solution tag). For more information, refer to guideline 21 in the FAQ.
Understood. My intention was to present correct code to a person, who would visit this in future. Modified to first post with correct solution
Thanks again!
I understand and that’s a good intention but in that case you should present an example that uses current conventions (as opposed to functional but outdated conventions).
For example, data_template was deprecated in favor of data many versions ago and the desired entity (or entities) is referenced with the target option.
The following example uses current conventions plus a few other handy things like a Template Condition in shorthand notation and defining a sub-second delay using the milliseconds option.
As mentioned in first post, I have no experience in scripts yet. Until Today I was exploring GUI & node red.
I already asked myself what is the difference between data and data template, why I can find different examples. And here we go - conventions.
My monster has been born by taking a working script from the Internet, modifying it to my needs according to documentation you linked and built-in validator.
Thank you for pointing me the right direction.
Do we have any recommended tools to validate the code, but also detect old conventions or other things I should avoid to create good code?
Most of the official documentation gets updated when there’s a significant change to scripting. If you are using an example from the forum (or elsewhere) that was created over a half year ago or more then it’s likely to be employ deprecated/outdated methods.