Prevent Update to Template Sensor if Referenced Entity Unavalable

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


How do I make sure that

  • (preferably) The template uses the last numeric value of the missing referenced entity
  • (alternatively) The template does not update at all if a referenced entity has a non numeric?

A simplified example is shown below

sensor.office_power_use

{% set printer = states('sensor.socket_printer_energy_total') | float(5) %}
{% set tv = states('sensor.socket_office_tv_energy_total') | float(5) %}

{{ (
   printer
   + tv
   ) | float(1) }}

Worked example
Prior values:

  • sensor.socket_printer_energy_total = 50.00 (kWh)
  • sensor.socket_office_tv_energy_total = 100.00 (kWh)
  • (template sensor) sensor.office_power_use = 150.00 (kWh)

Event

  • sensor.socket_office_tv_energy_total becomes unavailable

Result

  • sensor.socket_printer_energy_total = 50.00 (kWh)
  • sensor.socket_office_tv_energy_total = unavailable
  • (template sensor) sensor.office_power_use = 50.00 (kWh)

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.

For the second, use an availability template: you’ll need to use YAML rather than the UI for this:

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 }}

Wonderful!
Thank you for the quick advice. It makes sense. I will give it a try

Is it possible to get the last known numeric state of one of the individual entities I am using in my sum?

Your suggestion is really useful, but it does mean that if one goes offline, the counter will never increment

something like
{% set tv = states_last_known_valid('sensor.socket_office_tv_energy_total') | float('x') %}

You could make template sensors for each of them that continue to return the last known numeric value.

However, how would you know they had failed?

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!

1 Like