Service template: for loop (act on lights that are turned on only)

Hi everybody,

below is my current Fader automation. It is activated by an input_boolean.sz_fade entity; when it is turned on, it will set various WLED lights to nightlight mode (which means they will slowly fade darker and darker until they turn off).

Unfortunately, when this nightlight switch is activated, even those lights that are currently not turned on will initiate nightlight mode (so they will first turn on, then turn off), which can be a bit annoying.

- alias: [L] SZ Fader"
  trigger:
    - platform: state
      entity_id: input_boolean.sz_fade
      to: "on"
  action:
    - service: switch.turn_on
      entity_id: 
        - switch.sz_bett_nightlight
        - switch.sz_tv_nightlight
        - switch.sz_window_nightlight
    - service: input_boolean.turn_off
      entity_id: input_boolean.sz_fade
    - service: light.turn_off
      entity_id: light.schlafzimmer_nachttisch_light

I thought of using if as a template, but there must be an easier approach:

# (...)
  action:
    - service: switch.turn_on
      data:
        entity_id: >
          {% if is_state('light.sz_bett', 'on') and is_state('light.sz_tv', 'on') and           is_state('light.sz_window', 'on') %}
          - switch.light.sz_bett_nightlight
          - switch.light.sz_tv_nightlight
          - switch.light.sz_window_nightlight
        {% else if (......) %}
          # multiple different conditions, for example, if "bett" and "tv" are on and "window" is off, return
          # - switch.light.sz_bett_nightlight
          # - switch.light.sz_tv_nightlight
          # only
        {% endif %}

This would mean that Iā€™d have to create conditions for each case

  • all off
  • all but one (a) off
  • all but one (b) off
  • all but one Ā© off
  • all but two (a, b) off
  • all but two (a, c) off
  • all but two (b, c) off
  • etc. etc

I am having a rather hard time with jinja2, so I donā€™t know what to doā€¦ but is it possible to say ā€œthese are all lights; check all of their statuses, then return those that are currently on onlyā€?

Thank you in advance for your ideas :slight_smile:

Try this. ā€˜noneā€™ is a special case just for this.

- alias: [L] SZ Fader"
  trigger:
    - platform: state
      entity_id: input_boolean.sz_fade
      to: "on"
  action:
    - service: switch.turn_on
      entity_id: >
        {% switch.sz_bett_nightlight if is_state('light.sz_bett', 'on') else none %}
    - service: switch.turn_on
      entity_id: >
        {% switch.sz_tv_nightlight if is_state('switch.sz_tv_nightlight', 'on') else none %}
    - service: switch.turn_on
      entity_id: >
        {% switch.sz_window_nightlight if is_state('switch.sz_window_nightlight', 'on') else none %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.sz_fade
    - service: light.turn_off
      entity_id: light.schlafzimmer_nachttisch_light

Itā€™s clunky and it does not scale well but for 3 entities is ok.

1 Like

Create a group that contains all your lights. Then use this template to get a list of entity_ids for lights which are on.

{{ expand('group.my_lights') | selectattr('state', 'eq', 'on') | list }}
2 Likes

Thank you both @tom_l @Burningstone

Tomā€™s solution was my first approach, as I could understand it more easily (the selectattr part confuses me a bit and seems to return more than expected, output below).

However, this does not work. even though it should. My entire package for this is listed below. When I check it for errors via settings -> server control -> check configuration, it will show no errors. IF I create an error on purpose and check again, it will show this error (so the .yaml file is being checked by Home Assistant!). However, the automation does not show in developer settings -> state. The automation ought to be named automation.l_sz_fader, but it is not there (after multiple restarts and manually re-loading automations).

I renamed the .yaml file to something else and restarted again, but the automation is still not there. Perhaps this is related to this issue, but I could usually fix it by renaming the yaml filesā€¦

input_boolean:
  sz_fade:
    name: Fader

automation:
  - alias: "[L] SZ Fader"
    trigger:
      - platform: state
        entity_id: input_boolean.sz_fade
        to: "on"
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.sz_fade
      - service: switch.turn_on
        entity_id: >
          {% 'switch.sz_bett_nightlight' if is_state('light.sz_bett', 'on') else none %}
      - service: switch.turn_on
        entity_id: >
          {% 'switch.sz_tv_nightlight' if is_state('light.sz_tv', 'on') else none %}
      - service: light.turn_off
        entity_id: light.schlafzimmer_nachttisch_light
        data:
          transition: >
            {% 120 if is_state('light.schlafzimmer_nachttisch_light', 'on') else 0 %}
      - service: light.turn_off
        entity_id: light.jack
      - service: input_boolean.turn_off
        entity_id: input_boolean.sz_fade

Try it like this:

  action:
    - service: switch.turn_on
      data:
        entity_id: >
          {% switch.sz_bett_nightlight if is_state('light.sz_bett', 'on') else none %}
    - service: switch.turn_on
      data:
        entity_id: >
          {% switch.sz_tv_nightlight if is_state('switch.sz_tv_nightlight', 'on') else none %}
    - service: switch.turn_on
      data:
        entity_id: >
          {% switch.sz_window_nightlight if is_state('switch.sz_window_nightlight', 'on') else none %}
    - service: input_boolean.turn_off
      entity_id: input_boolean.sz_fade
    - service: light.turn_off
      entity_id: light.schlafzimmer_nachttisch_light
1 Like

I just came up with this, and it seems to work. I used the same line for entity_id, removed the >, and changed {% %} to {{ }}. Now the automation will show up and it seems to work fine.

1 Like

Hey, regarding that template, Iā€™ve got a grupo called group.baterias. I used your template:

{{ expand('group.baterias') | selectattr('state', 'eq', '76') | list }}

and it returns this:

[<template TemplateState(<state sensor.bateria_pir_barra=76; battery_voltage=2.6, unit_of_measurement=%, friendly_name=PIR Barra power, device_class=battery @ 2021-01-18T07:17:03.627022+01:00>)>]

how am I supposed to extract the entity_id? ā€˜sensor.bateria_pir_barraā€™

Like this I think:

{{ expand('group.baterias')|selectattr('state', 'eq', '76')|map(attribute='entity_id')|list|join(', ') }}
1 Like

One last off-topic, I donā€™t want to PM just for this. Where did you learn all that? thanks, it works fantastic

I leant it hanging around in these forums.

I forgot the map filter

{{ expand('group.baterias') | selectattr('state', 'eq', '76') | map(attribute='entity_id') | list }}

Damn it, somehow the page didnā€™t refresh, Tom was faster xD

2 Likes

Ah sorry. I butchered that. I donā€™t use the short form if/else often and the long form uses {% %}. Glad you got it working.