Turn off - Close Except /// Turn on - Open Except

Hi,
I am trying to have some automations and actions (scripts) to achieve what the title says and other similar things.

And since all my entities are arranged into areas, and the selectors in scripts offer the input of Area/Device/Entity

To save time in selections, I want to be able to use areas, devices, and entities (groups) but sometimes I want to exclude one or two entities from that action. Like ‘Turn off all ground floor lights but leave the stairs light on’ or vise versa.

Same applies to covers.

I tried the below script, but I know there is a better and more efficient way to express it. any advice?

blueprint:
  name: On Open Except
  description: Perform actions to prepare the house for a good night sleep.
  domain: script
  input:
    turn_on_lights:
      name: Turn on lights
      description: Turn on lights.
      default: ""
      selector:
        target:
    open_covers:
      name: Open Covers
      description: Open Covers.
      default: ""
      selector:
        target:
    # dely_time:
    #   name: Delay time
    #   description: Delay time between off and on.
    #   default: 00:00:00:00
    turn_off_lights:
      name: Turn off lights
      description: Turn off lights.
      default: ""
      selector:
        entity:
          domain: light
          multiple: true
    close_covers:
      name: Close Covers
      description: Close covers.
      default: ""
      selector:
        entity:
          domain: cover
          multiple: true

mode: restart

sequence:
  - service: light.turn_on
    data: {}
    target: !input turn_on_lights
  - service: cover.open_cover
    data: {}
    target:
      entity_id: !input open_covers
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target: !input turn_off_lights
  - service: cover.close_cover
    data: {}
    target:
      entity_id: !input close_covers

R.S.

I guess this is the first step of the solution. Now I need to figure out how to put it in an automation or script.

{% set exception_list = ('cover.gf_office_window', 'light.office_chandelier') |list  %}
{% for en_name in area_entities('office') -%}
  
   {%  if states[en_name].entity_id not in exception_list %}
     {{ states[en_name].name }} in
   {% else %}
     {{ states[en_name].name }} not
   {% endif %}
{%- endfor %}

Imagine there are more than 10 rooms in each floor in a three-floor building in the following code:

alias: Test area
sequence:
  - service: light.turn_off
    data: {}
    target:
      area_id: office
  - delay:
      hours: 0
      minutes: 0
      seconds: 3
      milliseconds: 0
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.office_chandelier
mode: restart

What I am trying to do is reaching a code that is:

  • Easy to build
  • Easy to maintain

Any assistance is appreciated
RS

I still don’t understand what you’re trying to do. This is just a script. Do you just want a script that you can call with variables?

Hi,
I want to have a button for each of the following statuses:

  • Welcome home: Close covers except some & Turn on lights except some
  • Have Guests
  • Have friends
  • Nice evening in the garden
    and those to handle the transition from one to another

The thing is I have so many areas and so many entities and I am doing a lot of trials to figure out what is the best approach to achieve this.
I hope I managed to explain it well!

is this going to be displayed on a screen or something? How is the script getting called?

Nothing to be displayed on screen. Just have a button to run it. Also I am not sure if it has to be a script, automation. But finally I went for script because it might involve a lot of could (to my limited knowledge)

I have over 200 light/switch circuits and if for each scenario I need to list all those to be ‘off’ and those to be ‘on’ for any variance it would be hectic I guess. especially when some of those scenarios will have SEASONAL variance too

This is the run of the code you provided in the other post

Domains: (28)
automation (17)
binary_sensor (72)
button (37)
camera (2)
climate (18)
cover (60)
device_tracker (21)
group (2)
input_boolean (9)
input_datetime (4)
input_number (21)
input_select (1)
input_text (4)
light (219)
media_player (14)
number (6)
person (5)
remote (1)
scene (1)
script (7)
select (8)
sensor (349)
sun (1)
switch (162)
update (10)
var (3)
weather (1)
zone (2)

I guess I still don’t understand. Are you going to be pressing this button? Is it physical? What’s the button going to do. What your describing here sounds like a mish mosh of 500 different actions. You need to break it down. How many buttons per area, what do you want each button to do

I have ‘Quick Actions’ buttons that will trigger those scripts i.e. pressing the ‘Good night’ button will Close covers / Turn off lights of Guests room, Kitchen, Living, Garden, Outdoor, Corridors,… but leaving 1-3 lights and covers (exceptions from being affected by the script.
Pressing ‘Welcome home’ button will trigger to have open / Turn on Living, Garden, Kitchen, and corridor lights and covers but not fully i.e. 1 light in each area.
However, same, If Guests was pressed at the start of the evening, after the guests leave, pressing ‘Welcome home’ will cause all all intended covers/lights to be on and the rest (in this case were opened by ‘Guests’ to be turned off/closed

This is will be applied for the whole home, and separately for each floor. and since each area has a lot of controllable items, making a blueprint out of it will surely minimize the time of setup and maintenance.

Still WIP but I think I got the concept via this post:

{% set x = ['unavailable','unknown'] %} 
{% set areas = namespace(home=[],b1=[],gf=[],ff=[],rf=[],od=[]) %}

{% set entities = states |rejectattr('state', 'in', x)|map(attribute='entity_id')|list %}

{%- for en in entities -%} 
  {%- if (area_name(en) != None and area_name(en) not in areas.home) -%}
     {% set areas.home = areas.home + [area_name(en)] %}
     {% if "GF " in area_name(en) %} {% set areas.gf = areas.gf + [area_name(en)] %}
     {% elif "FF " in area_name(en) %} {% set areas.ff = areas.ff + [area_name(en)] %}
     {% elif "RF " in area_name(en) %} {% set areas.rf = areas.rf + [area_name(en)] %}
     {% elif "B1 " in area_name(en) %} {% set areas.b1 = areas.b1 + [area_name(en)] %}
     {% else %} {% set areas.od = areas.od + [area_name(en)] %}
     {% endif %}
  {% endif %}
{%- endfor -%}