Changing input_boolean helper state

Hello,

I am new to automations. I would like that everytime the state of the entities (media_player.zone_11, media_player.zone_12, media_player.zone_14) changes, the corresponding helper variable also changes. I know this may seem pointless, but I am hoping to use it in a more advanced automation later so that I can recall a previous state

Only the zone_11 works below, zone_12 and zone_14 trigger the script but the corresponding helper variable doesn’t change. What am I doing wrong? This is probably a silly way to do this, but I am new, if there is a better way I am open to that too.

alias: New Automation
description: ""
trigger:
  - platform: state
    entity_id:
      - media_player.zone_11
      - media_player.zone_12
      - media_player.zone_14
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == media_player.zone_11 }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.zone1
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == media_player.zone_12 }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.zone2
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == media_player.zone_14 }}"
        sequence:
          - service: input_boolean.toggle
            data: {}
            target:
              entity_id: input_boolean.zone4
mode: queued

I’m surprised any of them work… in the comparisons you need to put single quotes around the entity ID’s. Unquoted, they will be interpreted as variable objects instead of strings.

  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.entity_id == 'media_player.zone_11' }}"

If you can rely on the entity naming such that the last character of the media player’s entity_id is the number of the zone to toggle, you could do this:


description: ""
mode: queued
trigger:
  - platform: state
    entity_id:
      - media_player.zone_11
      - media_player.zone_12
      - media_player.zone_14
condition: []
action:
  - service: input_boolean.toggle
    data: {}
    target:
      entity_id: input_boolean.zone{{ trigger.to_state.entity_id[-1] }}

It only seems like it “works” because choose will stop at the first condition that matches, and neither trigger.entity_id nor media_player.zone_11 are variables that are defined. Therefore, undefined does, in fact, equal undefined.

Try this out in the dev tools template editor and see the result:

{{ variable_that_does_not_exist == another_variable_that_does_not_exist }}
[quote="mekaneck, post:3, topic:643283"] `entity_id: input_boolean.zone{{ trigger.to_state.entity_id[-1] }}` [/quote]
entity_id: input_boolean.zone_1{{ trigger.to_state.entity_id[-1] }}

(You omitted the _1.)

Your correction is what makes sense to me too (from a consistent naming perspective), but the code in the first post has media_player.zone_11 matching up with input_boolean.zone1.

So although it’s not consistent it was intentional. :grin:

1 Like

Indeed! I guess my brain just automatically went there.

Interesting, I didn’t intend for either of them to be variables. But from the other posts, I understand why it looks like that, thanks!

This helps thank you