Access all entity ids used in a list of state triggers

Hey there,

I have a state trigger using several input_booleans. As a result of any of them changing their state to “on” I would like to switch all others to “off”.
I cant seem to find a way to reference the entire list of trigger entity ids though (in order to set the corresponding booleans to “off”) but only the boolean triggering/changing it’s state to “on” via trigger.entity_id.

I feel like I am missing something really obvious. Anybody know how to generate/reference all triggers? Or is there a more elegant way manipulate “all others”?

Cheers :slight_smile:

Welcome to the community!

Inspired by Drew’s post, I’m using the following automation (to simulate a tab bar):


automation:

- id: '202303071751'
  alias: Navigation Dashboard Lampen
  description: packages/automationen/navigation_dashboard_lampen.yaml

  variables:
    boolean: |-
      {%- set booleans = 
        [
          'input_boolean.diele',
          'input_boolean.schlafen',
          'input_boolean.kuche',
          'input_boolean.wohnen',
          'input_boolean.aussen'
        ] |reject('eq', trigger.entity_id) -%}
      {{ booleans |list }}

  trigger:
  - platform: state
    entity_id:
    - input_boolean.diele
    - input_boolean.schlafen
    - input_boolean.kuche
    - input_boolean.wohnen
    - input_boolean.aussen
    to: 'on'

  action:
  - service: input_boolean.turn_off
    target:
      entity_id: '{{ boolean }}'


1 Like

Thanks! It’s a great place :slight_smile:

And that is a nicely working approach, thank you.

I realized that you cannot use templates in the trigger statement? Then at least we could’ve avoided the double manual entry.

I will eventually try to make it so that I have to enter the list of booleans only once and preferably via the GUI for its selection and typo-avoidance capabilities :wink:.

If anyone has any suggestions to move in those directions that’d be most welcome :slight_smile:

Just played around. You can use trigger_variables:


trigger:
  - platform: template
    value_template: |
      {{ booleans |select('is_state', 'on') |list |count == 2 }}
action:
  - service: notify.persistent_notification
    data:
      message: test
  - service: input_boolean.turn_off
    target:
      entity_id: "{{ booleans |reject('eq', trigger.entity_id) |list }}"
trigger_variables:
  booleans: |-
    {{
      [
        'input_boolean.diele',
        'input_boolean.schlafen',
        'input_boolean.kuche',
        'input_boolean.wohnen',
        'input_boolean.aussen'
      ] }}
mode: queued
max: 10

2 Likes

Ah that’s great!

In the meantime I found a way to just draw all entity ids from states and filter them. That kind of shifts the problem to a more rigorous naming convention (I’m filtering for all input booleans that are “tagged” Room Occupation), but I’m confident I need that anyways.

That way I can use the GUI to add the triggers (technically I could use your approach + the filter to populate that list, automatically, too - so I’d only have to create the booleans) which at this point feels more convenient.

alias: Turn Off Other Input Booleans
trigger:
  platform: state
  entity_id:
    - input_boolean.room_occupation_basement
    - input_boolean.room_occupation_bathroom_downstairs
    - input_boolean.room_occupation_bathroom_upstairs
    - input_boolean.room_occupation_bedroom
    - input_boolean.room_occupation_gaming_room
    - input_boolean.room_occupation_kitchen
    - input_boolean.room_occupation_living_room
    - input_boolean.room_occupation_office
    - input_boolean.room_occupation_stairs

action:
  - service: input_boolean.turn_off
    target:
      entity_id: >
        {% set triggering_id = trigger.entity_id %}
        {% set filtered_ids = states | selectattr('entity_id', 'match', '^input_boolean\.room_occupation_.*') | map(attribute='entity_id') | reject('eq', triggering_id) | list %}
        {{ filtered_ids | join(', ') }}

What do you think?

Have a nice Sunday evening!

1 Like

I think that’s what counts.

There’s no reason to run a search on the entity ID of every entity in your instance when you know you only want Input booleans.

{% set triggering_id = trigger.entity_id %}
{% set filtered_ids = states.input_boolean 
| selectattr('object_id', 'match', 'room_occupation_.*') 
| map(attribute='entity_id') | reject('eq', triggering_id) | list %}
{{ filtered_ids | join(', ') }}