Script not working for specific lights

I have an automation that is supposed to turn off all the lights unless the entity is listed as an exclusion. However, it doesn’t turn off two of the lights “light.downstairs_ceiling_light” and “light.entry_light” for some reason. I checked the script trace and the lights are listed there so I would expect they should turn off. I can manually toggle them on and off individually so I’m confused why the script can’t turn them off.

Core: 2024.9.3
Frontend: 20240909.1

Any ideas on how I can fix this? Thanks!

Script:

alias: All Lights Off
sequence:
  - target:
      entity_id: |
        {{
          states.light
            | selectattr('state', 'eq', 'on')
            | rejectattr('entity_id', 'in', exclude_entity_ids)
            | map(attribute='entity_id')
            | join(', ')
        }}
    action: light.turn_off
description: Turn all lights off except an exclude list
mode: single
fields:
  exclude_entity_ids:
    description: entity_ids to exclude

Automation:

alias: All Lights Off
description: All lights off except for exclusions
trigger: []
condition: []
action:
  - action: script.all_lights_off
    data:
      exclude_entity_ids:
        - light.garage_exterior_light_1
        - light.garage_exterior_light_2
        - light.garage_exterior_lights
mode: single

Script trace (step details) - I can see the “light.downstairs_ceiling_light” and “light.entry_light” in the list of entities and all of the others listed turned off as expected.

Executed: September 25, 2024 at 3:20:54 PM
Result:

params:
  domain: light
  service: turn_off
  service_data: {}
  target:
    entity_id:
      - light.main_floor_group
      - light.lower_floor_group
      - light.garage_group
      - light.upper_floor_group
      - light.kitchen_pot_lights
      - light.living_room_pot_lights
      - light.bookcase_lights
      - light.garage_bench_lights
      - light.dining_table_lights
      - light.kitchen_pendant_lights
      - light.kitchen_ceiling_lights
      - light.kitchen_valence_lights
      - light.kitchen_cabinet_lights
      - light.downstairs_ceiling_light
      - light.garage_bench_lights_2
      - light.entry_light
      - light.oven_light
      - light.microwave_light
      - light.livingroom_dimmer
      - light.office_floor_lamp
      - light.couch_lamp
      - light.sliding_door_floor_lamp
      - light.living_room
      - light.office
running_script: false

What kind of lights are they?

They are tp-link kasa wall switches that I’ve created helpers for to convert them into light entities.

My experience trying to do this sort of thing with a large number of mixed Zwave and Zigbee lights is that if you do too many at once the network can get overloaded and drop the commands.

I’d suggest testing it by adding a 0.5s delay between each command. This won’t be easy with your current script, but you could use the first part to generate a list and then have a repeat that goes through the list.

If that turns them off, then that’s probably the problem.

I have multiple scripts that are trying to control larger groups of lights that look like this:

Script to dim a bunch of lights to 50%

  • first turn on of all lights to 52%
  • wait one second
  • try turning them on again to 51%
  • wait one second
  • try turning them on again to 50%

I find this works very reliably. If I just do the first one, then sometimes a few are left at the previous value. If I do this they all end up somewhere around 50%. :slight_smile:
-David

I had to delete those two helpers (change switch to light entity), restart home assistant (not sure if this was necessary), and create the helpers again to fix this problem. This seems like a bug, but I’m not sure what the problem was as turning on all kinds of various debug logging wouldn’t show me any obvious errors. Just posting this in case someone else has the same problem.