Template with AND of multiple entities

Hi,
Can someone send send me an example of a binary template, that will be ‘off’ when all items in this template are all ‘off’ and ‘on’ when all items are ‘on’?

I managed to create a similar template with OR, but I am stuck with the AND.

EDIT 1 with more details.

I have the following template, which returns OFF when one of the sensors goes offline.
I would like a template to return OFF when all sensors goes OFFLINE.

  - platform: template
    sensors:
      devices_status:
        friendly_name: 'Devices Status'
        device_class: connectivity
        value_template: >
          {% set sensors = [states.binary_sensor.living_room_wifi,
                            states.binary_sensor.open_plan_speaker] %}
          {{ sensors | selectattr('state','eq','off') | list | count == 0 }}

EDIT 2 with solution

group:
  power_status:
    name: Power Status
    entities:
      - binary_sensor.living_room_wifi
      - binary_sensor.open_plan_speaker

Going to need more info here. What do you mean ‘all items in this template’?

you are right petro … main post edited

change the last line to

{{ sensors | selectattr('state','eq','off') | list | count == sensors | length }}

thanks will test it out this evening … can you explain what is the difference between count ==0 and length?

this is counting how many sensors in sensor are off.

this is giving you a total number of sensors

1 Like

thank … will test it and update post accordingly

just tested the logic, and when the devices are “on”, the template is giving “off”
shall I change this:

{{ sensors | selectattr('state','eq','off') | list | count == sensors | length }}

to

{{ sensors | selectattr('state','eq','on') | list | count == sensors | length }}

?

but that’s opposite logic of what you requested.

EDIT: you’re wording is odd.

if they are all off you want the sensor to be off, which means you want to display on when any are on. Meaning you just want a group and you don’t need a template.

but if you want a template…

{{ sensors | selectattr('state','eq','on') | list | count > 0 }}
1 Like

my wording infact is wrong not odd :slight_smile:

if I combine the entities in a group, will the output be ANDed?

That’s the thing, anded or or’d is overcomplicating what you want. You just want it to be off when they are all off. Plain and simple. That’s how groups naturally work but if they are sensors it may not work because sensors don’t typically have on/off states. Either way, a groups default behavior is that the group is on if any of the entities inside are on. Same goes for the template above

tried with the following:

      power_status:
        friendly_name: 'Power Status'
        device_class: power
        value_template: >
          {% set sensors = [states.binary_sensor.plants_sensor,
                            states.binary_sensor.living_room_tv] %}
          {{ sensors | selectattr('state','eq','on') | list | count > 0 }}

while having:
binary_sensor.plants_sensor == off and binary_sensor.living_room_tv == off, the template is still returning on. Even when one of them (or both) are on. Will try to use the group option.

Works for me, not sure what’s going on for you. FYI you can’t just set the states of the devices and expect them to stick. So if that’s what you’re doing, that won’t work. You have to wait until they go off naturally.