Custom Template help needed

Can someone explain why this returns the list as a string and not as true list, or how to convert it to a true list.

{% macro list_entities_on(type, device_class='', state='on', select='', reject='', area='', exclude_area='') %}
{% set ns = namespace(sensors=[]) %}
{% if device_class and area %}
  {% for entity in type | selectattr('attributes.device_class', 'eq', device_class) | selectattr('entity_id', 'in', area_entities(area)) | selectattr('state','eq',state) %}
    {% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
  {% endfor %}
{% elif device_class %}
  {% for entity in type | selectattr('attributes.device_class', 'eq', device_class) | selectattr('state','eq',state) %}
    {% set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
  {% endfor %}
{% endif %}
{{ ns.sensors }}
{% endmacro %}

this returns

but if I try to count the items in the list it returns 704, instead of 7

Is this a bug in the core custom template ?

All macros return strings. You can alter your macro to return a json formatted string using to_json, then use the from_json filter on your function call:

{%- macro list_entities_on(type, device_class='', state='on', select='', reject='', area='', exclude_area='') %}
{%- set ns = namespace(sensors=[]) %}
{%- if device_class and area %}
  {%- for entity in type 
  | selectattr('attributes.device_class', 'eq', device_class) 
  | selectattr('entity_id', 'in', area_entities(area)) |
  selectattr('state','eq',state) %}
    {%- set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
  {%- endfor %}
{%- elif device_class %}
  {%- for entity in type 
  | selectattr('attributes.device_class', 'eq', device_class) 
  | selectattr('state','eq',state) %}
    {%- set ns.sensors = ns.sensors + [dict(entity_id = entity.entity_id, name = entity.name, state = entity.state)] %}
  {%- endfor %}
{%- endif %}
{{- ns.sensors|to_json }}
{%- endmacro %}

{{(list_entities_on(states.binary_sensor, device_class='connectivity')|from_json) }}



1 Like

thanks, got it working

{% macro count_entities(type, device_class='', state='', select='', reject='', area='', exclude_area='') %}
{% from 'tools.jinja' import list_entities %}
{{ list_entities(type,device_class,state,select,reject,area,exclude_area) | from_json | count }}
{% endmacro %}

and with my template sensors

    - name: "Number of Sockets On"
      unique_id: c838cee8-1919-4000-8fc0-d6587433e6fc
      attributes:
        navigation: sockets
        label: >
          {% if this.state not in ['unavailable','unknown'] %}
            {% if this.state | int > 0 %}
            {{ this.state }} On
            {% else %}
            All Off
            {% endif %}
          {% else %}
          All Off
          {% endif %}
        entities: >
          {% from 'tools.jinja' import list_entities %}
          {{ list_entities(states.switch,device_class='outlet') }}
        count: >
          {% from 'tools.jinja' import count_entities %}
          {% set ns = namespace(entities=[]) %}
          {% for s in states.switch | map(attribute='entity_id') | map('area_name') | unique | reject('none') | list %}
            {% set ns.entities = ns.entities + [ (s, count_entities(states.switch,device_class='outlet',state='on',area=s) | from_json )] %}
          {% endfor %}
          {{ dict.from_keys(ns.entities) }}
      state: "{{ this.attributes.entities | count }}"
      icon: mdi:power-socket-uk

for for the button

  - type: custom:mushroom-chips-card
    chips:
      - type: template
        icon: mdi:power-socket-uk
        content: "{{ state_attr('sensor.number_of_sockets_on', 'count')['Office'] }}"
      - type: template
        icon: mdi:lightbulb
        content: "{{ state_attr('sensor.number_of_lights_on', 'count')['Office'] }}"