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

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

Nope, will do and let you know.
Thanks

Otherwise you may find something helpful in this topic

entity_id: all gives me error:

Invalid config for [sensor.template]: Entity ID all is an invalid entity id for dictionary value @ data['sensors']['number_of_doors_open']['entity_id']. Got 'all'
Entity ID all is an invalid entity id for dictionary value @ data['sensors']['number_of_sensors_on']['entity_id']. Got 'all'. (See ?, line ?). 

Will try the format in the post you linked, although i dont think they are supposed to work with sensors (in the sense of updating automatically)

Here is how i did what i wanted to do, although something slightly different from the counting thingy:

Create groups with the device_class i want, automation to set them up on startup and a binary sensor to show them to me as safety

script:
  create_group_all_motion_sensors:
      sequence:
        - service: group.set
          data_template:
            object_id: "all_sensors_motion"
            entities: >
              {{ states.binary_sensor | selectattr('attributes.device_class','eq','motion') | map(attribute='entity_id') |  join(', ') }}
  
  create_group_all_opening_sensors:
      sequence:
        - service: group.set
          data_template:
            object_id: "all_sensors_opening"
            entities: >
              {{ states.binary_sensor | selectattr('attributes.device_class','eq','opening') | map(attribute='entity_id') | join(', ') }}

automation:
- alias: Startup Stuff - Notifications
    id: cea3ed9689054b0dasdadadsasdw
    trigger:
    - event: start
      platform: homeassistant
    action:
    - data:
        message: HomeAssistant is up
      service: telegram_bot.send_message
#......
    - alias: Create All Groups
      entity_id: script.create_group_all_motion_sensors, script.create_group_all_opening_sensors
      service: script.turn_on

binary_sensor:
#...
  - platform: template
    sensors:
#....
      all_motion_sensors_off:
        friendly_name: "No Motion"
        value_template: "{{ is_state('group.all_sensors_motion', 'on') }}"
        device_class: safety
        
      all_doors_closed:
        friendly_name: "All Doors Closed"
        value_template: "{{ is_state('group.all_sensors_opening', 'on') }}"
        device_class: safety
#had to class them as safety in order to exclude them from the previous group

When at least one motion detector is on:
image
With a lot of help from here


Also, about the instant depiction of the counters (relevant to this thread), If one needs it updated sooner than 1 minute (by sensor.time):

1 Like

Glad that you got it to work :slight_smile: and thanks for sharing your final code! Probably won’t need it, but you never know hehe

Thanks for the help buddy :slight_smile:
@123’s input might be helpful if somebody needs the counters to update every second for example.

1 Like

You can exclude them using the reject filter. For example:

  create_group_all_opening_sensors:
      sequence:
        - service: group.set
          data_template:
            object_id: "all_sensors_opening"
            entities: >
              {{ states.binary_sensor
                 | selectattr('attributes.device_class','eq','opening')
                 | map(attribute='entity_id')
                 | reject('eq', 'binary_sensor.all_door_closed')
                 | join(', ') }}
1 Like

Nice! thanks again buddy!
Where can i find some documentation for those operators btw? in the Jinja docs?
Nvm, found it :slight_smile:

This template works ok:

- platform: template
  sensors:
    lichten_hue_living_count_on:
      friendly_name: Aantal HUE living lampen aan
      value_template: "{{ states | selectattr('entity_id', 'in', state_attr('group.lichten_hue_living', 'entity_id')) |selectattr('state','eq','on') | list | count}}"

But ofcourse, it only update the count if the group changes from off-on. If there is 1 light on, and i turn a second light on, the template is not evaluated again. I could add the sensor.time, but then it updates 1/min. How can I make it update instantaneously without explicitely defining the group members ?

You can write an automation that updates the sensor every time the group changes like this:

- alias: 'group state change update'
  trigger:
    platform: event
    event_type: state_changed
  condition: 
    condition: template
    value_template: >
      {{ trigger.event.data.entity_id in state_attr('group.lichten_hue_living','entity_id') }}
  action:
    service: homeassistant.update_entity
    entity_id: 
      - sensor.lichten_hue_living_count_on

1 Like

Working. Thanks !

I am getting this response since yesterday - and not only to device_class = door

UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘device_class’

on this statement in the template

{{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', 'door') | list | count }}

is it only me or you also got this error?
how can this be solved