Cycle through scenes with a button?

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.

It took some effort to find this post, but the reward is fine: just what I was looking for: scrolling through a series of scenes with one button. Clear explanation and great working example! Thanks!

1 Like

I solved this with two automations and one input_select.

The input_select's values are the scene IDs (without scene. because I don’t like repeating myself).

The first automation handles changing the input_select's value when I click the button:

alias: Living Room Scene Rotate
description: ''
trigger:
  - platform: device
    domain: mqtt
    device_id: b7dd...
    type: click
    subtype: 'on'
    discovery_id: 0x000d... click_on
condition: []
action:
  - service: input_select.select_next
    target:
      entity_id: input_select.living_room_scenes
mode: single

The other automation is activated when that input_select changes and activates the selected scene with a template that reads the input_select's value.

alias: Living Room Scene Activate
description: ''
trigger:
  - platform: state
    entity_id: input_select.living_room_scenes
condition: []
action:
  - service: scene.turn_on
    target:
      entity_id: scene.{{ states('input_select.living_room_scenes') }}
mode: single
6 Likes

Im struggling hard. With my HUE Button, this works but with the Ikea Button I cannot get those scene working:

    action_button_down_short:
      - service: input_select.select_next
        data:
          cycle: true
        target:
          entity_id: input_select.scene_buro
      - service: scene.turn_on
        target:
          entity_id: "{{states('input_select.scene_buro')}}"

So the Problem is. It switches the scenes, but it’s unable to apply the scene then.
What am I doing wrong? Same script, works on the Hue Dimmer Switch…

Thanks!!! Cycling works great.

Hi,

I use this script to cycle through scenes it’s working but not always.
It changes the input select perfect but is only randomly actually changes the scene. On the dashboard I see the scene changing it’s just not applied to the lights sometimes. This usually happens when cycle a bit faster.
I have tried different modes (single, restart, queued) but that didn’t change anything.

Any help (also how to debug) would be much appreciated. I am pretty new to HASS

- service: input_select.select_next
  data:
    cycle: true
  target:
    entity_id: input_select.keuken_scenes
- service: scene.turn_on
  target:
    entity_id: >-
      {{'scene.' + states('input_select.keuken_scenes').replace("
      ","_").lower()}}

BTW I use Zigbee2MQTT

An update on my own post.

I’ve tried to solve it via MQTT this works a lot better but still not 100%.

alias: Test Scene selector (MQTT)
sequence:
  - service: input_select.select_next
    data:
      cycle: true
    enabled: true
    target:
      entity_id: input_select.keuken_scene_mqtt
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/Keuken Lampen/set
      payload_template: "{\"scene_recall\": {{ states('input_select.keuken_scene_mqtt') }} }"
      qos: "0"
mode: queued
max: 12

BTW to make it work fill the input_select.keuken_scene_mqtt with the number of the scenes (scene id from Zigbee2MQTT) something like 0, 1, 2, 3

Now how to take account light color at the start?

Ie. I have light toggle and then warm cold pink, it starts on pink, first scene in cycle happens to be pink so it does not change.

Any super nice way to do this?


I do get why this does not work as expected with turn off button action, but it does change the scene (color temp.) only after I press on the light switch again, triggering light toggle.
When switching scenes from HA UI it does switch color temp.

I do get “triggered” when watching scene dialog when I hold the button for about 4 sec. so that seems all ok.


6 years old thread now :smiley:

Using the following Blueprint made it easy for me.
:next_track_button: Simple Scene Toggle - Blueprints Exchange - Home Assistant Community (home-assistant.io)

How I’ve done it today:

  1. Made some Scenes in the GUI.
  2. Made a Dropdown Helper in the GUI – the Options were the scenes names (names! not IDs!)
  3. Made an Automation that activates the matching Scene when Dropdown option is selected (notice slugifying of the scene names!):
      alias: TV scenes activation
      description: ""
      trigger:
        - platform: state
          entity_id:
            - input_select.tv_scenes
      condition: []
      action:
        - service: scene.turn_on
          metadata: {}
          target:
            entity_id: "{{ 'scene.' + slugify(states.input_select.tv_scenes.state) }}"
      mode: single
    
  4. Finally made an Automation that cycles the Dropdown’s options upon a (zigbee) button click:
        alias: Button switching TV scenes
        description: ""
        trigger:
          - device_id: <redacted>
            domain: zha
            platform: device
            type: remote_button_short_press
            subtype: button_1
        condition: []
        action:
          - service: input_select.select_next
            target:
              entity_id:
                - input_select.tv_scenes
            data:
              cycle: true
        mode: single
    
1 Like

i followed a different approach, storing the number how often on was pressed in a persistent variable. The blueprint is for homematic button devices, but will work with some adoption for other devices as well.

blueprint:
  name: HM Push Button Light on/off/dim or next scene 
  description: >-
    A script to connect a push button device with a light. The script can switch the light on and off on short keypress
    and dim up and down on long keypress. If the light is on and the on button is pressed another time, the automation 
    switch throuh different scenes.
    It require the variable saver addon from here: https://github.com/PiotrMachowski/Home-Assistant-custom-components-Saver
  domain: automation

  input:
    pushbtn_dev:
      name: HM Push Button Device
      description: Please select a Button device of your Homematic integration.
      selector:
        device:
          integration: homematicip_local
    on_button:
      name: On Button
      description: "Align button channel to on function"
      selector:
        number:
          min: 0
          max: 12
    off_button:
      name: Off Button
      description: "Align button channel to off function"
      selector:
        number:
          min: 0
          max: 12
    light_dev:
      name: Light
      description: "Light device for on/off and dim"
      default: []
      selector:
        entity:
          multiple: true
          domain: light
    off_scene:
      name: Off Button Scene     
      description: "Select a script or scene which shall be executed when the off button is two times pressed"
      default: []
      selector:
        entity:
          multiple: true
          filter:
            domain: 
              - scene
              - script              
    scene_1:
      name: Scene 1    
      description: "Select a scene"
      default: []
      selector:
        entity:
          multiple: true
          filter:
            domain: 
              - scene
              - script              
    scene_2:
      name: Scene 2    
      description: "Select a scene"
      default: []
      selector:
        entity:
          multiple: true
          filter:
            domain: 
              - scene
              - script              
    scene_3:
      name: Scene 3    
      description: "Select a scene"
      default: []
      selector:
        entity:
          multiple: true
          filter:
            domain: 
              - scene
              - script              
    scene_4:
      name: Scene 4    
      description: "Select a scene"
      default: []
      selector:
        entity:
          multiple: true
          filter:
            domain: 
              - scene
              - script              


trigger:
- platform: event
  event_type: homematic.keypress
  event_data:
    device_id: !input pushbtn_dev


variables:
  light_dev:   !input 'light_dev'
  pushbtn_dev: !input 'pushbtn_dev'
  on_button:   !input 'on_button'
  off_button:  !input 'off_button'
  
  #                 0                  1                  2                  3                 4                 5                 6  
  target: [!input 'off_scene',!input 'light_dev',!input 'light_dev',!input 'scene_1', !input 'scene_2', !input 'scene_3', !input 'scene_4']

  # adjust and combine trigger, to have one point to adopt other switches
  event_key: >
    {% if trigger.event.data.type  == 'press_short' and trigger.event.data.subtype  == on_button %} 
      short_on 
    {% elif trigger.event.data.type  == 'press_short' and trigger.event.data.subtype  == off_button %} 
      short_off 
    {% elif trigger.event.data.type  == 'press_long' or trigger.event.data.type  == 'press_cont' and trigger.event.data.subtype  == on_button %} 
      long_on 
    {% elif trigger.event.data.type  == 'press_long' or trigger.event.data.type  == 'press_cont' and trigger.event.data.subtype  == off_button %} 
      long_off
    {% endif %} 

  # get counter from memory, decide based on trigger and subtrigger what to do
  # 0: short_off, coming from 1 and if target[0] is defined
  # 1: short_off but only if we switched not to 0
  #
  # 2: short_on while coming from 0 or 1, or coming from 2 and target[3] is not defined
  # 3: short_on while coming from 2 and target[3] is defined
  # 4: short_on while coming from 3 and target[4] is defined
  # 5: short_on while coming from 4 and target[5] is defined
  # 6: short_on while coming from 5 and target[6] is defined
  #  : next short_on returns to 3
  keycnt: >
    {% set t_cnt = saver_variable(trigger.event.data.device_id) or 0 %}
    {% if event_key == 'short_on' %} 
      {% if   t_cnt == '0' or t_cnt == '1' %}   {% set x_cnt = 2 %} 
      {% elif t_cnt == '2' and not target[3] %} {% set x_cnt = 2 %} 
      {% elif t_cnt == '2' and target[3] %}     {% set x_cnt = 3 %} 
      {% elif t_cnt == '3' and target[4] %}     {% set x_cnt = 4 %} 
      {% elif t_cnt == '4' and target[5] %}     {% set x_cnt = 5 %} 
      {% elif t_cnt == '5' and target[6] %}     {% set x_cnt = 6 %} 
      {% else %}                                {% set x_cnt = 3 %} 
      {% endif %} 

    {% elif event_key == 'short_off' %} 
      {% if t_cnt == '1' and target[0] %}       {% set x_cnt = 0 %}
      {% else %}                                {% set x_cnt = 1 %} 
      {% endif %} 
 
    {% elif event_key == 'long_on' or event_key == 'long_off' %} 
      {% set x_cnt = 2 %} 

    {% endif %} 
    {{x_cnt}}


  # find the correct service, depending on the service domain
  target_service: >
    {% if not target[keycnt] %}
      none    
    {% elif states[target[keycnt][0]].domain == 'scene' %} 
      scene.turn_on 
    {% elif states[target[keycnt][0]].domain == 'script' %} 
      script.turn_on
    {% elif states[target[keycnt][0]].domain == 'light' and keycnt == 1  %} 
      light.turn_off
    {% elif states[target[keycnt][0]].domain == 'light' and keycnt == 2 %} 
      light.turn_on
    {% endif %} 
  
  
  # assign entity_id based on state_cntr
  target_details:  >
    {% if states[target[keycnt][0]].domain == 'script' %} 
      {{ {"entity_id": target[keycnt] } }}
    {% else %}   
      {{ {"entity_id": target[keycnt], "transition": 0.5 } }}
    {% endif %} 
  

  
condition: []

action:
- choose:
  # a short press is switching the light on or a scene select if it was a repeated on press
  - conditions: '{{ event_key  == ''short_on'' or event_key  == ''short_off'' and target_service != ''none'' }}'
    sequence:  
    - service: '{{ target_service }}'
      data: '{{ target_details }}'

  # press the first button continous means dim the light up
  - conditions: '{{ event_key  == ''long_on'' and target_service != ''none'' }}'
    sequence: 
    - service: '{{ target_service }}'
      data:
        entity_id: '{{ target[keycnt] }}'
        transition: 0.2
        brightness_step: 20    

  # press the second button continous means dim the light down
  - conditions: '{{ event_key  == ''long_off'' and target_service != ''none'' }}'
    sequence: 
    - service: '{{ target_service }}'
      data:
        entity_id: '{{ target[keycnt] }}'
        transition: 0.2
        brightness_step: >
          {% set brightness = state_attr(light_dev[0], 'brightness') or 0 %}  
          {% if brightness < 3 %} {% set step = 0 %}
          {% elif brightness < 22 %} {% set step = -(brightness - 2) %}
          {% else %} {% set step = -20 %}
          {% endif %}        
          {{ step }}

# save the value in a secure store for next time usage
- service: saver.set_variable
  data:
    name: '{{ trigger.event.data.device_id }}'
    value: '{{ keycnt }}'

mode: queued
max: 5

thank you so much, this was so easy and did exactly what i wanted :slight_smile: