Actually, that’s a good alternative idea comparing to my filtering sensor as it allows to change value towards the current.
Thanks for sharing!
I’ll rework it a little bit as in python subtraction plays nasty tricks with us so it’s better to avoid it in comparison…
UPDATE: I think in the second part of the code it should be {{ previous + allowed }}
and {{ previous - allowed }}
as we use the last readings as basis, not new ones. And don’t forget that initial state of the template sensor is unknown
…
Anyway, here’s my version:
- platform: template
sensors:
this_sensor:
device_class: temperature
unit_of_measurement: '°C'
entity_id:
- sensor.original
value_template: >
{% set current = states('sensor.original') %}
{% set result = 'unknown' %}
{% if current != result %}
{% set current = current | float | round(1) %}
{% set previous = states('sensor.this_sensor') | float %}
{% set max_delta = 0.2 %}
{% set result = current %}
{% if previous %}
{% if current > previous %}
{% set upper_bound = previous + max_delta %}
{% set result = [current, upper_bound] | min | round(1) %}
{% elif current < previous %}
{% set lower_bound = previous - max_delta %}
{% set result = [current, lower_bound] | max | round(1) %}
{% endif %}
{% endif %}
{% endif %}
{{ result }}
and here is a shortened and compressed (without Jinja’s if
) main part:
{% if previous %}
{% set sign = current > previous %}
{% set boundary = previous + max_delta if sign else previous - max_delta %}
{% set pair = [current, boundary] %}
{% set result = (pair | min if sign else pair | max) | round(1) %}
{% endif %}
It might be a bit shorter using python (not Jinja!), but that’s another story.