Population of Sensor from Template

I have a question on the population of values to templates
I have a sensor that spikes for no reason sometimes to -40, i do not want these spikes in the database or in a graph.
In Lovelace I have now displayed both values as badges but the sensor (sensor.new_wp_buitentemp) that get’s filled when the IF is true, the strange thing is that the template is populated once every few minutes(not a fixed period), the original sensor('sensor.wp_buitentemp) is populated every few seconds.
does anyone understand what and/or why this is happening ? and perhaps how to fix this.
same behavior in the Template editor.

{% if states(‘sensor.wp_buitentemp’) | float > -39 %}
{ ((states(‘sensor.new_wp_buitentemp’)|float (states(‘sensor.wp_buitentemp’)|float))) }
{% endif %}

thanks
Erik

Not a direct answer to your question, but spikes can be filtered out by using a filter sensor. You then use the filter sensor in place of the actual sensor. https://www.home-assistant.io/integrations/filter/

Hi Thomas
I have tried this but as the sensor has a big range I have not got the filter correct.
i have rewritten the sensor and this is working as quick as i would epected
i don’t understand why this would be different.

  • platform: template
    sensors:
    temp_wp_buitentemp:
    unit_of_measurement: ‘°C’
    friendly_name: “Temp WP BuitenTemp”
    value_template: >-
    {% set temp_wp_buitentemp = ((states(‘sensor.wp_buitentemp’) | float)) | float %}
    {{ temp_wp_buitentemp if temp_wp_buitentemp > -30 else ‘’ }}

anyone an idea ?

thanks
Erik