Automation to trigger different scenes (like an array)

Hi all,

I was wondering whether it was possible to adjust the below code to switch between different scenes, maybe in a specific order, but I just don’t know how to accomplish that.

My thoughts came across an automation that triggers some different colors in the kids room everytime the Cube is flipped.

- alias: Xiaomi Cube - Change Scene [Flip 90]
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d047abfeeo1
      action_type: flip90     
  action:
      service: light.turn_on
      entity_id: light.children_room1_spots
      data_template:
         brightness_pct: 30
         rgb_color: >
           {% set colors = [[255,0,0], [255,255,0], [0,0,255], [[0,0,255]], [128,0,128]] %}
           {{ colors|random}}

How can I flip the cube and then Set scene 1 → Cube flipped again → Set scene 2 → Cube flipped again → Set scene 3?

Is that even possible?

Let’s say you have flipped the cube and it sets scene1.

You wait 1 hour and then flip the cube again.

Should it set scene 2 or simply set scene 1 again?

In other words, should it keep track of how many flips have occurred, regardless of how much time has passed between flips, or there is some sort of time duration you have in mind after which it resets the number of flips back to zero?

1 Like

Ah yes - good thoughts and a good question.
I was actually thinking that we should do some measuring with conditions, but maybe I have the answer there and then go with the “choose” option.

So if all lights are off, then do this, but if light 1 and 2 is on then do that and finally if all lights are on then do this third thing. Hmm.

That’s different behavior from what you described in the first post which simply tracks the number of flips and is independent of the state of various entities.

Once you decide how you want the cube to operate, I can help you implement it.

I haven’t decided yet, I just thought that it was a good idea :slight_smile:
My initial thought was to switch between the 3 scenes I had in mind; 1,2,3 and then start over even though xx time has passed. But it could make sense to introduce that after 1 hour it should reset.

Thanks for helping!

Create an input_select containing the names of the three scenes. Make the third option the initial option.

Create an automation with an Event Trigger that triggers whenever the cube is flipped. Its action should simply execute input_select.select_next.

Create an automation with a State Trigger that triggers whenever the input_select changes state for 5 seconds. Its action should use the input_select’s current selection to determine which scene to activate.

If you want, the two automations can be consolidated into one.

Would it be possible for you to show a quick automation how it would look like?

I explained how it would look like in my previous post; just follow the instructions I provided.

If you’re struggling with the instructions, here’s something to help you get going.

Create an input_select containing the names of the Scenes you want to use. Call the scenes whatever you want.

input_select:
  cube_scenes:
    name: Cube Scenes
    options:
    - 'first'
    - 'second'
    - 'third'
    initial: 'third'

Create the three scenes you want (change the following example to whatever you require).

scene:
  - name: first
    entities:
      light.children_room1_spots:
        state: "on"
        brightness: 200
        color_mode: "xy"
        xy_color: [0.33, 0.66]
  - name: second
    entities:
      light.children_room1_spots:
        state: "on"
        brightness: 125
  - name: third
    entities:
      light.children_room1_spots:
        state: "on"
        brightness: 125
        color_mode: "white"

Finally, create the following automation. Each time the cube is flipped, the input_select is stepped to its next option. If the input_select’s chosen option remains unchanged for 3 seconds, the automation will activate the scene specified by the input_select.

- alias: 'Xiaomi Cube - Flip Scenes'
  mode: queued
  trigger:
  - platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d047abfeeo1
      action_type: flip90
  - platform: state
    entity_id: input_select.cube_scenes
    for: '00:00:03'
  action:
  - choose:
    - conditions: "{{ trigger.platform == 'state' }}"
      sequence:
      - service: scene.turn_on
        target:
          entity_id: "scene.{{ trigger.to_state.state }}"
    default:
    - service: input_select.select_next
      target:
        entity_id: input_select.cube_scenes
1 Like

So nice of you. Exactly what I was looking for. Now I have a guideline. Will post my final automation when I have created and tested it :slight_smile:

Glad you like it.

NOTE

I used scenes because that’s what you requested in your first post. However, the example in your first post doesn’t contain a scene but uses a service call to set a random color. I don’t believe that is possible with a scene. If you want to use the cube to select different service calls or scripts (instead of scenes), I can show you how to do that as well.

The automation in my first post was just used as an example, as I thought it might be doable calling the scenes somehow.

I believe the scene thing as you’ve showed is the right path for me. I’m trying to keep the different light setting as scenes, because I can reuse them in the different rooms.

Currently I have also other automations with the cube turning different media things on, starts stream and also volume control. Nice Little ‘controller’ :blush:

Are you familiar with this blueprint?

I don’t think it handles multiple flips the way you requested but it does many other things.

1 Like

Yeah it’s amazing! Been looking at it a few times, but I would get issues remembering all the different things I would trigger :sweat_smile: