Cycle through scenes with a button?

Wow @jimpower that is quite impressive setup and documentation you did there. I will definitely take a deeper look at your “Kingia Castle” setup as it is exactly this kind of home automation I’m going toward. Big thanks for sharing!!

I’ll try your code this week-end.

@Joachim_Ahm_Mortense this should work if input_select.select_next isn’t working.

trigger:
  - platform: state
    entity_id: sensor.living_room_switch
    to: 4_click
action:
  - service: input_select.select_option
    entity_id: input_select.living_room_scenes
    data_template:
      option: >
        {% set options = state_attr('input_select.living_room_scenes','options') -%}
        {% set selection = options.index(states('input_select.living_room_scenes')) -%}
        {% if (selection + 1) >= options | length -%}
        {{ options[0] }}
        {% else %}
        {{ options[selection + 1] }}
        {% endif %}
4 Likes

also trying same setup, but cant get it work. This is in my automation yaml. What im doing wrong?

input_select:
  yeecolors:
    name: Color mode
    options:
      - dim    
      - bright
- name: dim
  entities:
  light.yeelight_rgb_7c49eb0ff332:
    state: on
    transition: 2
    brightness_pct: 10
    kelvin: 5000
    
- name: bright
  entities:
  light.yeelight_rgb_7c49eb0ff332:
    state: on
    transition: 2
    brightness_pct: 100
    kelvin: 5000
    
  ##Cycle yeelight colors
- alias: Change light scenes
  trigger:
platform: event
event_type: click
event_data:
  entity_id: binary_sensor.switch_158d0001d8ea53
  click_type: double
  action:
service: input_select.select_next
data:
  entity_id: input_select.yeecolors

Can you please format your code by highlighting it and selecting the </> option from your window so I can see indentation cheers

full automation.yaml. lights on/off works well, long click works well, cycling doesnt work. Please, give advise

- alias: Toggle dining light on single press
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001d8ea53
      click_type: single
  action:
    service: light.toggle
    entity_id:
    - light.yeelight_rgb_7c49eb0ff332
    - light.yeelight_rgb_7c49eb1028fd
    
- alias: Turn on light scene long click
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001d8ea53
      click_type: long_click_press
  action:
    service: light.turn_on
    entity_id:
    - light.yeelight_rgb_7c49eb0ff332
    - light.yeelight_rgb_7c49eb1028fd
    data:
        brightness: 255
        kelvin: 2700
        
    input_select:
      yeecolors:
        name: Color mode
        options:
          - dim    
          - bright
- name: dim
  entities:
      light.yeelight_rgb_7c49eb0ff332:
        state: on
        transition: 2
        brightness_pct: 10
        kelvin: 5000
        
- name: bright
  entities:
      light.yeelight_rgb_7c49eb0ff332:
        state: on
        transition: 2
        brightness_pct: 100
        kelvin: 5000
        
  ##Cycle yeelight colors
- alias: Change light scenes
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001d8ea53
      click_type: double
  action:
    service: input_select.select_next
    data:
      entity_id: input_select.yeecolors

Try this

Put this into your input_select

input_select:
  yeecolours:
    name: Color Mode
    options:
      - dim
      - bright
    initial: bright

this in your scene

scene:
  - name: Dim
    entities:
      light.yeelight_rgb_7c49eb0ff332:
          state: on
          transition: 2
          brightness_pct: 10
          kelvin: 5000
		  
  - name: Bright
    entities:
      light.yeelight_rgb_7c49eb0ff332:
          state: on
          transition: 2
          brightness_pct: 100
          kelvin: 5000

This is your automations you will need to get your scene id from the states tab in HA and insert below where I have scene.yourscenehere

automation:
  - alias: Yeelight Dim
    trigger:
    - platform: state
      entity_id: input_select.yeecolors
      to: dim
    action:
    - service: scene.yoursceneidhere
	
  - alias: Yeelight Bright
    trigger:
    - platform: state
      entity_id: input_select.yeecolors
      to: bright
    action:
    - service: scene.yoursceneidhere
	
  - alias: Change light scenes
    trigger:
    - platform: event
      event_type: click
      event_data:
        click_type: double
        entity_id: binary_sensor.switch_158d0001d8ea53
    action:
    - service: input_select.select_next
      data: 
        entity_id: input_select.yeecolors
2 Likes

thanks for your time. Im new to home automation. Check config button gives error

Invalid config for [automation]: [input_select] is an invalid option for [automation]. Check: automation-&gt;action-&gt;0-&gt;input_select. (See /config/configuration.yaml, line 67). Please check the docs at https://home-assistant.io/components/automation/

can you explain which scene id from the states tab should i enter? Thank you

So you need to follow the steps I have provided above, the input_select does not go in the automation section.

Step One - Add input_select into your configuration.yaml file exactly as above.

Step Two - Add your scenes to your scene.yaml or configuration.yaml file depending on how you handle scenes, then reboot HA this will add the input_select & scenes and make them available for automations.

Step Three - Add automations to your automation.yaml and reboot.

Once HA Comes back up you can call your automations from the service button to ensure they work.

1 Like

thank you, now i understand hassio structure much better.
Still struggling with this. Light doesnt respond to double click if input select and scenes are not connected. But from webui i can activate scenes. Where is my mistake?

input_select:
  yeecolours:
    name: Color Mode
    options:
      - dim
      - bright
    initial: bright
    icon: mdi:palette

scene:
  - name: dim
    entities:
      light.yeelight_rgb_7c49eb0ff332:
          state: on
          transition: 1
          brightness_pct: 10
          kelvin: 5000

  - name: bright
    entities:
      light.yeelight_rgb_7c49eb0ff332:
          state: on
          transition: 1
          brightness_pct: 100
          kelvin: 5000

automations.yaml

  - alias: Yeelight Dim
    trigger:
    - platform: state
      entity_id: input_select.yeecolours
      to: dim
    action:
    - service: scene.dim

  - alias: Yeelight Bright
    trigger:
    - platform: state
      entity_id: input_select.yeecolours
      to: bright
    action:
    - service: scene.bright

  - alias: Change light scenes
    trigger:
    - platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d0001d8ea53
        click_type: double
    action:
    - service: input_select.select_next
      data: 
        entity_id: input_select.yeecolours
1 Like

Thanks a lot! At last i got it working. Made some changes to automations.yaml

  - alias: Yeelight Dim
    trigger:
      platform: state
      entity_id: input_select.yeecolours
      to: dim
        action:
          service: scene.turn_on
          entity_id: scene.dim

  - alias: Yeelight Bright
    trigger:
      platform: state
      entity_id: input_select.yeecolours
      to: bright
    action:
      service: scene.turn_on
      entity_id: scene.bright

  - alias: Change light scenes
    trigger:
      platform: event
      event_type: click
      event_data:
        entity_id: binary_sensor.switch_158d0001d8ea53
        click_type: double
    action:
      service: input_select.select_next
      data: 
        entity_id: input_select.yeecolours
1 Like

Sorry didn’t see this until you had fixed it well done.

found good solution to reduce amount of code if you have many scenes.
Instead of commented block i used data_template. Works. Cheers

#   - alias: Yeelight Dim
#     trigger:
#       platform: state
#       entity_id: input_select.yeecolours
#       to: dim
#     action:
#       service: scene.turn_on
#       entity_id: scene.dim

#   - alias: Yeelight Bright
#     trigger:
#       platform: state
#       entity_id: input_select.yeecolours
#       to: bright
#     action:
#       service: scene.turn_on
#       entity_id: scene.bright

  - alias: Yeelight scenes
    trigger:
      platform: state
      entity_id: input_select.yeecolours
    action:
      service: scene.turn_on
      data_template:
        entity_id: scene.{{trigger.to_state.state}}
3 Likes

If you have spaces in your input_selects and scenes add these jinja2 filters | replace(" ","_") | lower

So like this:

- alias: Living Room Scenes
  trigger:
    platform: state
    entity_id: input_select.living_room_scenes
  action:
    - service: scene.turn_on
      data_template:
        entity_id: scene.{{trigger.to_state.state | replace(" ","_") | lower}}
3 Likes

I would love to use this solution for cycling through scenes (and playlist-entries) within Node-Red. Has anyone tried this perhaps? Will be thankful for any hints.

I’m using this:

in configuration.yaml

input_select:
   huiskamer_scenes:
     name: Huiskamer Scenes
     options:
       - uit (huiskamer)
       - soft achter (huiskamer)
       - soft voor en achter (huiskamer)
       - soft voor (huiskamer)
       - fel (huiskamer)
     initial: uit (huiskamer)
     icon: mdi:lightbulb-on-outline

and in automations.yaml

- id: '1583839323446'
  alias: zb switch A 2 huiskamer
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id:  zb_switch_a
  action:
  - service: input_select.select_option
    entity_id: input_select.huiskamer_scenes
    data_template:
      option: >
        {% set options = state_attr('input_select.huiskamer_scenes','options') -%}
        {% set selection = options.index(states('input_select.huiskamer_scenes')) -%}
        {% if trigger.event.data.event == 1002 -%}
          {% if (selection + 1) >= options | length -%}
            {{ options[1] }}
          {% else -%}
            {{ options[selection + 1] }}
          {% endif %}
        {% else %}
          {% if trigger.event.data.event == 2002 -%}
            {{ options[0] }}
          {% endif -%}
        {% endif -%}
  - service: scene.turn_on
    data_template:
      transition: 1
      entity_id: scene.{{states('input_select.huiskamer_scenes') | replace(" ","_") | replace("(","") | replace(")","") | lower}}

The options: script cycles through all options pressing the on (I) button, except for option 0 (off), which is only selected when the off (o) zigbee button is pressed.

I’m using the IKEA tradfri with deconz-II


https://phoscon.de/en/conbee2
3 Likes

To teach myself the new scripting features in 0.113, I modified your automation to use the new choose feature. It is useful when the action must choose between different services and especially when the services have different options. Although it can be done entirely with templates, the result is a bit messy; the choose feature makes it neater.

Here’s how it works:

  • If the event is 1002, it calls select_next.
  • If the event is 2002, it sets the input_select to its first option.
  • Finally, the action calls scene.turn_on and computes the scene’s name. I shortened the template slightly by using regex_replace to remove ( and ) characters.
- id: '1583839323446'
  alias: zb switch A 2 huiskamer
  initial_state: 'on'
  trigger:
    platform: event
    event_type: deconz_event
    event_data:
      id:  zb_switch_a
  action:
    - choose:
        - conditions:
            - condition: template
              value_template: "{{trigger.event.data.event == 1002}}"
          sequence:
            service: input_select.select_next
            entity_id: input_select.huiskamer_scenes
        - conditions:
            - condition: template
              value_template: "{{trigger.event.data.event == 2002}}"
          sequence:
            service: input_select.select_option
            data_template:
              entity_id: input_select.huiskamer_scenes
              option: "{{state_attr('input_select.huiskamer_scenes','options')[0]}}"
    - service: scene.turn_on
      data_template:
        transition: 1
        entity_id: >
          scene.{{states("input_select.huiskamer_scenes").replace(" ","_") | regex_replace(find="[\(\)]", replace="") | lower }}
4 Likes

Wow that looks lots better :grinning:
Only question I have is, in my earlier script it skipped option 0 as this is the Off option, which I do not want to cycle through.
How does your script prevent that ?

It doesn’t. All it was meant to do was serve as a demonstration of the use of choose to call two different services (select_next and select_option). If you need to skip option 0 then you’re better off using your original automation that calls just one service and uses a data_template.

1 Like

Clear, and yes I would use choose when possible, thanks for the heads up

I have used Taras code for cycling scenes in my house for some time now, and as far as I can see the best alternative I can find, but there are some issues:
My biggest problem is it looks like the automation has taken total control over my switches. I try to use the dim up and dim down buttons in two different automations to dim up and down the light. When I press one of those buttons, I see a short flicker in the light before it’s back to what was set in the scene. When I look in the core log I get these messages (AUR301_06: Dim down automation, AUR301_01: Cycle scene automation):

AUR301_06: Running automation actions
AUR301_06: Executing step device automation
AUR301_01: Running automation actions
AUR301_01: Executing step call service
AUR301_01: Running automation actions
AUR301_01: Executing step call service

So it looks like AUR301_01 is triggered even tho the dim up and dim down buttons are not mapped in that automation.

A similar thing happens when switching my house to night mode: I have an automation for long press off on all my dimmer switches that calls the night mode settings. When I long press the off button my whole house goes into night mode, except light in the room where I press the button. In this room light dims shortly before going back to the previous scene.

Another problem I have is after night mode has been triggered it looks like the first cycle scene press I do in a room recalls my last used scene or the next one in the list, not the first one.