Automate for making sure lights are off before toggling

Hia :wave:
If this has already been answered, then I apologise in advance
I need help creating an automation.

In my bathroom I got

  • 5 LED spots (Ikea)
  • a switch (tasmota)
  • Ikea 5 button remote

I’d like to model the following behaviour on button-push

if(some lights are on){
  turn off
}
else
{
  if(its night){
    turn-some-lights on
  }   
  else{
  CRANK IT UP
  }
}

But how to do it?? I’d really like your input.

Right now, I simply toggle everything on btn-push. But, as I use ZHA-groups sometimes not all the lights receives the order, which brings the room into an annoying (disco)state.

Thanks :slight_smile:

That’s something that cannot be resolved by HA, if the underlying integration fails to address and report devices properly.

With up to 5 lamps you could drop that ZHA group in favour of a ZA Light Group. The group will be on if one element is on and off, if all elements are off.

Drawback is, that lamps will not switch on/off at the same time, but with noteable delay.

You should get your Zigbee network fixed.

I never said the entities doesn’t report back :slight_smile: They do! (at least most of the time). It seems to be a “timing issue”; sometimes not all the lights in the ZHA Group won’t turn off(/on)

Thanks for the suggestion with HA light Groups (I assume that’s what you mean)

I’m sure HA would be capable of solving my issue.
I’ve fiddled with templates, like this one:

 {% if (
 is_state('light.bad_1_spot_1', 'on') or
 is_state('light.bad_1_spot_2', 'on') or
 is_state('light.bad_1_spot_3', 'on') or
 is_state('light.bad_1_spot_4', 'on') or
 is_state('light.bad_1_spot_5', 'on') or
 is_state('light.bad_1_spejllys', 'on')
 )
 %}
   Some lights on
     
 {% else %}
  all lights off
 {% endif %}

But I’m comming up short as to how to best integrate this into an automation. My thinking is that maybe that template could be expanded upon??

With 2 separate automations this wouldn’t be much of an issue. One automation with condition “at least one of the lights is on”. And another with condition “if all lights are off”.

That’s exactly the behaviour of a (Light) Group.

It’s easily done with one automation and two choose actions. I would group the lights in that room and create a night scene

Trigger on switch event

Actions:
Choose:
If light.bad is off
Condition daytime
Turn on 100%

Choose:
If light.bad is off
Condition nighttime
Set scene.night

Default action turn light.bad off

You’re correct. That choose: is new… :man_facepalming:

And very very useful

You just need to add an appropriate trigger to this:

  action:
  - variables:
      lights: ['light.bad_1_spot_1', 'light.bad_1_spot_2', 'light.bad_1_spot_3', 'light.bad_1_spot_4', 'light.bad_1_spot_5']
      lights_on: "{{ expand(lights) | selectattr('state', 'eq', 'on') | map(attribute='entity_id') | list }}"
  - choose:
    - conditions: "{{ lights_on | count == 0 }}"
      sequence:
      - service: light.turn_on
        data:
          entity_id: "{{ lights }}"
          brightness_pct: "{{ 100 if is_state('sun.sun', 'above_horizon') else 50 }}"
    default:
    - service: light.turn_off
      data:
        entity_id: "{{ lights_on }}"

How it works

  • It creates two variables:
    • lights is a defined list of your lights
    • light_on is a computed list of the lights that are currently on.
  • If no lights are on it will turn all lights on. If it’s daytime they are set to 100% otherwise to 50%.
  • If any lights are on it will turn them off.
1 Like

Thanks!!! That was awesome and elegant!!!
I did not mention I was using a scene for the light-settings at night, but I tweaked your code myself.

alias: Bad 1 remote kontrol
description: ''
trigger:
  - device_id: f13d08a716aa11ebb664037d737935e2
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - variables:
      lights:
        - light.bad_1_spot_1
        - light.bad_1_spot_2
        - light.bad_1_spot_3
        - light.bad_1_spot_4
        - light.bad_1_spot_5
        - light.bad_1_spejllys
      lights_on: >-
        {{ expand(lights) | selectattr('state', 'eq', 'on') |
        map(attribute='entity_id') | list }}
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ lights_on | count == 0 }}'
          - condition: time
            after: '23:00'
            before: '05:00'
        sequence:
          - scene: scene.bad_1_nat
      - conditions:
          - condition: template
            value_template: '{{ lights_on | count == 0 }}'
        sequence:
          - service: light.turn_on
            data:
              entity_id: '{{lights }}'
              brightness_pct: 100
    default:
      - service: light.turn_off
        data:
          entity_id: '{{ lights_on }}'
mode: single

Thank you so much for sharing!!
and also thanks to @m0wlheld, @samnewman86 and @metbril for chipping in.
You guys rock!!!

1 Like

If you’re interested, here’s your automation with the shorthand notation for Template Conditions:

alias: Bad 1 remote kontrol
description: ''
trigger:
  - device_id: f13d08a716aa11ebb664037d737935e2
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: turn_on
condition: []
action:
  - variables:
      lights:
        - light.bad_1_spot_1
        - light.bad_1_spot_2
        - light.bad_1_spot_3
        - light.bad_1_spot_4
        - light.bad_1_spot_5
        - light.bad_1_spejllys
      lights_on: >-
        {{ expand(lights) | selectattr('state', 'eq', 'on') |
        map(attribute='entity_id') | list }}
      qty: '{{ lights_on | count }}'
  - choose:
      - conditions: '{{ qty == 0 and (now().hour == 23 or now().hour < 5) }}'
        sequence:
          - scene: scene.bad_1_nat
      - conditions: '{{ qty == 0 }}'
        sequence:
          - service: light.turn_on
            data:
              entity_id: '{{lights }}'
              brightness_pct: 100
    default:
      - service: light.turn_off
        data:
          entity_id: '{{ lights_on }}'
mode: single

EDIT

Correction. Changed = to == in choice’s first condition.

@123, I would be very interested :slight_smile: Thank you.
That’s even more consice, I love it. Thank you for sharing

I believe a spotted a tiny mistake in this:

We need an evaluation (==/>=), not an assignment (=) of the 23rd hour.

I don’t think I was explicit in this, but I want the night-scene to be activated between 23 and 6. So for me that would be:
{{ qty == 0 and (now().hour >= 23 or now().hour < 5) }}

Again; thank you for showing me how this can be achieved. The templates-syntax and usage has been black magic, so having a Master of the Dark Art show me is a privilige :slight_smile:
Thanks

You’re correct; doubled-equals is needed there. I have updated the original example.

There’s no point in using >= because it is checking the hour and there is no hour greater than 23. Midnight is represented as 0 hours.