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

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

got it.
include selectattr('attributes.device_class', 'defined') in the statement.

Hello guys,
I am new to Home Assistant and just configuring my Dashboard.

I hope I did not get it wrong; but in order to add my sensors to a class, I need to create a new file called customize.yaml in home assistant/config, right?

Then paste the following code in customize.yaml and add my sensors:

#################################################################
#                                                               #
#                          Customize                            #
#                                                               #
#################################################################
# In this section you will customize your sensors
    binary_sensor.door_window_sensor_158d00024e17b9:
      friendly_name: Sliding Door
      device_class: door

Then add this code snippet to configuration.yaml:

homeassistant:
  customize: !include customize.yaml

and also the following code:

#################################################################
#                                                               #
#                      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 }}

Right now, when I select the entity “door_count” or “window_count” in my dashboard’s entity widget, it always displays all open windows/doors and not only the ones I defined in. What did I do wrong?

For whatever reason the customizations will only adopted when the concerned entities have been updated (in your case: Close those open doors).
Another reason could be that your other doors also have the same device class.

Not sure if anyone can help as I am a newbie to templating. I am trying to put both the ‘motion’ and ‘occupancy’ in one line but not able to do it. Ended up having to do an addition. :frowning:

Thanks.

    total_motion_on:
      friendly_name: "Total motion on"
      value_template: >
        {{ states.binary_sensor 
           | selectattr('state', 'eq', 'on')
           | selectattr('attributes.device_class', 'eq', 'motion')
           | list | count
           +
           states.binary_sensor 
           | selectattr('state', 'eq', 'on')
           | selectattr('attributes.device_class', 'eq', 'occupancy')
           | list | count }}

Ok…I managed to find the solution. :slight_smile:

    total_motion_on:
      friendly_name: "Total motion on"
      value_template: >
        {{ states.binary_sensor 
           | selectattr('state', 'eq', 'on')
           | selectattr('attributes.device_class', 'in', 'motion, occupancy')
           | list | count }}
2 Likes