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?
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.
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.
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’.
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.
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.
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?