Script to conditionally turn off multipule devices IF they are on?

Basically I want a sequence that checks each TV and turns them off only if they are on.

Now since they are smart TVs I have created binary sensors to check if they are on, and I want to use the new broadlink switch module to turn them off, however the command is a toggle so I only want to call it IF the TV is already on.

I have been poking around at the condition formats which I don’t think will work because I want to check each TV not one TV in this script.

I also looked at the {% if %} syntax but nothing in the examples seems to fit.

Maybe you can use a sequence of scripts, and each script contains a condition to check if TV is ON.

Something like this:

automation:
- alias: "Turn TVs off"
  trigger:
     your triggers here
  action:
    service: script.turn_on
    entity_id: script.check_tvs

...

script:
  check_tvs
    sequence:
      - service: script.turn_on
         entity_id: script.check_tv1
      - service: script.turn_on
         entity_id: script.check_tv2

  check_tv1:
    sequence:
    - condition:
        condition: state
        entity_id: sensor.tv1
        state: 'on'
    - service: homeassistant.turn_off
      entity_id: switch.tv_1
  check_tv2:
    sequence:
    - condition:
        condition: state
        entity_id: sensor.tv2
        state: 'on'
    - service: homeassistant.turn_off
      entity_id: switch.tv_2
2 Likes

I will give that a try, I thought condition blocks effected the WHOLE script not individual sequences.

Looks like this worked perfectly, realized after reading it that you where suggesting a script that calls two scripts.

I wish there was a slightly more syntactically efficient way to do this but it works.

I was looking for same solution as you. I can up with this. Slightly different. You just need one line in the automation to loop through all your script device checks.
automation:

- alias: 'Turn off all devices on'
  trigger: 
  - platform: state
    entity_id: group.all_home
    to: 'off'
  action:
  - service: script.turn_on
    entity_id: script.dev_checks

script:

  dev_checks:
    sequence:
      - condition: template
        value_template: '{{ states("switch.ac_lr_unit") != "off" }}'
      - service: switch.turn_off
        entity_id: switch.ac_lr_unit
      - service: script.turn_on
        data:
          entity_id: script.check_2
  check_2:
    sequence:
      - condition: template
        value_template: '{{ states("switch.ac_mr_unit") != "off" }}'
      - service: switch.turn_off
        entity_id: switch.ac_mr_unit
      - service: script.turn_on
        data:
          entity_id: script.check_3
  check_3:
    sequence:
      - condition: template
        value_template: '{{ states("switch.sr_ac") != "off" }}'
      - service: switch.turn_on
        entity_id: switch.sr_ac
      - service: script.turn_on
        data:
          entity_id: script.check_4
  check_4:
    sequence:
      - condition: template
        value_template: '{{ states("switch.heat_seat") != "off" }}'
      - service: switch.turn_off
        entity_id: switch.heat_seat

1 Like