Getting a value spike on a template sensor. How do I prevent it?

I created a “Delta” template sensor to work out a temperature difference between a single sensor and a Min/Max Helper created for an average to measure against, but I noticed that the results spiked every time HA is restarted.

To start, I created the Helper to get an average value from selected nearby rooms.

Then I created a template sensor to calculate the delta between the Helper value and a weather sensor on my (dumb) boiler. This is set up so I can double check that it’s been switched on by our switchbot switch and that we have hot water ready for use.

Typically the delta value will range from -1 to +15 depending on the average ambient and if the boiler is generating heat

But it looks like the helper or template isn’t starting in time, so the calculation returns the full value of the Boiler Temperature as there’s no Helper number available to calculate against for a few seconds.

This results in false spikes in the Lovelace card (heating’s been off for a few hours):
image

I tried changing the template sensor to default as 0 if the helper or isn’t available or the helper returns a 0, but that hasn’t worked.
Has anyone else had this kind of issue and needed to ignore or set a default value until the true value is returned?

Template as it is atm:

# Boiler Room Delta
- sensor:
    - name: boiler_room_temperature_delta
      unique_id: ddd99cd0-ddd99cd0-ddd99cd0-ddd99cd0
      unit_of_measurement: °C
      icon: mdi:thermometer
      state: >
        {% if is_state('sensor.home_temperature', '0') or is_state('sensor.boiler_room_temperature', '0') or is_state('sensor.home_temperature', '0.0') or is_state('sensor.boiler_room_temperature', '0.0') %}
        0
        {% else %}
        {{ (((states.sensor.boiler_room_temperature.state) |float (0)) - ((states.sensor.home_temperature.state) | float (0))) | round(1) }}
        {% endif %}

As a test I used one of the hallway sensors in the template and restarted.

No spike, so it’s definitely caused by the Helper

The first step is to set an availability. Your example actually creates false values when the component entities are “unknown” or “unavailable”. A properly set availability will keep the values from being recorded while the component entities are in those states.

# Boiler Room Delta
- sensor:
    - name: boiler_room_temperature_delta
      unique_id: ddd99cd0-ddd99cd0-ddd99cd0-ddd99cd0
      unit_of_measurement: °C
      icon: mdi:thermometer
      state: >
        {% set home = states('sensor.home_temperature') | float(0) %}
        {% set boiler_room = states('sensor.boiler_room_temperature') | float(0) %}
        {{ ( boiler_room  - home ) | round(1) }}
      availability: >
        {{ states('sensor.home_temperature') | float("no") is number and 
        states('sensor.boiler_room_temperature') | float("no") is number }}
1 Like

I had tested (and failed) to check for availability, but your way is way more elgant … and works :smiley:

Thanks :slight_smile: