Circadian Lighting: Possible fix to keep light groups off

Circadian Lighting: Possible fix to make light groups stay turned off

Difficulty: Advanced

The problem

I have multiple light fixtures which have more than one Zigbee bulb. So I created Zigbee groups for each fixture. Unfortunately, with Circadian Lighting enabled for these light groups, the bulbs all turn back on within 2 seconds - 2 minutes. This is a known issue without a documented fix so far.

The problem occurs while the bulbs fade to black. The light group briefly itself off to Home Assistant, but then realizes bulbs in the group have a brightness > 0%, so it changes the state back to on. Well, Circadian Lighting now sees a light which has just turned on, so it dutifully starts adjusting the light colors/brightness appropriate for the current time of day. <sarcasm>How helpful!</sarcasm> :stuck_out_tongue_winking_eye:

Now that we know the problem, I managed to come up with a solution which took some extra work to pull off. In the end I think the effort is work it (so long as it ends up being as reliable as I hope it is).

Proposed solution

I have put together an automation which triggers when one of my light groups turn off. It relies on several of the features added in 0.113: mode: parallel, and an until loop!

Home Assistant group of lights

Unfortunately ZHA doesn’t provide a list of entity_id’s in the light group, so I created light fixture groups for all four of my fixtures. Here is one such example.

group:
  name: Family Room Floor Lamp Fixture
  entities:
    - light.family_room_floor_lamp1
    - light.family_room_floor_lamp2
    - light.family_room_floor_lamp3

Circadian Lighting switches

I broke my Circadian Lighting mode switches up. Here is are two examples.

switch:
  - platform: circadian_lighting
    name: Dining Room
    lights_ct:
      - light.dining_room

  - platform: circadian_lighting
    name: Family Room Floor Lamp
    lights_brightness:
      - light.family_room_floor_lamp

Light group turns off automation

Here I break down what happens when I turn my Family Room Floor Lamp off.

  1. This automation is triggered when the Floor Lamp’s state changes to off (ie. light.family_room_floor_lamp).
  2. Don’t bother preforming any actions if Circadian Lighting mode is disabled for the Floor Lamp (ie. switch.circadian_lighting_family_room_floor_lamp).
  3. Temporarily disables Circadian Lighting mode only for the Floor Lamp.
  4. Delay for 5 seconds.
  5. Tell Home Assistant to turn off the Floor Lamp’s “Home Assistant group of lights” (ie. group.family_room_floor_lamp_fixture).
  6. Are the lights actually off now? If not, return to step #4.
  7. Delay for 5 seconds.
  8. Enable Circadian Lighting mode for the Floor Lamp.
alias: "Circadian Lighting: Force Light Groups To Stay Off"
id: circadian_light_group_turn_off
initial_state: true
mode: parallel
trigger:
  - platform: state
    entity_id:
      - light.brian
      - light.dining_room
      - light.family_room_floor_lamp
      - light.play_room_floor_lamp
    to: "off"
condition:
  # Only when circadian lighting is enabled for this light group.
  - condition: template
    value_template: "{{ is_state(trigger.entity_id|replace('light.','switch.circadian_lighting_'), 'on') }}"
action:
  # Temporarily disable the circadian lighting mode for this fixture.
  - service: switch.turn_off
    data_template:
      entity_id: "{{ trigger.entity_id|replace('light.','switch.circadian_lighting_') }}"

  - alias: Turn the light group off repeatedly until it stays off.
    repeat:
      sequence:
        - delay:
            seconds: 5

        # Turn the light group off, again.
        - service: homeassistant.turn_off
          data_template:
            entity_id: "{{ trigger.entity_id|replace('light.','group.') + '_fixture' }}"
      until:
        - condition: template
          value_template: "{{ is_state(trigger.entity_id, 'off') }}"

  - delay:
      seconds: 5

  # Turn the circadian lighting mode back on.
  - service: switch.turn_on
    data_template:
      entity_id: "{{ trigger.entity_id|replace('light.','switch.circadian_lighting_') }}"

Conclusion

I have only has this solution in place for a couple of hours so I don’t know how reliable it will be in the long run. Thus far it is a massive improvement over the failure I was experiencing before this fix. :slight_smile:

Honestly this solution is complex. You could probably simplify the code a bit by avoiding the ZHA and Home Assistant Light Groups (ie. light.family_room_floor_lamp), and directly controlling the lights GROUP (ie. group.family_room_floor_lamp).

2 Likes

I am playing with something similar currently.
Would it also work maybe to:
Trigger on any light going off
Disable the circadian switch where this light belongs to
Wait 10 seconds
Turn on circadian switch?

Or is there something I am missing here?

Basically. However, I needed the Repeat turn the light off repeatedly until they are actually off. Leaving that out the lights would just turn right back on.

I am trying this right now (shorter delays like 5 sec. helped a lot already but where not 100% reliable):

- id: '1598208244342'
  alias: circadian lighting workaround vorzimmer
  description: ''
  trigger:
  - entity_id: light.vorzimmer_light, light.gang_light
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: switch.circadian_lighting_vorzimmer
    state: 'on'
  action:
  - data: {}
    entity_id: switch.circadian_lighting_vorzimmer
    service: switch.turn_off
  - delay: '130'
  - data: {}
    entity_id: switch.circadian_lighting_vorzimmer
    service: switch.turn_on
  mode: single
1 Like

Sweet. Simple code that does the same job done is always better.

1 Like

It would be nice if one could configure CL to only trigger when a light is on for x seconds. But the author stated on his page that he does not want to do that.
Maybe a fork or PR is on order :grinning:

I understand why that would be undesirable. It would cause the light to turn on full brightness, then it would suddenly change after the delay. I believe our work-around for individual troublesome light fixtures is the lesser of two evils so to speak. :slight_smile:

You could just turn on the lights to the right brightness and color already like kn CLs advanced use section described.