I have a template sensor to sum the values of several (7 at present) total_increasing sensors.
When one of these is unavailable it seems to be considered as zero.
This leads to wildly inaccurate charts and energy measurements as pictured
For the first question, use something like this as the state template:
{% set printer = states('sensor.socket_printer_energy_total') | float('x') %}
{% set tv = states('sensor.socket_office_tv_energy_total') | float('x') %}
{% if printer is number and tv is number %}
{{ printer + tv }}
{% else %}
{{ this.state }}
{% endif %}
I’m only returning the calculation if both parts are numeric, but you can work with either if you want. The this variable is documented here.
availability: >
{% set printer = states('sensor.socket_printer_energy_total') | float('x') %}
{% set tv = states('sensor.socket_office_tv_energy_total') | float('x') %}
{{ printer is number and tv is number }}
You could make template sensors for each of them that continue to return the last known numeric value.
That makes sense. Thank you once again
However, how would you know they had failed?
In this scenario I am taking data from energy monitoring power sockets. The most common reason for them to report “unavailable” or similar is that I have unplugged them.
It is more important to me to know the overall usage of the collection of devices.
I recognise that this assumes an “unavailable” device is not drawing power which may not be the case. It may have lost it’s WiFi, for example.
I intend to create a dashboard showing the state of each monitoring device so will consider failures separately from the collection of overall power draw data.
EDIT: I have tested my changed template as you advised above and unplugged a few devices. It works well, no spikes to my collected data. Marked as solved.
Thanks!