How to save RGB attributes in an automation?

Hi there,

since I’m new to HA and also Jinja2 my thoughts on the following issue are based on how I would realise it with PHP.

I’d like to built an automation that controls an rgb bulb that reminds me on an open window after several timers have passed. Due to the point that I don’t wanna set specific values for rgb and brightness these attributes are variables.

So the logic behind my intended automation should be:

  1. retrieve current state and attributes of the lamp and store these information in an array to reset the lamp later on:
{{states('light.bulb‘)}}
{{state_attr('light.bulb‘,’brightness')}}
{{state_attr('light.lbulb‘,’rgb_color')}}

$initial_state = array()
$initial_state['state_on_off'] = {{states('light.bulb‘)}}
$initial_state['rgb'] = {{state_attr('light.lbulb‘,’rgb_color')}}
$initial_state['brightness'] = {{states('light.bulb‘)}}
  1. some automation stuff like:
## trigger ###############################
trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: BA
#######################################

## actions ###############################
action:
  - if:
      - condition: trigger
        id: BA
    then:
      - service: light.turn_on
        data:
          rgb_color:
	## set fixed values
            - 67
            - 180
            - 252
          brightness_pct: 20
        target:
          entity_id: light.bulb
        alias: 1st stage 
…
some more actions and now set back to initial attributes
…
action:
  - if:
      - condition: trigger
        id: BA
    then:
      - service: 
## depending from the initial state #############
if ($initial_state['state_on_off'] == ’on’){
	light.turn_on
} else {
	light.turn_off
}
#######################################
        data:
          rgb_color:
## depending from the initial color #############
$initial_state['rgb']
#######################################
          brightness_pct: 
## depending from the initial color #############
$initial_state['brightness'] 
#######################################
        target:
          entity_id: light.bulb
        alias: 3rd stage 

Thanks in advance.

Why don’t you use scene for that?

2 Likes

Thanks for the hint but as far as I understand scenes create a snapshot for a given status. In my case I would need to create a scene of that actual moment within an automation instead of calling a scene that has fixed values.

To be more precise here is some explanation of the logic:

  1. the bulb can be either on or off → when its on than it can have a color and brightness setting whatever it was set before depending from the mood and playing around - so this is not fixed.

  2. in a first step the window is open for more than 10 minutes and the color and brightness of the bulb changes to blue and decreases brightness - all some fixed settings.

  3. in the second step there is a condition for the bulb either the window will be closed and the state/settings will be reset to the initial one which I don’t really know → as mentioned in no.1
    or
    after further 10mins of an open window the color will change to red and again increase the brightness to some other fixed settings.

  4. As long as the window is still open the bulb remains with the red light and will be reset to no.1 after the window has been closed.

As far as I understood a scene has to be fixed, or am I wrong and there is another way to go for this?

That’s what this does. You can create an ad-hoc scene at that point in time. Here is a script that does the same thing (it creates an adhoc scene called front_snapshot, makes my porch light bright and then starts a timer):

script:
  porch_light_bright:
    alias: "Porch light bright-white for 3 mins"
    sequence:
    - service: scene.create             # save light to scene
      data:
        scene_id: front_snapshot
        snapshot_entities:
        - light.porch_colour
    - service: light.turn_on            # turn on light
      data:
        entity_id: light.porch_colour
        transition: 1
        brightness_pct: 100      
        rgb_color: [255, 255, 255]
    - service: timer.start              # start timer
      target:
        entity_id: timer.porch_light
    mode: single
    icon: hass:coach-lamp

I have an automation that calls that script when motion is detected. The same automation does the following after the timer has cleared:

automation:   
  [...]
  action:
      - if:
          - condition: trigger
            id: 'timer_finished'
        then:
          - service: scene.turn_on      # revert light to original scene
            data:
              entity_id: scene.front_snapshot

I do it this way as I call that script from other automatons as well and the timer allows the automation to not be actively waiting for a long period of time.

[edit: typos and clarity]

Well, @jchh explained in detail. :slightly_smiling_face:

@jchh Thanks for your response.

After tinkering around all night long and even trying to implement scenes I’m not really happy with the result.

The following code works almost fine except one little issue. For the case that the bulb initially is on everything is fine. Just in case that the bulb is initially off it won’t create the right scene.

In example, like explained in no.1 from my last post, the logic should be:

light: can be either on or off, random rgb code and brightness

case 1 (light is on): open window for 10mins - change rgb color to light blue and increase brightness (if window is closed reset values) - after 20mins change color to red and increase brightness (if window is closed reset values).

case 2 (light is off) : open window for 10mins - turn light on to blue and increase brightness (if window is closed reset values and turn off) - after 20mins change color to red and increase brightness (if window is closed reset values and turn off)

The following code:

alias: Reminder - bathroom
description: ""
trigger:
  - type: opened
    platform: device
    device_id: ec28fefd342148856ddbd1d7e6d81a3b
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    domain: binary_sensor
    id: BA-open
    alias: open
  - type: opened
    platform: device
    device_id: ec28fefd342148856ddbd1d7e6d81a3b
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 5
    id: BA-open-10
    alias: 10mins
  - type: opened
    platform: device
    device_id: ec28fefd342148856ddbd1d7e6d81a3b
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    domain: binary_sensor
    for:
      hours: 0
      minutes: 0
      seconds: 10
    id: BA-open-20
    alias: 20mins
  - type: not_opened
    platform: device
    device_id: ec28fefd342148856ddbd1d7e6d81a3b
    entity_id: binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2
    domain: binary_sensor
    id: BA-close
    alias: closed
condition: []
action:
  - if:
      - condition: trigger
        id: BA-open
    then:
      - service: scene.create
        data:
          scene_id: test
          snapshot_entities:
            - light.lichtkugel1
  - if:
      - condition: trigger
        id: BA-open-10
    then:
      - service: light.turn_on
        data:
          rgb_color:
            - 67
            - 180
            - 252
          brightness_pct: 20
        target:
          entity_id: light.lichtkugel1
    enabled: true
  - if:
      - condition: trigger
        id: BA-open-20
    then:
      - service: light.turn_on
        data:
          rgb_color:
            - 255
            - 9
            - 0
          brightness_pct: 30
        target:
          entity_id: light.lichtkugel1
    enabled: true
  - if:
      - condition: trigger
        id: BA-close
    then:
      - service: scene.turn_on
        data: {}
        target:
          entity_id: scene.test
    enabled: true
mode: single

works fine for the first case when the light is on. In the second case when the light should be turned off it doesn’t set the value for rgb and brightness correctly. No matter if the window is being closed within the first 20min or afterwards - after calling the initial scene when the window is being closed it turns off with the last information either blue or red. When turning the light manually back on its either blue or red instead of the initial color and brightness.

The scene.create & scene.turn_on work perfectly for me with the light off scenario. I have just rechecked it before posting this.

Question: Are you triggering after 10 & 20 secs for the purpose of testing? If so, can you change to 1 min and 2mins and try again?

Yes, that’s for testing purposes only. I just changed them to 1 and 2 mins but with no change. Still the same result.

I’m using:
Home Assistant 2023.2.5 Supervisor 2023.01.1 Operating System 9.5 Frontend 20230202.0 - latest
running with SkyConnect as Coordinator in ZHA integration. The bulb is a LIDL/Livarno E14 RGB model HG08131B Version 10/2021.

Sorry, I’m out of ideas.

No worries, I appreciate your help. Thanks anyways.