Following up on a very comprehensive answer by @Didgeridrew to a question on a problem of mine, I stumbled upon another problem
I ended up with the following script to count the number of lights on and off in an area
mixed_switch:
sequence:
- alias: set variables
variables:
lights: >
{{ expand(area_entities("salon_centre")) | selectattr('domain', 'eq', 'switch')| list }}
count_on: "{{ lights | selectattr('state','eq', 'on') | list | count }}"
count_off: "{{ lights | selectattr('state','eq', 'off') | list | count }}"
light_action: "{{ iif( count_on >= count_off, 'off', 'on' ) }}"
- alias: debug variables
service: persistent_notification.create
data:
message: "{{lights}} {{count_on}} {{count_off}} {{light_action}}"
title: "debug"
- alias: turn all lamps on or off
service: homeassistant.turn_{{ light_action }}
target:
area_id: salon_centre
The output of the “debug” section is as follows (I reformatted for clarity)
[
<template TemplateState(<state switch.salon_blanche_entree_gauche=off; device_class=outlet, icon=mdi:floor-lamp, friendly_name=blanche entrée gauche @ 2023-01-29T15:28:10.498726+01:00>)>,
<template TemplateState(<state switch.salon_lampe_haute=off; icon=mdi:lamp, friendly_name=salon lampe haute @ 2023-01-29T15:15:40.553449+01:00>)>,
<template TemplateState(<state switch.salon_tableau=off; device_class=outlet, friendly_name=salon_tableau @ 2023-01-29T15:26:03.766786+01:00>)>
]
0 0 off
The first list is correct - I put these three switched in the test area salon_centre
. They are all switch
and all are off.
{{count_off}}
should therefore be equal to 3
. If I understand the sequence correctly (“Match entries where state
is off
, list them, then count the elements”) then it seems to be fine.
Would you have any ideas why it is not?