Advice to make an input_select more efficient

Hello. Home Assistant Noob here.
So… after many, many hours, days even, i’ve managed to get my HASS configured with 3 lamps and a Xiaomi button. Using input_select i’ve been able to configure the button so each single press cycles through 4 states:
The lamps are called “Hall”, “Stairs” and “Snooker”
Lamp Hall on
Lamp Hall/Stairs on
Lamp Hall/Stairs/Snooker on
Lamps off
It works like a charm.
I’m hoping someone can give me some advise to reduce this code as I feel it’s inefficient. First here’s the configuration.yaml

scene:
  - name: Hall Light
    entities:
      switch.sonoffsocket01:
        state: 'on'
      switch.0x000d6ffffe6248f5_switch:
        state: 'off'
      switch.sonoffsocket03:
        state: 'off'

  - name: Hall Stairs Light
    entities:
      switch.sonoffsocket01:
        state: 'on'
      switch.0x000d6ffffe6248f5_switch:
        state: 'on'
      switch.sonoffsocket03:
        state: 'off'

  - name: Snooker Hall Stairs Light
    entities:
      switch.sonoffsocket01:
        state: 'on'
      switch.0x000d6ffffe6248f5_switch:
        state: 'on'
      switch.sonoffsocket03:
        state: 'on'

  - name: Hall Off
    entities:
      switch.sonoffsocket01:
        state: 'off'
      switch.0x000d6ffffe6248f5_switch:
        state: 'off'
      switch.sonoffsocket03:
        state: 'off'

input_select:
  hall_scenes:
    name: Hall Scenes
    options:
      - Hall Light
      - Hall Stairs Light
      - Snooker Hall Stairs Light
      - Hall Off
    initial: Hall Off
    icon: mdi:lightbulb-on-outline

#Cycle Hall Lights
automation:
  - alias: Cycle Hall Lights
    trigger:
    - entity_id: sensor.0x00158d0003614c8f_click
      platform: state
      to: single
    action:
      service: input_select.select_next
      data:
        entity_id: input_select.hall_scenes      

  - alias: Hall Light
    description: ''
    trigger:
    - entity_id: input_select.hall_scenes
      platform: state
      to: Hall Light
    condition: []
    action:
    - scene: scene.hall_light

  - alias: Hall Stairs Light
    description: ''
    trigger:
    - entity_id: input_select.hall_scenes
      platform: state
      to: Hall Stairs Light
    condition: []
    action:
    - scene: scene.hall_stairs_light

  - alias: Snooker Hall Stairs Light
    description: ''
    trigger:
    - entity_id: input_select.hall_scenes
      platform: state
      to: Snooker Hall Stairs Light
    condition: []
    action:
    - scene: scene.snooker_hall_stairs_light

  - alias: Hall Off
    description: ''
    trigger:
    - entity_id: input_select.hall_scenes
      platform: state
      to: Hall Off
    condition: []
    action:
    - scene: scene.hall_off

My feeling is that the last 4 events could be combined into one event that passes the scene name as part of the action.

Any ideas?

Thanks in advance.

  • Michael D

Your 4 automations can be shrunk into 1 because of the way you named things.

  - alias: Hall Off
    description: ''
    trigger:
    - entity_id: input_select.hall_scenes
      platform: state
    condition: []
    action:
    - service: scene.turn_on
      data_template:
        entity_id: scene.{{ trigger.to_state.state.replace(' ','_').lower() }}

you’re able to do this because you named your input selects “Hall Off” which can be converted to hall_off by replacing the spaces with underscores and converting it to all lowercase.

You sir are a genius! Thank you so much. Worked perfectly.