So after looking through some configs on Github for inspiration, I found some logic that gets the count for the number of devices currently on my home. For example, I have a lights count that loops through all of the devices and it returns a number:
# Lights Count
lights_count:
entity_id: []
value_template: >-
{%- set domains = ['light'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
Currently it returns a 4 for the lights.
However, when I try to do the same for my zwave devices, it doesn’t return a value at all.
# Z-Wave Devices Count
zwave_count:
entity_id: []
value_template: >-
{%- set domains = ['zwave'] -%}
{%- for domain in domains -%}
{%- for item in states[domain] -%}
{% if loop.first %}
{{loop.length}}
{% endif %}
{%- endfor -%}
{%- endfor -%}
What is strange is that when I go into Developer Tools and use the Template editor, it returns a 6. But the value of the sensor (sensor.zwave_count) is blank in the States tab. Does anyone know why that would be the case?
Probably because those entities don’t exist when the template is first evaluated. As you defined these template sensors, they will only update at startup, since there are no entities discovered to monitor for state changes.
BTW, that is a very complicated way to get the count. This would be much simpler:
Thanks Phil for the infomation. That does make sense. I was able to use your code and you’re right, that way does look complicated and I like your way much better!
As for the zwave devices, since as you mentioned that those entities don’t exist when the template is first evaluated, does that mean I cannot use the {{ states.zwave|count }} the same way as the other one? Both are defined in my sensors.yaml file. I am getting a return value of 0 when I load Home Assistant.
It means you have to come up with some way to get the sensor to re-evaluate it’s template once all the zwave entities exist. There are ways to do that. But one has to wonder, why have these sensors at all? Does your system change that much?
Okay thanks. I’ll see if I can figure out an alternative.
I really just wanted to create those sensors for informative purposes. Just to see how many automations, binary_sensors, lights, and zwave devices I am currently using on a view.