I liked the Philips Hue scene cycling. I tried various ways to do this in Home Assistant, and this Smart Home Junkie video really helped me understand:
- how to get a drop-down helper to store the current scene
- how to use an automation to handle the button clicks and state changes
- how to use
run-mode
=queued
to cope with multiple button clicks
Then I found I could reduce the effort to set up scene cycling quite a lot using a little bit of templating. So this, below, is how my scene cycling now works. It takes a bit less work to set up and is easier to alter if I later alter my scenes.
Step 1: I create the scenes for my room. Itâs important that their names all start with the same prefix, e.g. âBedroom 1 brightâ and âBedroom 1 midâ scenes for my bedroom 1.
Step 2: I create a drop-down helper listing all the scene names. These names must match the bit after the prefix, so for âBedroom 1 brightâ the option is just âbrightâ.
Step 3: I add my drop-down to a dashboard with a custom:mushroom-select-card
so I can use it directly if I need to.
Step 4: I create the automation that will handle my button. It must have run-mode
= queued
. Each trigger is given an ID and I use a choose
action.
One of the triggers will be a state change on the drop-down I created and its option is the one that calls the service to turn on the scene. This is done with a template like this
service: scene.turn_on
metadata:
transition: 1
target:
entity_id: "{{ 'scene.bedroom1_' ~ states('input_select.bedroom_1_scenes') }}"
This shows why the consistent name prefix is important - it concatenates the string 'scene.bedroom1_'
with the name of the selected scene.
Hereâs an example of the whole thing:
alias: bedroom 4 button
description: ""
trigger:
- device_id: 9c5bb1b88d0fc3a51100188be353d207
domain: zha
platform: device
type: remote_button_short_press
subtype: remote_button_short_press
id: single press
- platform: state
entity_id:
- input_select.bedroom_4_scenes
id: selected
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- single press
sequence:
- service: input_select.select_next
data:
cycle: true
target:
entity_id: input_select.bedroom_4_scenes
- conditions:
- condition: trigger
id:
- selected
sequence:
- service: scene.turn_on
metadata:
transition: 1
target:
entity_id: >-
{{ 'scene.bedroom4_' ~ states('input_select.bedroom_4_scenes')
}}
mode: queued
max: 10
Notice that the first option doesnât select a scene, it only alters the current drop-down helper value. So a button press triggers the automation a first time and this in turn causes the automation to be triggered again, which turns on the selected scene.
As a matter of preference, the state change trigger and action could instead be moved out into a different automation; I decided to keep both parts together in one automation, so Iâd find them easily later. The queued
run-mode is what makes this possible and cleanly handles multiple button clicks.