For many template sensors (used for statistics or the energy dashboard) it is important to avoid their values falling to zero, especially/e. g. if this is not possible in reality (e. g. because it’s a total_increase sensor or whatever).
While I managed to set up percentage sensors correctly (they do basic calculations - basically transferring absolute sensor values to percentage values), I struggle with others and I’m wondering what’s the “one size fits all” solution, the gold standard of achieving this.
E. g. this one is based on a group. On every reload of groups this sensor falls to zero…
…even I meanwhile have double and triple checks to avoid this aaaaand an availability template:
template:
- sensor:
- name: Problematische Entitäten (ignorierte) Anzahl
# entity_id: problems_any_ignored_count
unique_id: xxxxxx-something-xxxxxx
state_class: total
icon: mdi:numeric
#state: "{{ expand(states.group.problems_any_ignored) | length }}"
state: >-
{% if (expand(states.group.problems_any_ignored) | length) | int(0) == 0 %}
{% else %}
{% if (expand(states.group.problems_any_ignored) | length) | int(0) > 0 %}
{{ expand(states.group.problems_any_ignored) | length }}
{% endif %}
{% endif %}
availability: "{{ (expand(states.group.problems_any_ignored) | length) | int(0) > 0) }}"
…but still, every group reload (which is just ONE but a nice trigger cause I can provocate it nicely) gives this in the database (states
table) and of course renders graphs unusable:
state_id domain entity_id state attributes event_id last_changed last_updated created old_state_id attributes_id
65885671 NULL sensor.problems_any_ignored_count 31 NULL 67523426 2022-06-01 00:09:12.928600 2022-06-01 00:09:12.928600 NULL 65885652 478
65885652 NULL sensor.problems_any_ignored_count 0 NULL 67523407 2022-06-01 00:09:12.409461 2022-06-01 00:09:12.409461 NULL 65885219 478
What’s wrong here? Maybe the default of the int (int(0)
)?
But I’m not into a solution for this specific issue. I’m interested in general:
how do you template your sensors when it’s important to avoid them switching to zero?