Scenes operating as a switch

I have four HUE lights (2 - strips/2 - bulbs) that are together operating in four scenes. Scene 1 is all four lit up and one color. Scene 2 is all four lit up, but a different color than Scene 1. Scene 3 is the two bulbs, same color, and lit without the strips (strips are off). Scene 4 is the same as Scene 3, but a different color for the bulbs.

I want to be able to trigger any of the scenes. In Lovelace I have them showing up in an entities card, but the card displays “execute” instead of a slider switch. Is there a way to make the scripts look and act as four regular item in an entities card?

Any help or guidance you can provide would be terrific. Thank you.

1 Like

If you just want to be able to trigger them you could add them as buttons with an action call to scene.turn on. This won’t display which one is active, as a scene entity status is always “scening” but it will activate them.

I wanted to display which one is active, so I used an input_select with the scene names, and have my buttons trigger the input_select. An automation then triggers on input_select change, sending the scene.turn_on call. in the linked image the icons are what trigger the scene change.

4 Likes

Thank you for the post @TheYeti. That is an elegant solution and I think it will probably do exactly what I want (but more simple). Would you be willing to share your code?

Sure. Note, 90% of the below is only necessary if you want to ‘cycle’ through scenes with physical buttons etc. or otherwise display which scene is active. If you don’t care about that, you can just implement the button second from the end.

First, I defined the scenes i wanted in scenes.yaml:-

# Living room scenes
- name: Living Room Day
#  icon:
  entities:
    light.paper_lamp:
      state: "on"
      brightness: 255
      color_temp: 250
    light.kitchen_table_overhead_2:
      state: "on"
      brightness: 255
      color_temp: 250
    light.living_room_overhead:
      state: "on"
      brightness: 255
      color_temp: 250

Repeat until all scenes defined.

I then set up an input_select in configuration.yaml:-

# input_select to use switch to cycle through scenes
input_select:
  living_room_scene_select:
    options:
      - scene.living_room_day
      - scene.living_room_afternoon
      - scene.living_room_evening
      - scene.living_room_night
      - scene.living_room_off

Notice the names in input select are the same as the scene name. This is important.

I then set up an automation that would trigger when the input_select changed state - i.e., when I’ve changed scene:-

- alias: 'Send Living room scene'
  trigger:
    - platform: state
      entity_id: input_select.living_room_scene_select
  condition:
    - condition: template
      value_template: "{{ trigger.to_state != trigger.from_state }}"
  action:
    service: scene.turn_on
    data_template:
      entity_id: >
        {{ states('input_select.living_room_scene_select') }}

Notice in the condition value_template it’s basically saying ‘has the input_select been changed to a new state’. If so, the action section then uses a scene.turn_on call and templates the input_select state - this is why the input_select options have to be the same as the name of the scenes).

So, putting this in a button in ui-lovelace.yaml:-

            cards:
              - type: button
                name: Day
                show_name: false
                icon: 'mdi:white-balance-sunny'
                show_state: true
                state_color: true
                tap_action:
                  action: call-service
                  service: input_select.select_option
                  service_data:
                    entity_id: input_select.living_room_scene_select
                    option: scene.living_room_day

In my example, this is the icon you would click on. It sends the command to change input_select, whcih then triggers the previously defined automation. If you don’t care about cycling through scenes or displaying which one is active you could just use the above button but change the action_service to ‘action.scene_on’ as per the HA scene action definition.

Next, the bar underneath the icon is actually a custom resource. It monitors the input_select state and changes colour based on it:-

            cards:
              - type: custom:button-card
                entity: input_select.living_room_scene_select
                aspect_ratio: 16/1
                color_type: card
                show_name: false
                triggers-update:
                  - input_select.living_room_scene_select
                styles:
                  card:
                    - background-color: >
                        [[[
                          if (states['input_select.living_room_scene_select'].state == "scene.living_room_day")
                            return "var(--paper-item-icon-active-color)";
                          return "var(--paper-card-background-color)";
                        ]]]

The important bits in that are the triggers-update entry and the background color ‘if’ condition under styles. That’s what changes the colour of it depending on input_select state.

I put this mish-mash together based on some google-fu and shameless plagarism of other’s code and suggestions, so kudos go to them.

5 Likes

Wow! That is incredible. Thank you so much for the detailed explanation.

I was trying to replicate this, but my automation won’t be triggered when pushing the button.

# Helper
input_select:
  input_select.licht_szenen:
    options:
      - scene.abendlicht_an
      - scene.alle_lichter_hell
      - scene.alle_lichter_aus
      - scene.licht_infusion

# Automation
alias: 'Lichtszene senden'
trigger:
  - platform: state
    entity_id: input_select.licht_szenen
condition:
  - condition: template
    value_template: "{{ trigger.to_state != trigger.from_state }}"
action:
  service: scene.turn_on
data_template:
  entity_id: >
    {{ states('input_select.licht_szenen') }}

# Button
type: button
tap_action:
  action: call-service
  service: input_select.select_option
  service_data:
    entity_id: input_select.licht_szenen
    option: scene.abendlicht_an
icon: mdi:weather-sunset-down
show_name: false
show_state: true

Hi @nightfever ,
I had the same issue when I try to build the same. First I thought the yaml syntax has been evolved but trigger.from_state / to_state is still valid.
However, as the automation is triggered only if the state of the input select variable is changed (I’ve tried by Developer Tools / State → set the same status again → not triggered) you can leave the condition out.
For me this is now working as expected.
Cheers, Bastian

Hi TheYeti

Fantastic work. I have been able to reproduce this at my end. The selection works fine. But the color isn’t changing for me.

How would I set an absolute color instead of “–paper-item-icon-active-color” which comes from the theme?

Hi,

The colour is set in the If statement at the bottom of the button-card custom resource.

It’s been a while since I tinkered with this, but I would check the syntax for the button-card custom resource to see what it expects for colours. You may be able to do it as an RGB tuple, so something like 255,0,40 in lieu of the “var(—paper-item…” in the If statement.