Philips Hue Power Spike Fix

I have approx 40 Philips Hue bulbs, and they are all configured to restore previous state on power loss / restore.

But so far, for every power loss that has occured, when power is restored all bulbs switch on with maximum brightness.

I believe this is because the powerloss was very short, a spike and the bulbs cannot decide whether the user has flicked off and on the wall switch to demand full brightness or whether it was a power loss and decides the former.

So this automation fires whenever >= 80% of the available bulbs were all powered on within 1 second of each other and if so, it will assume it was a spike and will switch them all off.

alias: Hue Power Restore
trigger:
  - platform: template
    value_template: >-
      {% set ns = namespace(hue_on=0,hue_total=0) %}
      {% for light in states.light -%}
        {% if 'hue_' in light.entity_id and light.state == 'on' -%}
          {% set ns.hue_on = ns.hue_on + 1 -%}
        {% endif -%}
        {% if 'hue_' in light.entity_id and light.state != 'unavailable' -%}
          {% set ns.hue_total = ns.hue_total + 1 -%}
        {% endif -%}
      {% endfor -%}
      {#  80% of Bulbs Are On #}
      {{ ns.hue_on >= (ns.hue_total * 0.8)|int }}
      
condition: []
action:
  - service: light.turn_off
    data_template:
      entity_id: |
        {% set lights = states.light
          | selectattr('state', '==', 'on')
          | selectattr('last_changed', '>', now()-timedelta(seconds=1))
          | selectattr('entity_id', 'contains', 'light.hue_')
          | map(attribute='entity_id')
          | join(', ') 
        %}
        {{ lights if lights | length > 0 else 'none' }}
1 Like