Automation issue with lights

So some of my lights, maddeningly, sometimes don’t switch on or off when they are supposed to so I wrote an automation to turn them on or off a second and third time straight away and again after a delay of 5 seconds. It looks like this:

- id: '1532478833426'
  alias: Lights Off Failsafe
  initial_state: 'on'
  trigger:
  - platform: state
    from: 'on'
    to: 'off'
    entity_id: light.lounge, light.dining, light.spots, light.office
  condition: []
  action:
  - service: light.turn_off
    data_template:
      entity_id: >-
        {%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 5) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5) -%}
        light.lounge, light.spots
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 5) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5) -%}
        light.office, light.spots
        {%- elif as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 5 -%}
        light.spots
        {%- elif as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 5 -%}
        light.office
        {%- elif as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 5 -%}
        light.lounge
        {%- else -%}
        light.dining
        {%- endif -%}
  - delay: '00:00:05'
  - service: light.turn_off
    data_template:
      entity_id: >-
        {%- if (as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 15) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15) -%}
        light.lounge, light.spots
        {%- elif (as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 15) and (as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15) -%}
        light.office, light.spots
        {%- elif as_timestamp(now()) - as_timestamp(states.light.spots.last_changed) < 15 -%}
        light.spots
        {%- elif as_timestamp(now()) - as_timestamp(states.light.office.last_changed) < 15 -%}
        light.office
        {%- elif as_timestamp(now()) - as_timestamp(states.light.lounge.last_changed) < 15 -%}
        light.lounge
        {%- else -%}
        light.dining
        {%- endif -%}

These are limitlessled lights and are grouped of course and I find 1 of the 4 sometimes doesn’t turn on or off the first time.

I’m pretty sure I have the template right as it seems to be triggered correctly in the editor but I’m still finding it’s not turning on or off correctly…

Any ideas? Maybe I have the times wrong or something stupid like that.

How reliable is the connection of these lights?

I have two WiFi plugs that drop off of HA just for a few seconds every x hours, but sometimes this is enough for them not to get triggered on or off, e.g. I have a circulation pump that sometimes doesn’t get shut off after two minutes because the connection to the plug has dropped and then I wonder why I get a notification from the statistics sensor that the pump has been on for too long :confused:

They are pretty good… they use a hub. I can have 4 lights in a group and only one of them won’t turn on or off… It’s very weird! Hence the automation.

I’m almost 100% sure that automation is working now as I had the second one visually trigger after a delay just now. I added a 2 second delay before the first one triggers so now I have the original state change, a 2 second delay and the action is repeated and then a 5 second delay before a second repeat. Same for off and on.

I’m also checking for 2 lights at once as it’s possible for both office lights to be on and my nighttime routine usually has the lounge and spots on at the same time and turned off together.
Of course if I think of other combinations it’s simple to add those in as well.

I was making a mistake by not having the combinations first and then one of the lights would trigger and if loop would terminate… Good learning exercise.

I have a similar situation with an automation that I have that’s supposed to turn on all my lights. Sometimes it won’t turn them all on. And it’s not the same one all the time. And it’s not limited to platform either. I have sonoffs, zwave and zigbee that randomly have the same issue.

There was another user on here a while back that posted that they had the same strange functionality. It must be a bug in that HA loses track of which devices it switched if operating a bunch of them at the same time.

I haven’t tried that automation in a while since I switched to the NUC so it might work now. maybe its a Pi cpu load thing?

Nope. I’m on a NUC now as well. It’s nothing to do with Home Assistant. LimitlessLED uses a hub and the lights are set up so in my case, Zone 1 has 2, Zone 2 has 3, Zone 4 has 3 and Zone 4 has 4 globes. Even using the iOS app, the command gets sent once by the app/home assistant and sometimes it doesn’t hit all of them in the one command… I have the hub situated centrally… as you find, it’s a different globe all the time and sometimes all work, sometimes one doesn’t.

So anyway, this automation seems to be working. I did add a delay before the first trigger of 2 seconds as well so HA will turn on or off at 0, 2 and 5 seconds after trigger… I also never have this issue with my 2 Yeelights which connect directly to the router.