Set input_select when changing scene

I have created a couple of scenes:

scene.livingroom_morning
scene.livingroom_evening

I created an input_select to track which one was last activated:

input_select.living_room_scene:
  options:
  - scene.livingroom_morning
  - scene.livingroom_evening

I’d like to sync these two. I set up an automation to change the scene with the input_select changes:

  alias: Living Room scene automation
  description: ''
  trigger:
  - platform: state
    entity_id:
    - input_select.living_room_scene
  condition: []
  action:
  - service: scene.turn_on
    target:
      entity_id: '{{ states(''input_select.living_room_scene'') }}'
    metadata: {}
  mode: single

This works great. I’d also like to sync scene activations to update the input select (it should not create a recursion). I tried creating this automation but it doesn’t work:

  alias: Living Room scene to switch
  description: ''
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: scene
      service: turn_on
  condition: []
  action:
  - service: input_select.select_option
    target:
      entity_id: input_select.living_room_scene
    data:
      option: '{{ trigger.event.data.service_data.entity_id }}'
  mode: single

According to the logbook this runs without errors but the input_select is not changed. A few times it worked however:

Any idea what might be going on?

Bonus:

  1. Can I run this conditionally only for scenes that start with scenes.livingroom?
  2. Can I avoid this automation triggering the scene switch again? (when it works)

PS: I know I could add a manual input_select set to each scene, but I want to automate this instead.

It seems that the issue is that sometimes (but not always) the entity_id is a string, but usually it’s an array.

Shouldn’t this be input_select.set_options ?

That would change the list of selectable options. select_option is correct.

But this is a loop. Automation 2 triggers automation 1.

Yes, but automation 1 is only triggered when the input_select changes value - if it’s set to the same value it was it will not happen.

Anyway, I can only suggest to look at the automation trace. Tested your second automation and it’s running flawlessly :man_shrugging:

To answer your first bonus question: You can limit the execution by using the following condition:


condition:
  - condition: template
    value_template: |
      {% set trigger = trigger.event.data.service_data.entity_id %}
      {{ trigger is search('livingroom_morning|livingroom_evening') }}

Yeah it breaks when invoked from certain places. In the end it’s this old “feature”:

As for the condition, I ended up going with this one liner:

{{ 'living_room' in trigger.event.data.service_data.entity_id }}

FWIW, the second automation can be eliminated if you constrain your other automations/scripts to set the desired scene exclusively via the input_select and not directly via a scene.turn_on service call.

I’m thinking about going the other direction now where everything is done via turning on scenes.

OK. What was the original requirement for using an Input Select to choose a scene? Is that requirement no longer needed?