Help with template sensor (count)

I have a couple of issues with templating. I keep writing templates that work absolutely fine when looked at via the gui… but then return nothing when in my config. Example below… returns zero when in config as a template sensor… same template when viewed on the templates page in the gui works. What am I missing in my YAML?

  - platform: template
    sensors:
      occupied_count:
        friendly_name: "Number of Occupied Rooms"
        value_template: >
          {{ states | selectattr('entity_id','in', ['binary_sensor.kitchen_occupied','binary_sensor.living_room_occupied','binary_sensor.conservatory_occupied', 'binary_sensor.upstairs_hall_occupied', 'binary_sensor.bathroom_occupied','binary_sensor.bedroom_occupied'] ) |selectattr('state','eq','on') | list | count }}

Thanks.

Do you see a warning in the logs over these templates?

Ah, breaking change from a while back. I need to list entity ids to trigger it in the yaml it seems.

Yes, or call homeassistant.update to force an update of those sensors.

This template should work fine. You are listing all the entity_id’s in the templates so you shouldn’t need to add the entity_id attribute to the sensor itself and the breaking changes shouldn’t affect you.

Are you saying that it doesn’t count or doesn’t return an answer? Doesn’t count would mean it’s always zero, doesn’t return an answer would be no state whatsoever or some cryptic ‘unknown’.

As a side note, this will also only update and perform a count when one of said sensors changes states. It will not be a live tracking at all times kind of sensor. If you want it to update every minute or so, you can add sensor.time or sun.sun as the entity_id and it will update minutely.

I carried out a test and can confirm that by putting the entities in an array, Home Assistant cannot automatically select them for monitoring.

This version does not work:

  - platform: template
    sensors:
      lights_activated:
        value_template: >
          {{ states | selectattr('entity_id','in', ['light.family', 'light.kitchen', 'light.foyer'] ) |selectattr('state','eq','on') | list | count }}

This version works:

  - platform: template
    sensors:
      lights_activated:
        entity_id:
          - light.kitchen
          - light.family
          - light.foyer
        value_template: >
          {{ states | selectattr('entity_id','in', ['light.family', 'light.kitchen', 'light.foyer'] ) |selectattr('state','eq','on') | list | count }}

They must have removed the searching for entity_id’s without states or state_attr.

If that’s the case the best solution would be to just list it once and be done with it

  - platform: template
    sensors:
      lights_activated:
        value_template: >
          {{ [states('light.family'), states('light.kitchen'), states('light.foyer')] | select('on') | list | count }}
1 Like

My template sensor for counting all movement sensors which state is ‘on’ worked until I updated to HA 0.97.2. Now it reports ‘Unknown’ whenever at least one sensor is ‘on’.

  - platform: template
    sensors:
      pir_sensors_on:
        entity_id:
          - binary_sensor.sensor1
          - binary_sensor.sensor2
          - binary_sensor.sensor3
          - ...
        value_template: >-
          {% if states('group.MySensors') == 'on' %}
            {%- for item in states.group.MySensors.attributes.entity_id if (states(item) == 'on') %}
              {% if loop.last %}
                {{ loop.index }}
              {% endif %}
            {% endfor %}
          {% else %}
            0
          {% endif %}

Any hints?

Thanks!

This error shows up in my log:

Log Details (ERROR)

Sun Aug 25 2019 18:48:44 GMT+0200 (CEST)

Could not render template Movement: str: Invalid entity ID ‘group.MySensors’

For anybody who discovers this thread in the future, we just discussed this in Discord and the problem here was:

Invalid entity ID ‘group.MySensors’

Home Assistant only uses lower case letters for entities, and group.MySensors isn’t valid.

2 Likes

This is my understanding as well which makes it difficult to understand how this statement could be true:

Were upper-case letters ever permitted for entities? If so, it must be many versions ago …

Tinkerer,

please excuse me not being fast enough to add the solution for myself.

Thanks again for your hint!
mny

Hm, maybe 0.92 was less strict here?

So five versions ago.

It was. I believe that change went in around 93 to 94ish.