Automation ignores partially conditions

I can’t explain why, but my HA keeps on partially ignoring autmation conditions …
Maybe you could give me a hint.

My idea is: when i did NOT turn on lights by hand, they should turn on and off, as i walk by.
When i did, they should stay on, until i turn them off by hand again. SO motion is ignored then.
And auto-on/off should only happen in the evening.
So my first code (after a lot of improving) was:

- alias: lights on evening
  trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_motionsensor
    to: 'on'
  condition:
  - condition: state
    entity_id: light.1
    state: 'off'
  - condition: state
    entity_id: light.2
    state: 'off'
  - condition: state
    entity_id: light.3
    state: 'off'
  - condition: time
    after: '19:00:00'
    before: '00:01:00'
  action:
  - service: light.turn_on
    entity_id: light.group_2
  - delay: 00:00:45
  - service: light.turn_off
    entity_id: light.group_2

And this works (mostly) pretty well.
Just from time to time the light goes off, even i turned it on by hand, but it’s like 1 of 30 cases.
It’s ok for me.

Now i wanted to have the same in the morning. So i tried to put another condition “or” with the time in the morning inside this automation, but it did not work at all.
For this i copied the whole automation and just changed the time.
This is the new (second) code:

- alias: lights on morning
  trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_motionsensor
    to: 'on'
  condition:
  - condition: state
    entity_id: light.1
    state: 'off'
  - condition: state
    entity_id: light.2
    state: 'off'
  - condition: state
    entity_id: light.3
    state: 'off'
  - condition: time
    after: '04:30:00'
    before: '07:15:00'
  action:
  - service: light.turn_on
    entity_id: light.group_2
  - delay: 00:00:45
  - service: light.turn_off
    entity_id: light.group_2

And now comes the real strange thing:
It does NOT work!
The light is turned off every time i walk by, doesn’t matter if i turn it on by hand or not.
But it’s exactly copy of the first rule, this works and still it seems like HA is ignoring the “if light 1/2/3 is off” condition.
Is it my code, which is wrong?

PS:
light.group_2 is the group, where light 1/2/3 a re in.
I cannot change all the 3 lights to one group, because wh en i tell google to turn on lights, it turns on all 3 lights separatly, and not the group, so the condition is always met.

First, you are doing a cardinal no no. You should never name your entity_id’s with just a number. In code, this causes issues. You should change them all to light.light1 or something. This will cause issues with your system. In fact, it may be the root cause of the intermitten issues you are seeing.

Second, you can use them all in a single group but you need to use a value_template, you can’t use a normal condition.

This should work for you, if it doesn’t rename your lights to use a starting character that is not a number I.E instead of light.1 use light.hallway1:

- alias: lights on morning and evening
  trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_motionsensor
    to: 'on'
  condition:
  - condition: state
    entity_id: light.1
    state: 'off'
  - condition: state
    entity_id: light.2
    state: 'off'
  - condition: state
    entity_id: light.3
    state: 'off'
  - condition: or
    conditions:
    - condition: time
      after: '19:00:00'
      before: '00:01:00'
    - condition: time
      after: '04:30:00'
      before: '07:15:00'
  action:
  - service: light.turn_on
    entity_id: light.group_2
  - delay: 00:00:45
  - service: light.turn_off
    entity_id: light.group_2

Or if you want to try the value template try the code below. The value template makes a list of lights in the group and a list of lights that are off. If the total number of lights that are off matches the total number of lights, then all the lights are off (and we can continue).

- alias: lights on evening
  trigger:
  - platform: state
    entity_id: binary_sensor.tradfri_motionsensor
    to: 'on'
  condition:
  - condition: template
    value_template: >
      {% set all_lights = state_attr('group.group_2','entity_id') %}
      {% set off_lights =  states.light | selectattr('entity_id','in', all_lights ) | selectattr('state','eq','off') | list %}
      {{ all_lights | length == off_lights | length }}
  - condition: or
    conditions:
    - condition: time
      after: '19:00:00'
      before: '00:01:00'
    - condition: time
      after: '04:30:00'
      before: '07:15:00'
  action:
  - service: light.turn_on
    entity_id: light.group_2
  - delay: 00:00:45
  - service: light.turn_off
    entity_id: light.group_2

Big thx for your reply.

The first code, as you written, was also my first try to get it to work.
It doesn’t ^^
The “or” condition is not reacting so the lights turns on automaticly at evening only.

I followed your advice and renamed the lights 1/2/3 to hallway1/hallway2/hallway3 (indeed these are hallway lights :wink: ) in the Tradfri app.

But now my “group_2” is gone, which i missed yesterday so the lights did’t turn on (for obvious reason) today in the morning … i will check this out and maybe re-create a new group for this.

Will report asap …

Great!
It really helped fixing the bugs.
Even the “or” condition works now fine.
Thank you :slight_smile:

1 Like