#for_each loop

1 messages · Page 1 of 1 (latest)

calm willow
#

Thanks for helping, so I don't need any condition for this automation?

real meteor
#

Not in the condition: section. And within each loop, use an if: action to check if the state of the current entity is on.

calm willow
#

so for example I have my automation now:

repeat:
  for_each:
    - input_select.office_tv_status
    - input_select.livingroom_tv_status
  sequence:
    - service: light.turn_off
      target:
        entity_id: "light.{{ repeat.item }}"

Each TV has it own script to turn off/on the device. how do I put it correctly on the sequence section? like IF input_select.office_tv_status is ON then action the script to turn it on/off
and if the state of the same TV is off do nothing.

#

script.ir_turn_on_off_living_room_tv
script.ir_turn_on_off_bedroom_tv
those for example my two scripts for living room and bedroom tv's

real meteor
#
repeat:
  for_each:
    - helper: input_select.office_tv_status
      script: script.ir_turn_on_off_office_tv
    - helper: input_select.livingroom_tv_status
      script: script.ir_turn_on_off_livingroom_tv
    - ...
calm willow
#

but how do he knows if I want it to check if device state is on that way

#

can you paste it to pastie or something? when I copy it it's mix up the lines

real meteor
#
  sequence:
    - if: "{{ is_state(repeat.item.helper, 'on') }}"
      then:
        - service: "{{ repeat.item.script }}"
#

Sorry tapping this out on a phone

#

But you get the gist.

calm willow
#

Mmmm.. let me try;
What about if one of my TV's is not via script it's by doing a switch toggle? (office tv)

real meteor
#

The idea is that in each for_each loop, the treatment of each entity needs to be the same. Thus you need one loop for entities where you can check the state directly and just call homeassiatant.turn_off or similar, and another for TVs where the state is tracked by a helper and the action is an ir command. Ergo if you have a third type of entity (eg. Your TV that is toggled by a switch), you should implement yet another loop. Or if it's just one entity, you can just provide an action in your automation sequence to handle just that entity specifically.

calm willow
#

so another action with a loop

#

I get this error

Message malformed: Entity {{ repeat.item.helper }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['action'][0]['repeat']['sequence'][0]['if'][0]['entity_id']
#
description: Will turn off lights & TV's in the entire house that are not in the Bedroom.
mode: single
trigger:
  - platform: state
    entity_id:
      - sensor.kitchen_sticker_switch_button_action
condition: []
action:
  - repeat:
      for_each:
        - helper: input_select.living_room_tv_status
          script: script.ir_turn_on_off_living_room_tv
      sequence:
        - if:
            - condition: state
              entity_id: "{{ repeat.item.helper }}"
              state: "On"
          then:
            - service: "{{ repeat.item.script }}"
alias: Test - Bedroom Button
#

That's the entire yaml so far

real meteor
#

Hmm it looks like entity_id: does not accept templates.

#

Try this instead:
- if: "{{ is_state(repeat.item.helper, 'on') }}"

#

I.e. delete the condition: block under if:

#

To change to a template condition instead of a state condition.

#

Edited the codeblock above too to reflect this.

calm willow
#
description: Will turn off lights & TV's in the entire house that are not in the Bedroom.
mode: single
trigger:
  - platform: state
    entity_id:
      - sensor.kitchen_sticker_switch_button_action
condition: []
action:
  - repeat:
      for_each:
        - helper: input_select.living_room_tv_status
          script: script.ir_turn_on_off_living_room_tv
      sequence:
        - if: "{{ is_state(repeat.item.helper, 'On') }}"
          then:
            - service: "{{ repeat.item.script }}"
alias: Test - Bedroom Button