Turn off All but list

Hi All, To start… I absolutely love home assistant. It is a wonderful system. I am having a bit of a difficult time here creating a script that will turn off ALL “light.” entities with the exception of a list.

Basically, button you can press before bed to ensure all lights are off with the exception of the front porch light (light.front_porch) and the christmas lights (light.christmas_lights).

I had a script working that would do 1 which was the front porch light. That worked fine till now. Basically didnt have a need for that switch to be on, but now that the christmas lights are up, I dont want them turning off.

So I have tried EVERY suggestion from the forums and even chatgpt generated configuration. Nothing has worked with any success.

This will generate the list with out the two defined, however it does not work.

service: light.turn_off
data_template:
  entity_id: >
    {% set excluded_lights = ['light.christmas_lights', 'light.front_porch'] %}
    {% for state in states.light -%}
      {%- if state.entity_id not in excluded_lights %}
        'data:{{ state.entity_id }}',
      {%- endif %}
    {%- endfor %}

The above generates a list, but when I run the service i get the message saying: not a valid value for dictionary value @ data[‘entity_id’]. Got None

I surely hope someone out there can help me. It is :pinching_hand:this close to working, I can feel it.


It wouldn’t let me post this to the initial post

I posted this to show what the main page looks like. I get inspiration from the posts from others on what all you can do. Mine is pretty basic though running the mushroom hacs frontend. The most complicated thing is the states on the front door lock / unlocked.

Using a for loop like that creates a list-shaped string, not a true list… it’s also less efficient than using the built-in filters and methods. FWIW, data_template has been deprecated for core services for over 2 years.

service: light.turn_off
target:
  entity_id: >
    {% set excluded_lights = ['light.christmas_lights', 'light.front_porch'] %}
    {{ states.light | rejectattr('entity_id', 'in', excluded_lights)
    | map(attribute='entity_id') | list }}
1 Like

I greatly appreciate your time to provide some alternative config to try. However, it did not work when pasted in as defined in the post above. I am open to thoughts.

For giggles I posted this into developer tools and it gave me nothing back. The other code atleast provided a list of entities that were not the front porch or christmas lights.

Thank you, Russell

Template rendered invalid entity IDs: {( states.light | rejectattr(‘entity_id’, ‘in’, excluded_lights) | map(attribute=‘entity_id’) | list }}

:point_up_2: This is the error message in copy pasteable format :slight_smile:

One of the brackets wasn’t curly enough… I have corrected it in the original post above.

You are truely amazing :clap:. That worked perfectly.

Again thank you! Got you two coffees :coffee: