Flash a light with a specific color

Hi all,

I am fairly new to home assistant and still learning the way to automate my smart devices. I realized there are a lot of possibilities which makes it quite hard some time to get things the way I want it. So far try and error did the trick for me but here’s something I can’t figure it out.

I simply create two scripts. one does flash a light 10 times in red for alarming me. Another one switches on the same light for 10s in green color.

'1588765873091':
  alias: Flash Hue Go - red 10x
  sequence:
  - data:
      brightness: '255'
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
      hs_color:
      - 0
      - 100
      transition: '0'
    entity_id: light.hue_go
    service: light.turn_on
  - data: {}
    entity_id: light.hue_go
    service: light.turn_off
  - delay: '2'


'1588766986243':
  alias: Hue Go - green 10s
  sequence:
  - data:
      brightness: '255'
      hs_color:
      - 120
      - 100
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '10'
  - data: {}
    entity_id: light.hue_go

First script for flashing the light does work, however it doesn’t change the color to red (hs_color: [0,100]). The second script works as well and here it actually changes the color to green (hs_color: [120,100]) independently from the color it had before. Exactly like it is supposed to be.

So what’s the difference between both scripts, so that the flashing script doesn’t change the color to red. Is

flash: short

maybe incompatible with color changes?

thanks for any hints how to solve this?

Best
Pete

Having just recently acquired a few Hue color lights, the problem you described intrigued me.

I performed a few simple experiments using Developer Tools > Services. I tried different combinations for hs_color, and then using color_name, but the result was always the same: the light flashes (once) using whatever color it was originally set to use and not the one specified in the service call.

Screenshot from 2020-05-06 10-21-35

This implies that first service call should set the color and subsequent ones request flash: short.

great. thx. that did the trick.

the last service call should probably be the color being used before the script went off. Is there any way to save and recall this last state?

BR

I can’t think of an elegant way to do that. The light’s current color (before you change it with the script) is not stored anywhere. Therefore the last service call in your script won’t know what color to use.

The light’s hs_color attribute is not available when the light is off. So your script can’t get this initial value unless the light is already on.

Would this work ? Not tried it myself, apparently you can save a state and restore it

Creating scenes on the fly

EDIT, no not quite, only restores state and not colour

Correct. You would want the scene to store and restore attributes and it doesn’t support that.

well, for now I think I just figure out the color code being used normally and call it as a service at the end of the blinking script.

Maybe this ?

No. That python_script has to work with the same limitations. A light’s attributes, like hs_color, are only available when the light is on. The python_script saves the light’s attributes only if it is on. Here’s the relevant line:

if entity_id.startswith('light.') and cur_state.state == 'on':

from here:

Peter’s requirement is to get the light’s initial color prior to the first flash operation while it is still off.

One workaround would be to initially turn on the light, pause a moment, then read and save its hs_color attribute (to an input_text) and then proceed to flash the light. At the end of the flashing process, the value in the input_text can be used to set the light to its initial color, pause, then turn it off.

1 Like

thanks for the answers. I started to set all light scenes via home assistant. This way it is actually not needed to save latest color state.

for the sake of completeness, here the working code for flashing the light

  alias: Flash Hue Go - red 10x
  sequence:
  - data:
      brightness: '255'
      hs_color:
      - 0
      - 100
    entity_id: light.hue_go
    service: light.turn_on
  - data: {}
    entity_id: light.hue_go
    service: light.turn_off
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on
  - delay: '1'
  - data:
      flash: short
    entity_id: light.hue_go
    service: light.turn_on

Best

1 Like

Effectively, you’ve changed the requirements because a scene doesn’t know anything about a light’s previous hs_color.

For example, let’s say the light is turned on and set to blue, then turned off. If you run a scene now involving that light, the scene doesn’t know the light’s color was blue so it can’t restore it to blue.

alright let me explain this more detailed.

the situation was the following:

  • the flash of this particular light goes off with a color red.
  • later on, I switch on a scene via philips hue hub directly. This way, the scene uses the latest color of the involved lights. In this case the light being used for flashing turns red instead of the color set via hue hub.
  • The workaround I use is that I don’t use the scenes from hue hub anymore but instead created scenes within home assistant which basically do the same but have dedicated color for each light in this scene. So it actually overwrites the color red by the color being set up in the HA scene.

Point is, if I use scenes in HA with defined colors for each bulb, I don’t need to save the color of the lights before the flash goes off.

?

A Hue scene can specify each light’s color.

  • The advantage of a Hue scene is that Home Assistant sends one command to activate the Hue scene and all lights within the scene change to the desired state (and color, etc) simultaneously.

  • A Home Assistant scene sends commands for each and every light defined in the scene.

FWIW, I use Hue scenes to set groups of lights to specify colors and brightness and that takes precedence over whatever previous color and brightness they may have had.