Counting Open Doors & Motion Sensors? (Solved by ranting at myself)

Afternoon All,

Right so I have some door sensors and motion sensors and I’d like to create a template sensor to count how many are open / have detected motion. I’d also like to differentiate between door and window if possible.

Searching through the forums I have started with this (as a template sensor):

door_count:
  value_template: >-
    {{ states.binary_sensor | selectattr('state', 'eq', 'on') | list | count }}

Obviously this will account for all binary sensors, how do I split this out to meet the above requirements?

I have set the following 3 device classes which I hope can be used to get the counts:

  • door
  • window
  • motion
2 Likes

Right just tried this to no avail however I feel as if I’m moving in the right direction (could be completely wrong though).

door_count:
  value_template: >-
    {{ states.binary_sensor | selectattr('state', 'eq', 'on') | rejectattr('attributes.device_class', 'eq', 'window') | rejectattr('attributes.device_class', 'eq', 'motion') | list | count }}

Okay, so they seem to be working but the default value for all 3 is “1”. So the totals are always one more than the actual amount of sensors that are in the “on” state. Any ideas?

And… for anyone who needs this in the future, I just subtracted one in the end to get the desired amount:

motion_count:
  value_template: >-
    {{ states.binary_sensor | selectattr('state', 'eq', 'on') | rejectattr('attributes.device_class', 'eq', 'door') | rejectattr('attributes.device_class', 'eq', 'window') | list | count -1 }}

No idea why there is an additional value on each but this has sorted my issue. If anyone knows why and cares to explain so as I can implement this correctly it would be appreciated.

1 Like

hi @Knottyboy thanks for the tip.
Can u share your yaml with all config?

Yeah of course:

- platform: template
  sensors:
    door_count:
      value_template: >-
        {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'door') | list | count }}
    window_count:
      value_template: >-
        {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'window') | list | count }}
    motion_count:
      value_template: >-
        {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'motion') | list | count }}

I did have to set the device class in my customize.yaml (so the above picks it correctly).

10 Likes

Did you ever figure this out?

1 Like

This did it for me:

- platform: template
    sensors:
      door_count:
        value_template: >-
           {{ states | selectattr('entity_id','in',state_attr('group.door_sensors','entity_id')) | selectattr('state','eq','on') | list | count | int}}
      window_count:
        value_template: >-
           {{ states | selectattr('entity_id','in',state_attr('group.window_sensors','entity_id')) | selectattr('state','eq','on') | list | count | int}}
2 Likes

See if this helps too explain a little of what is going on in the code. I am building this for my house alarm to see if any windows or doors are open so all this is in my ha_alarm.yaml file I created.

#################################################################
#                                                               #
#                          Customize                            #
#                                                               #
#################################################################
# In this section you will customize your sensors
homeassistant:
  customize:
    binary_sensor.door_window_sensor_158d00024e17b9:
      friendly_name: Sliding Door
# You set the device class for your sensor here so you can reference in door_count below
      device_class: door
    binary_sensor.door_window_sensor_158d0002768916:
      friendly_name: Front Door
      device_class: window
# Keep listings your sensors and giving them a door or window class
#################################################################
#                                                               #
#                      Template Sensors                         #
#                                                               #
#################################################################
# Now to start counting the ones that are ON by referencing their state as seen in the developer tools area STATES we are also using the template platform to create sensor.door_count and sensor.window_count which will become entities that you can add to your front end.
sensor:
  - platform: template
    sensors:
# Lets count doors that are open! So from the states of all binary sensors select states that are equal to 'on' and that have a door class of 'door' which we set above list them and then count how many.
      door_count:
        value_template: >-
           {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'door') | list | count | int }}
      window_count:
        value_template: >-
           {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'window') | list | count | int }}

Thanks, I got the code but i think the binary_sensor count being a device_class of door was adding to the number (but not actually being a door or window) and so my increment was always one more. I’ve fixed this using my door and window groups instead.

Oh cool I will have play around too with your method.

It’s pretty much the same except it insists on performing the count of entity_id’s within the define group instead of going by all binary_sensors with device_class ‘door’ or ‘window’.

Works for me I’ve used your code


Simple entities card


Node red showing count too

1 Like

Hey there,
I’m trying to implement your code, but due to the fact that i wont declare an entity_id for HA to watch the sensors wont update. Is there any workaround on that? (something like entity_id: binary_sensor.*)

Here is my code:

      number_of_doors_open:
        value_template: "{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'opening') | list | count | int }}"           
      number_of_sensors_on:
        value_template: "{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'motion') | list | count | int }}"

It only updates once on startup and then gives me this warning:

2020-01-17 08:38:11 WARNING (MainThread) [homeassistant.components.template] Template sensor 'number_of_doors_open' has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.
2020-01-17 08:38:11 WARNING (MainThread) [homeassistant.components.template] Template sensor 'number_of_sensors_on' has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

How do i address this?

You should be able to add the below to each sensor for frequent update of the sensor.

entity_id: sensor.time

And of course you need to have the time sensor

number_of_doors_open:
  entity_id: sensor.time
  value_template: "{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'opening') | list | count | int }}"           
number_of_sensors_on:
  entity_id: sensor.time
  value_template: "{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'motion') | list | count | int }}"

Tried with the .all statement as per the latest documentation but it didn’t work.

      number_of_sensors_on:
        entity_id: binary_sensor.all
        value_template: "{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'motion') | list | count | int }}"

Will try with time and report back, Thanks!

It does work with sensor.time but, it does not update instantly.
It looks like it updates every minute?
Any other ideas to make it more responsive?

Yes you are right, it updates every minute as this is the frequency that the sensor.time gets updated. I’m sorry, I don’t know of any other solution except for hardcoding all the binary sensors. There is a feature request to add force_update to template sensors.

Thanks for the answer,

Shouldn’t entity_id: binary_sensor.all work instantly though?

Found it in the documentation of the last update:
" The automation and script configuration panels are updated to show and manage all automations/scripts. If you want to show cards in your Lovelace UI with all entities for a single domain, use the auto-entities card. If you want to target all entities in a service call, use all as value for entity_id ."

Is that what it’s talking about or am I missing something?

Or does it only refer to the group.all… groups that were discontinued?

group.all_automations
group.calendar
group.all_covers
group.all_devices
group.all_fans
group.all_lights
group.all_locks
group.all_plants
group.remember_the_milk_accounts (???)
group.all_remotes
group.all_scripts
group.all_switches
group.all_vacuum_cleaners

Haven’t read the release notes for the latest release yet.
Did you try this as well?

entity_id: all