Conditional execution of individual items in a script?

In scripts, you can add tests for conditions. If the condition fails, the script stops. But I think that’s the only kind of condition test allowed?

I was wondering how easy it would be to allow individual items in a script sequence to have a condition added? Ansible scripts do this with a ‘when’ clause – in HA I guess this might look like:

script:
  example_script:
    sequence:
      - service: do_something

      - service: light.turn_on
        data:
          entity_id: light.ceiling
        when: "{{ sensor.room_brightness < 128 }}"

      - service: do_something_else

I haven’t looked at the code – it may be that this would be tricky for some reason – but it does feel as if the current condition tests are a bit limited.

this is rather easily done using service_templates.

    service_template: >
      script.{{'turn_down_the_house' if is_state('input_boolean.house_turned_down','off') else 'nothing'}}

where dummy is a script doing nothing:

nothing:
  sequence: []

or eg

    - service_template: >
        script.{{'switch_dorm_off' if is_state('binary_sensor.imac_off','on') else 'prompt_imac'}}

hope this helps

1 Like

Ha! I admire the ingenuity of this! But I do think it would be better to have a solution that didn’t require such ingenuity…