Template unknown

I have sensors defined in a templates.yaml file. I have used the same code for listing windows that are open and doors that are open. The sensor for windows works fine and yet the sensor for doors returns ‘unknown’ in the Developer Tools template window. Plus in Settings > Entities the name of the sensor does not display the correct unique id, name or icon.

I have renamed and changed the unique id of the sensors. But I have deleted the entities, purged them from the recorder, reloaded YAML configuration/template entities and restarted Home Assistant multiple times, plus deleted the code from the templates.yaml file and repeated all of the above.

The code is identical other than the sensor name, unique id, icon and a line to select the device class’, see below. Can anyone help with this?

#List Windows Open#
  - name: "List Windows Open"
    unique_id: list_windows_open
    icon: mdi:window-open
    state: >
      {%- set ns = namespace(sensors=[]) -%}
      {%- set ns.sensors = states.binary_sensor
        | selectattr('attributes.device_class', 'defined')
        | selectattr('attributes.device_class', '==', 'window')
        | selectattr('state', 'eq', 'on')
        | map(attribute='name') | list -%}
      {% if ns.sensors | count > 0 | int %}
        {{ ns.sensors | list | join('\n') }}
      {% else %}
        {{ 'All windows are shut or sensors are unavailable' }}
      {% endif %}

  #List Doors Open#
  - name: "List Doors Open"
    unique_id: list_doors_open
    icon: mdi:door-open 
    state: >
      {%- set ns = namespace(sensors=[]) -%}
      {%- set ns.sensors = states.binary_sensor
        | selectattr('attributes.device_class', 'defined')
        | selectattr('attributes.device_class', '==', 'door')
        | selectattr('state', 'eq', 'on')
        | map(attribute='name') | list -%}
      {% if ns.sensors | count > 0 | int %}
        {{ ns.sensors | list | join('\n') }}
      {% else %}
        {{ 'All doors are shut or sensors are unavailable' }}
      {% endif %}

Try this template in the Template Editor and let me know if it also reports unknown (or it works correctly).

{% set doors = states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', '==', 'door')
  | selectattr('state', 'eq', 'on')
  | map(attribute='name') | list %}
{{ doors | join('\n') if doors | count > 0 else
   'All doors are shut or sensors are unavailable' }}
1 Like

The code looks fine and works here, although there is no need to use a namespace (see Taras’s rewrite). Your code from above included in my config, completely unchanged except for matching indent level with my setup (where there are no door or window sensors):

My guess is that you have enough doors open that the list exceeds the 255-character limit of the sensor state; there should be a related error in the logs.

Close some doors and it should start working :slight_smile: . If it does, you can turn the state into a count, and move the list of names into an attribute, which has a much higher size limit.

You might also find your window version fails if you open a lot of windows.

1 Like

Following Troon’s line of reasoning… if you want to keep the state as close to what you already have, you can use truncate() to limit the number of characters that are output and avoid the issue. In 123’s template from above this would look like:

{% set doors = states.binary_sensor
  | selectattr('attributes.device_class', 'defined')
  | selectattr('attributes.device_class', '==', 'door')
  | selectattr('state', 'eq', 'on')
  | map(attribute='name') | list %}
{{ doors | join('\n') | truncate(254) if doors | count > 0 else
   'All doors are shut or sensors are unavailable' }}
1 Like

FWIW, I have similar Template Sensors that report open doors, unlocked locks, etc. However, due to the 255 character limitation of the state property, I use attributes to store the list of names (it can hold about 16K). I use state simply to report the quantity of open doors.

1 Like

Thank you Taras, your improved code works perfectly in the Template Editor, but not when I added it to the YAML file. Troon’s guess is correct, there were a lot of doors open, which must have exceeded the 255 character limit for the state. Once I went around and closed all but a couple of doors it worked. Thank you for the improved code though. I have a steep learning curve ahead of me, so I really appreciate the help!

Thanks Troon,

Your guess is correct, I closed all but 2 doors and it worked. I would have never have thought of that, you have literally saved my sanity!

1 Like

You’re welcome! If you now use it the way I had suggested, you can mitigate the 255 character limit.

I have changed it to what you recommended and have assigned the count to the state and created a attribute to store the sensor names. It works perfectly thank you!

I have tried to reuse the “doors” variable for the attribute, but I presume the scope of the variable is only within the sensor block of code. So I assume the only way is to repeat the code and filter to assing the names of the sensors to the attribute?

#Doors Open#
  - name: "Doors Open"
    unique_id: doors_open
    icon: mdi:door-open 
    state: >
      {% set doors = states.binary_sensor
        | selectattr('attributes.device_class', 'defined')
        | selectattr('attributes.device_class', '==', 'door')
        | selectattr('state', 'eq', 'on')
        | map(attribute='name') | list %}
      {{ doors | list | count if doors | count > 0 else
        'All doors are shut or sensors are unavailable' }}
    attributes: 
      doors_open: > 
        {% set doors = states.binary_sensor
          | selectattr('attributes.device_class', 'defined')
          | selectattr('attributes.device_class', '==', 'door')
          | selectattr('state', 'eq', 'on')
          | map(attribute='name') | list %}
        {{ doors | join('\n') if doors | count > 0 else
          'All doors are shut or sensors are unavailable' }}

You’re welcome!