Light toggle script

Hi Guys!

I would like to use a single script to toggle Ikea Tradfri bulb.

I cannot set both brightness and color in a single call, so I use this script to turn on and configure a light:

toggle_ikea_light:
  sequence:
    # Check whether the light is off (optional)
    - condition: template
      value_template: "{{ is_state(entity, 'off') }}"
    - delay: '00:00:01'
    # Set the brightness of the lights.
    - service: light.turn_on
      data_template:
        entity_id: "{{ entity }}"
        brightness_pct: "{{ brightness_pct }}"
        transition: 1
    - delay: '00:00:01'
    # Set the color temperature.
    - service: light.turn_on
      data_template:
        entity_id: "{{ entity }}"
        color_temp: "{{ color_temp }}"
        transition: 5

   # Check whether the light is on
    - condition: template
      value_template: "{{ is_state(entity, 'on') }}"
    - service: light.turn_off
      data_template:
        entity_id: "{{ entity }}"

To simplify automations, I would like to turn off the bulb if it is ON when script is being called.

When I add another condition template in the end, with condition is_state==on, it is not being executed, as script exits on first condition.

How to configure “if” "&“else” in this script?

In next step I would like to automate color & brightness calculation based on time.

In the end, it should work like this:

  • if light is on - turn it off
  • if light is off and brightness & color is specified - turn it on and configure given parameters
  • if light is off and brightness or color not specified - calculate not specified value, turn on the light and configure brightness & color.

It is my first script, so if there is any better method or more elegant way to achieve my goal - I would appreciate any suggestions.

Kind Regards

With a choose.

1 Like

Thank you, this works for me:


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 :wink:
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.

toggle_ikea_light:
  sequence:
    choose:
      conditions: "{{ is_state(entity, 'off') }}"
      sequence:
        - service: light.turn_on
          target:
            entity_id: "{{ entity }}"
          data:
            brightness_pct: "{{ brightness_pct }}"
            transition: 0.1
        - delay:
            milliseconds: 100
        - service: light.turn_on
          target:
            entity_id: "{{ entity }}"
          data:
            color_temp: "{{ color_temp }}"
            transition: 0.1
    default:
      - service: light.turn_off
        target:
          entity_id: "{{ entity }}"
1 Like

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.