Update scene data on the fly

Hi all,

I’m fairly new to HA, migrating over from openHAB. I’m having some trouble creating some of my smart home behaviors.

I have an rgb led strips that I want to have some presets I can change through but also to update it on the fly form the Lovelace UI.

I defined my light in configuration.yaml and managed to define two scenes like so:

# snippet from scenes.yaml
- name: sink_lights_day_mode
  icon: "hass:weather-sunny"
  entities:
    light.sink_lights:
      state: "on"
      rgb_color: [255,255,178]
      brightness: 255
- name: sink_lights_night_mode
  icon: "hass:weather-night"
  entities:
    light.sink_lights:
      state: "on"
      rgb_color: [255,36,0]
      brightness: 223

In order to cycle between them I than defined an input boolean with the following automation:

#snippet from automations.yaml
- alias: "Sink Lights Day Mode"
  trigger:
    - platform: state
      entity_id: input_boolean.sink_lights_toogle
      to: "on"
  action:
    - service: scene.turn_on
      target:
        entity_id: scene.sink_lights_day_mode

- alias: "Sink Lights Night Mode"
  trigger:
    - platform: state
      entity_id: input_boolean.sink_lights_toogle
      to: "off"
  action:
    - service: scene.turn_on
      target:
        entity_id: scene.sink_lights_night_mode

So far so good- I have a switch in Lovelace that I can switch on to turn the light to “Day” mode and off for “Night”.
But what I can’t seems to do is dynamically change those RGB and Brightness values for the scene. What I would like to do is have another button that I can press and set whatever color that is on right now as one of those scenes.

I tried to create a new scene on the fly with this automation:

#snippet from automations.yaml
- alias: "Update Sink Light Day Scene"
  trigger:
    - platform: state
      entity_id: input_boolean.test_switch
      to: "on"
  action:
    - service: scene.create
      data_template:
        scene_id: sink_lights_day_mode_runtime
        entities:
          light.sink_lights:
            state: "on"
            rgb_color: "{{state_attr('sink_lights','rgb_color')}}"
            brightness: "{{state_attr('sink_lights','brightness')}}"

And then activate it with another input boolean and this automation:

#snippet from automations.yaml
- alias: "Sink Lights Runtime Scene"
  trigger:
    - platform: state
      entity_id: input_boolean.runtime_scene_switch
      to: "on"
  action:
    - service: scene.turn_on
      target:
        entity_id: scene.sink_lights_day_mode_runtime

I get the following error:

2021-05-03 13:18:05 ERROR (MainThread) [homeassistant.components.automation.sink_lights_runtime_scene] Sink Lights Runtime Scene: Error executing script. Invalid data for call_service at pos 1: expected int for dictionary value @ data['brightness']
2021-05-03 13:18:05 ERROR (MainThread) [homeassistant.components.automation.sink_lights_runtime_scene] Error while executing automation automation.sink_lights_runtime_scene: expected int for dictionary value @ data['brightness']

Now from what I could understand in this thread it shouldn’t happen but I also tried with |int filter for the brightness value and it didn’t work as well.
I got the following error:

2021-05-03 13:22:40 ERROR (MainThread) [homeassistant.components.automation.sink_lights_runtime_scene] Sink Lights Runtime Scene: Error executing script. Invalid data for call_service at pos 1: None for dictionary value @ data['rgb_color']
2021-05-03 13:22:40 ERROR (MainThread) [homeassistant.components.automation.sink_lights_runtime_scene] Error while executing automation automation.sink_lights_runtime_scene: None for dictionary value @ data['rgb_color']

So my questions are:

  1. Is this even the right approach? Like I said I’m new to HA and have a feeling I’m going at it the wrong way.
    In OH what I did was to store the relevant color configuration to an item (like a global variable and that is persisted through reboots) and when I wanted to update it I just overwrite it with the current color configuration.
    What would be the best way to do this in HA?
  2. In case this is the right approach, what am I doing wrong here?
    This YAML syntax is also new for me so I’m sure I messed up all sot of things there…

Thank you all in advance…

OK, so I took one step forward.
I changed the scene creation automation to use snapshot_entities and now it looks like this:

- alias: "Update Sink Light Day Scene"
  trigger:
    - platform: state
      entity_id: input_boolean.test_switch
      to: "on"
  action:
    - service: scene.create
      data_template:
        scene_id: sink_lights_day_mode_runtime
        snapshot_entities:
          light.sink_lights:

Now I can create a new scene and activate it.

But- it doesn’t survive a reboot. I kinda guessed it would happen but I don’t know what’s the solution.

Anybody share my use case and can share her/his solution?

Thank you.

I believe the error message in the first case is a result of attempting to use a template where it’s not permitted. When setting an entity’s options for a scene, it expects actual values and not templates to compute the values.

As you have already discovered, the snapshot_entities option permits creating a scene where the entities are defined with their current values. The limitation is that the generated scene is temporary and cannot survive a restart.

Although scenes are handy, they have limitations in order to keep them simple to use. If you want complete flexibility and control then you should consider using a script.

@123 thank you for the answer. :slightly_smiling_face:

I read up on scripts but I can’t figure out how should go about it.
How would I store the current configuration of an entity to use it later, and make sure it survives a restart?
I thought about variables but it looks like they’re not even available outside the script.

Thanks.