Get scene name from combining input_select name and content

Good Day,

I’m right now switching from openhab to home assistant, an I’m trying to migrate some of my automations/rules.

While doing so I encountered a question: If I have a input_select called “bedroom_light” and some options in it like “On”. “Off”, “Night” is there a way to take the name of the input_select and the name of the selected option to then build the scene name out of it like “bedroom_light_on”?

Yes and no.

Yes you can create the name with a template, e.g. try this in Developer Tools > Template

{{ states.input_select.bedroom_light.object_id ~ '_' ~ states('input_select.bedroom_light')|slugify}}

However you can only template the scene_id and only if creating the scene on the fly.

Ah ok so if I created all my scenes beforehand and trying to activate them with this little snippet in an automation it won’t work?

If when you created the scenes you named them scene.bedroom_light_on and scene.bedroom_light_off, and scene.bedroom_light_night and the options in the input select were, On, Off, and Night, (not case sensitive) then yes it would. The slugify filter changes the option to lower case and replaces spaces and special characters with underscores. So make sure your scene names match what that filter does to your options and you’re good. In this case you can simplify the template a bit too:

trigger:
  platform: state
  entity_id: input_select.bedroom_light # triggers on any change of state
action:
  service: scene.turn_on
  target:
    entity_id: "{{ 'scene.bedroom_light_' ~ states('input_select.bedroom_light')|slugify}}"

Ah okay, I see but this would only work for one input_select, so I would still have to have one for every input_select I’d like to have this working.

You can do this for more than one trigger:

trigger:
  - platform: state
    entity_id: input_select.bedroom_light 
  - platform: state
    entity_id: input_select.kitchen_light 
  - platform: state
    entity_id: input_select.outside_light 
action:
  service: scene.turn_on
  target:
    entity_id: "{{ trigger.to_state.object_id ~ '_' ~  trigger.to_state.state|slugify }}"

You can’t test that template in the template editor as the trigger.to_state will not be defined. You’ll have to test it by using the automation.

Thank you very much, this is exactly what I was searching for.

1 Like

It’s a great idea. I might use it to simplify my scene input selects. Though I have a mixture of scenes and scripts the input select automation calls, so that will need some fixing.

1 Like

Yeah that was basically how my rule on OH worked, and I thought there must be a way to do in in hass but I just couldn’t work it out.

Here are all the trigger variables you have access to if it helps in future:

And all the state objects:

1 Like