Counter of sensor windows door motion

hi, before update (from 81.1) to 81.6 all works fine, now i have problem:

Template sensor window_count 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.

works before update:

  • platform: template
    sensors:
    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 }}

whats changed?

This was covered in the breaking changes sections. You need to list out all the sensors in order for it to update properly.

They fixed issues with templates. Template <insert component> shouldn’t have been updating without a list of entities to trigger the updates.

To fix your issues, list out the entity_ids that us the door/window device class under the entity_id attribute for your sensors.

did u know how to change this code?
for example:
binary_sensor.door_window_sensor_158d0003
binary_sensor.door_window_sensor_158d0002

like this should work:

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

You’ll need to decide which entities are in the 'window type & which are in the ‘door’ type.

1 Like

Also, apparently there is a new service that can be called:

  • Template sensors for which we could not determine what states to track to re-render will now only be updated when manually refreshed using the new homeassistant.update_entity service. (@balloob - #17276) (sensor.template docs) (breaking change)

last question, how to count other type sensors like switch (sonoff t1) and led (yeelight)? its all light :slight_smile:

as i understand the clou is class type attributes.device_class:

  lights_count:
    entity_id:
      - switch.espurna_adbc_0
      - switch.espurna_adbc_1
      - light.kitchen_led
      - light.gateway_light_78d87df
      - switch.lamp1
    value_template: >-
      {{ states.binary_sensor | selectattr('state', 'eq', 'on') | selectattr('attributes.device_class', 'eq', '???????') | list | count | int }}

You could place all the devices into a group, then use the group as your filter:

{{ states | selectattr('entity_id','in',state_attr('group.mygroup','entity_id')) | selectattr('state','eq','on') | list | count }}

or if you are super lazy and you only have lights and switches, this would work out of the box:

{{ states | selectattr('entity_id','in', state_attr('group.all_lights','entity_id') + state_attr('group.all_switches','entity_id')) |selectattr('state','eq','on') | list | count }}

I used your code and it works great and all lights are counted correctly. Thanks

But now I would like to send the outcome of the sensor to my GIRA KNX Quadclient to use the information in the application as a Kommunication Object (KO) and not as a sensor or switch.

So the question is how can I send the information to the KNX bus of a KO “Number of light on” of a changed value without being it a sensor or switch?

probably should start by looking at the knx integration

thanks. KNX components are already integrated and it works. However for this Question I miss the answer as it is None of the listed KNX components as the sensor (which I created in HA) does not have a KNX comparison. I just want to disclose the same value in my GIRA(KNX) app that I have in the HA app. Maybe it is very simple but I do not get it Right now

I’m not sure, never used KNX.

I would expect it to be this portion of the documentation:

You can use entity_id: [] in your templates to avoid this message.
Check the official doc.

platform: template
  sensors:
    door_count:
      entity_id: []
      value_template: >
         {{ states.binary_sensor | selectattr(‘state’, ‘eq’, ‘on’) | selectattr(‘attributes.device_class’, ‘eq’, ‘door’) | list | count | int }}

Hi, currently using this but it seems to only provide a count of 1, even though my group contains multiple open windows. Any ideas? Thanks in advance!

is the state of these windows ‘on’? or is the state something else?

Yes, the state is ‘on’ when open and ‘off’ when closed. But the sensor only shows ‘1’.

Here is my code.

  group:
    window_sensors:
        name: Windows
        control: hidden
        entities:
          - binary_sensor.0x00158d0002e9e1a9_contact
          - binary_sensor.0x00158d0002c8d6a4_contact
          - binary_sensor.0x00158d0002ea3460_contact
          - binary_sensor.0x00158d0002e9e092_contact
          - binary_sensor.0x00158d0002e9c9ee_contact
          - binary_sensor.0x00158d0002ea9ebf_contact
          - binary_sensor.0x00158d0002ea0766_contact
          - binary_sensor.0x00158d0002e9e2f0_contact
          - binary_sensor.0x00158d0002e9e010_contact

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

sensor is inside the group catagory, you need to make it it’s own catagory. You’ll also need to add all the sensors in the group to the template as a list.

  group:
    window_sensors:
        name: Windows
        control: hidden
        entities:
          - binary_sensor.0x00158d0002e9e1a9_contact
          - binary_sensor.0x00158d0002c8d6a4_contact
          - binary_sensor.0x00158d0002ea3460_contact
          - binary_sensor.0x00158d0002e9e092_contact
          - binary_sensor.0x00158d0002e9c9ee_contact
          - binary_sensor.0x00158d0002ea9ebf_contact
          - binary_sensor.0x00158d0002ea0766_contact
          - binary_sensor.0x00158d0002e9e2f0_contact
          - binary_sensor.0x00158d0002e9e010_contact

  sensor:
    - platform: template
      sensors:
        window_count:
          entity_id:
            - binary_sensor.0x00158d0002e9e1a9_contact
            - binary_sensor.0x00158d0002c8d6a4_contact
            - binary_sensor.0x00158d0002ea3460_contact
            - binary_sensor.0x00158d0002e9e092_contact
            - binary_sensor.0x00158d0002e9c9ee_contact
            - binary_sensor.0x00158d0002ea9ebf_contact
            - binary_sensor.0x00158d0002ea0766_contact
            - binary_sensor.0x00158d0002e9e2f0_contact
            - binary_sensor.0x00158d0002e9e010_contact
          value_template: >-
             {{ states | selectattr('entity_id','in',state_attr('group.window_sensors','entity_id')) | selectattr('state','eq','on') | list | count }}

Sorry my indentation was off earlier. So I still have to retain a list of entity_id’s within the template sensor? I thought the group effectively became the list with this approach?

Here is my full code

## Groups

group:
  alarm:
    name: Home Alarm
    control: hidden
    entities:
      - alarm_control_panel.home_alarm
      - sensor.door_count
      - sensor.window_count
      - sensor.alarm_panel_online

  motion_sensors:
    name: Motion
    control: hidden
    entities:
      - binary_sensor.entry_motion
      - binary_sensor.0x00158d00020b767f_occupancy
      - binary_sensor.0x00158d00022809ec_occupancy
      - binary_sensor.living_room_motion
      - binary_sensor.0x00158d00022809d8_occupancy
      - binary_sensor.0x00158d000223ed18_occupancy
      - binary_sensor.0x00158d00022809cc_occupancy
      - binary_sensor.0x00158d00020b76b1_occupancy
      - binary_sensor.master_bedroom_motion

  door_sensors:
    name: Doors
    control: hidden
    entities:
      - binary_sensor.garage_side_entry_door
      - binary_sensor.0x00158d000239f6bd_contact
      - binary_sensor.0x00158d00023dbef6_contact
      - binary_sensor.0x00158d000239f6ee_contact
      - binary_sensor.0x00158d000239cb52_contact

  window_sensors:
    name: Windows
    control: hidden
    entities:
      - binary_sensor.0x00158d0002e9e1a9_contact
      - binary_sensor.0x00158d0002c8d6a4_contact
      - binary_sensor.0x00158d0002ea3460_contact
      - binary_sensor.0x00158d0002e9e092_contact
      - binary_sensor.0x00158d0002e9c9ee_contact
      - binary_sensor.0x00158d0002ea9ebf_contact
      - binary_sensor.0x00158d0002ea0766_contact
      - binary_sensor.0x00158d0002e9e2f0_contact
      - binary_sensor.0x00158d0002e9e010_contact

## Sensors

sensor:
  - 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 }}
          #  {{ states | selectattr('entity_id','in',state_attr('group.door_sensors','entity_id')) | selectattr('state','eq','on') | list | count | int}}
        # entity_id:
        #   - binary_sensor.0x00158d000239cb52_contact
        #   - binary_sensor.0x00158d000239f6bd_contact
        #   - binary_sensor.0x00158d000239f6ee_contact
        #   - binary_sensor.0x00158d00023dbef6_contact
        #   - binary_sensor.garage_side_entry_door
      window_count:
        value_template: >-
           {{ states | selectattr('entity_id','in',state_attr('group.window_sensors','entity_id')) | selectattr('state','eq','on') | list | count }}
          #  {{ states | selectattr('entity_id','in',state_attr('group.window_sensors','entity_id')) | selectattr('state','eq','on') | list | count | int}}
        # entity_id:
        #   - binary_sensor.0x00158d0002e9e1a9_contact
        #   - binary_sensor.0x00158d0002c8d6a4_contact
        #   - binary_sensor.0x00158d0002ea3460_contact
        #   - binary_sensor.0x00158d0002e9e092_contact
        #   - binary_sensor.0x00158d0002e9c9ee_contact
        #   - binary_sensor.0x00158d0002ea9ebf_contact
        #   - binary_sensor.0x00158d0002ea0766_contact
        #   - binary_sensor.0x00158d0002e9e2f0_contact
        #   - binary_sensor.0x00158d0002e9e010_contact
      alarm_panel_online:
        friendly_name: 'Alarm Panel'
        value_template: "{% if states.device_tracker.alarm_panel %}
          {% if is_state('device_tracker.alarm_panel', 'home') %}
            Online
          {% else %}
            Offline
          {% endif %}
          {% else %}
            Unknown
          {% endif %}"

Yes, otherwise the sensor will never update because it has nothing to update against. Template sensors update when reference entities update. This only has 1, the group. The group will only update when switching to from off to on or on to off. That does not always occur because if one item is on and another turns on, the groups state does not change.

1 Like