A simple single automation candle effect for Hue White Ambience (and other) bulbs

I wanted a nice “candle” effect, with changing brightness and warmth, but there didn’t appear to be much out there.

I have two Hue White Ambience bulbs either side of my bed, and this works an absolute treat. Suggestions/simplifications welcome!

- alias: Candle Mode Automation
  initial_state: on
  trigger: 
    platform: state
    entity_id: input_boolean.candle_mode
    to: 'on'
  action:
    - alias: Repeat this until the boolean is turned off
      repeat:
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: light.left_bedside
              brightness: '{{ (range(1, 25)|random) }}'
              color_temp: '{{ (range(400, 500)|random) }}'
              transition: 1
          - delay:
              milliseconds: '{{ (range(200, 700)|random) }}'
          - service: light.turn_on
            data_template:
              entity_id: light.right_bedside
              brightness: '{{ (range(1, 25)|random) }}'
              color_temp: '{{ (range(400, 500)|random) }}'
              transition: 1
          - delay:
              milliseconds: '{{ (range(200, 700)|random) }}'
        until:
          - condition: state
            entity_id: input_boolean.candle_mode
            state: 'off'

The automation is triggered by input_boolean.candle_mode being turned on, which I do from a switch in my dashboard.

After that, the sequence will repeat until the input boolean is turned off.

It should be fairly self-explanatory, but basically it turns each light on to a random (but low) brightness, then to a random (but warm) colour temperature, then waits a random time between doing it again.

I put transition: 1 because 0 was too jarring, but it’s a real shame there isn’t a 0.5 option, as 1 doesn’t qute give the “flicker” I’d like.

14 Likes

This is really cool! Thanks very much!

I might have to try this tomorrow on my 9 bulb chandelier in my dining room. Thanks for sharing.

9 bulb! I’d love to hear how that goes… the automation goes via each light in sequence, so with 9 bulbs you may find each are on for a relatively long period of time.

I wonder if there is a way of running this in parallel? 3 automations each handling 3 bulbs maybe?

Keep us posted!

remove the random delay statement for every 3 entities? or just shorten?

suggestion way outside of my coding skills:

  1. save the state of the bulb before starting the loop, revert after exiting the loop.

If I am understanding you correctly, this would be very easy to do with the handy dandy scene.create service. Take a look at the last example here. https://www.home-assistant.io/integrations/scene/#creating-scenes-on-the-fly

I’ll have to give this a try tomorrow! I also have two bedside hue lights so this is perfect. :slightly_smiling_face:

Regarding the nine bulb situation… I wonder if you could write this as a script with the entity as a variable, and have nine instances of the script running in parallel? That would spare you “writing out” the automation actions nine times.

2 Likes

For my Immax Neo Bulb is this too fast. I have to little change code:

 action:
    - alias: opakovat dokud se přepínač nevypne
      repeat:
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: light.0x680ae2fffee34c7d_light
              brightness: '{{ (range(15, 60)|random) }}'
              color_temp: '{{ (range(450, 480)|random) }}'
              transition: 2
          - delay:
              milliseconds: '{{ (range(1500, 2000)|random) }}'
        until:
          - condition: state
            entity_id: input_boolean.efekt_svicky
            state: 'off'

But really THANK YOU !! :smiley:

transition: 0.5 seems to work.

Thank you!

For my Philips Hue with color I modified some, and below is the best I managed to came up with. With orange / yellow effect.

Suggestions to make it even better appreciated!

action:
  - repeat:
      sequence:
        - service: light.turn_on
          data_template:
            entity_id: light.kitchen
            hs_color:
              - '{{ (range(25, 45)|random) }}'
              - '{{ (range(94,101)|random) }}'
            brightness: '{{ (range(20, 80)|random) }}'
            transition: '{{ (range(1, 3)|random) }}'
        - delay:
            milliseconds: '{{ (range(200, 800)|random) }}'

Thanks for your clever idea!
I moved it a bit further: when brightness is higher, the color on my bulb goes more yellow - and back to red-ish color, when brightness is lower.
I needed variable for this, so i created script, which is called in automation.
Automation:

alias: Candle effect
trigger:
  - platform: state
    entity_id:
      - input_boolean.candles
    from: "off"
    to: "on"
action:
  - repeat:
      until:
        - condition: state
          entity_id: input_boolean.candles
          state: "off"
      sequence:
        - service: script.random_candle_setting
        - delay:
            milliseconds: "{{ (range(500, 1000)|random) }}"
mode: single

Script:

alias: Random candle setting
variables:
  random_value: "{{ range(0, 100) | random }}"
sequence:
  - service: light.turn_on
    data:
      brightness: "{{ ((140 * random_value / 100) + 10) | int }}" #10-150
      rgb_color:
        - 255
        - "{{ (195 * random_value / 100) + 60 | int}}"  #60-255
        - 0
      transition: 0.5
    target:
      entity_id: light.candle_bulb
mode: restart
icon: mdi:menorah-fire
1 Like

Ok, I thank you for your suggestions but all these solutions didn’t satisfy me fully.
I had to learn yaml (hey, guy from hass why create a new language, there are soo much that already make the job?)
I have improved the script to make it generic be able to apply to many lights simultaneously by independently.
The result is better but still does not convince me, you are welcome if you have other suggestions to make my phillips hue lamps look like candles.
Script:

alias: Light_to_candle
mode: parallel
max: 25
icon: mdi:candle
variables:
  random_value: "{{ range(0, 100) | random }}"
fields:
  target_light:
    description: light to turn to candle
    example: light.room_kindoflight
sequence:
  - repeat:
      until:
        - condition: template
          value_template: "{{ states(target_light) == \"off\" }}"
      sequence:
        - service: light.turn_on
          data:
            hs_color:
              - "{{ (range(25, 45)|random) }}"
              - "{{ (range(94,101)|random) }}"
            brightness: "{{ (range(20, 80)|random) }}"
            transition: "{{ (range(1, 3)|random) }}"
          target:
            entity_id: "{{ target_light }}"
        - delay:
            milliseconds: "{{ (range(200, 800)|random) }}"

Automation:

alias: Candle bath
description: ""
trigger:
  - platform: device
    domain: mqtt
    device_id: 9b1c3eb7c0d8d7e9579239c6df07e862
    type: action
    subtype: "on"
    discovery_id: 0xdc8e95fffef3ebf9 action_on
condition: []
action:
  - parallel:
      - service: script.light_to_candle
        data:
          target_light: light.salon_light_left
      - service: script.light_to_candle
        data:
          target_light: light.salon_light_right
mode: single

You can also use this pyscript : PyScript Candle Light Simulator · GitHub . It gives a result that is much closer to the candle effect in the hue app.

I have modified this script so that it retains the color and brightness of the bulb. It makes it possible to apply the effect on any color and brightness without having to do a specific automation. In this version the light temperature is not used for the effect, but I found it to be more candle-like this way, and this is the behavior of the effect in the hue app.

Here is the pyscript (to use with the pyscript integration):

import random

@service
def candle_light(**params):
    entity_id = params.get('entity_id')
    if entity_id is None:
        log.error(f'candle_light: entity_id required. saw {params}')
        return

    if not entity_id.startswith('light.'):
        log.error(f'candle_light {entity_id}: entity_id must be a light')
        return

    task.unique(f'candle_light_{entity_id}')

    statef = params.get('state')
    if statef != 'on':
        log.info(f'candle_light {entity_id}: stopping')
        return

    log.info(f'candle_light {entity_id}: starting')
    
    brightness = state.getattr(entity_id)["brightness"]
    brightness_min = max(brightness - 10, 0)
    brightness_max = min(brightness + 10, 255)

    delay = params.get('delay', 500)
    delay_min = round(delay * 0.5)
    delay_max = round(delay * 1.5)

    log.info(f'candle_light {entity_id}: brightness {brightness_min}-{brightness_max},  delay {delay_min}-{delay_max}')

    while True:
        brightness = random.randint(brightness_min, brightness_max)
        delay = (random.randint(delay_min, delay_max)) / 1000
        light.turn_on(
            entity_id=entity_id,
            brightness=brightness,
            transition=delay
        )

        task.sleep(delay)

And the action to use in automation :

service: pyscript.candle_light
data:
  entity_id: light.yourlightid
  state: "on"
  delay: 150
enabled: true

To switch off the effect :

service: pyscript.candle_light
data:
  entity_id: light.yourlightid
  state: "off"

With a delay of 150 it works nicely, I compared it with the hue app ‘candle’ effect on a different bulb at the same time and if you select the right temperature it does match quite well. When changing the color of your bulb to orange you have the ‘fireplace’ effect. And in green it’s interesting as well, you feel like you’re in Harry Potter :slight_smile:

2 Likes

I get this error when i use your script. of course i adjusted it to fit my lights:

alias: Movie Night Test
mode: parallel
max: 25
icon: mdi:string-lights
variables:
  random_value: "{{ range(0, 100) | random }}"
fields:
  target_light:
    description: light to turn to Movie lights
    example: light.living_room_lights
sequence:
  - repeat:
      until:
        - condition: template
          value_template: "{{ states(target_light) == \"off\" }}"
      sequence:
        - service: light.turn_on
          data:
            hs_color:
              - "{{ (range(25, 45)|random) }}"
              - "{{ (range(94,101)|random) }}"
            brightness: "{{ (range(20, 80)|random) }}"
            transition: "{{ (range(1, 3)|random) }}"
          target:
            entity_id: "{{ light.living_room_lights }}"
        - delay:
            milliseconds: "{{ (range(200, 800)|random) }}"

image

Much easier and better solution has been lately proposed to me by a fried of mine.
It’s the outcome of this thread where Devian sniffed Hue traffic
([Feature request]: Support Candle and Fireplace effects on Hue lights · Issue #15699 · Koenkk/zigbee2mqtt · GitHub).
Basically all you need to do is sent Zigbee command with the use of zha_toolkit (available via HACS: https://hacs.xyz/)
The command for candle would look like that:

service: zha_toolkit.zcl_cmd
data:
  manf: 4107
  ieee: light.philips_go_light
  endpoint: 11
  cluster: 0xfc03
  dir: 0
  cmd: 0x00
  args:
  - 0x21
  - 0x00
  - 0x01
  - 0x01
2 Likes

I have tried to get my head around that but failed :sweat_smile:

Could you please go into a bit more detail how to get the needed zha_toolkit variables.
Which ones are necessary and where do I get them.
Possibly to get the fireplace effect working as well.

Thanks :slight_smile:

Hi @zaki,
I won’t be able to help you with all as I got some help from my friend :slight_smile: but hope that will help:

Manufacture ID data: Received message from unsupported device with Zigbee model 'LOM003' · Issue #4244 · Koenkk/zigbee2mqtt · GitHub
Cluster ID data: List and function of ManufacturerSpecificCluster in ZHA · zigpy/zigpy · Discussion #823 · GitHub
As to the args I’ve used those sniffed ones: [Feature request]: Support Candle and Fireplace effects on Hue lights · Issue #15699 · Koenkk/zigbee2mqtt · GitHub
For fireplace you have to use those:
args:
– 0x21
– 0x00
– 0x01
– 0x02