Hi, please could I ask the community for some help. Without using the Trend function.
I have a sensor called sensor.temperature and need to know if the value is increasing or decreasing over a 2 minute period.
I even used chat got but it does not work, #not surprised
Any help will be greatly appreciated.
binary_sensor:
- platform: template
sensors:
temperature_change:
friendly_name: "Temperature Change"
value_template: >
{% set duration = 120 %} # Set the duration in seconds (2 minutes)
{% set threshold = 0.5 %} # Set the threshold for significant change
{% set start_time = as_timestamp(now()) - duration %}
{% set end_time = as_timestamp(now()) %}
{% set start_temp = states('sensor.temperature') %}
{% set end_temp = state_attr('sensor.temperature', 'unit_of_measurement') %}
{% set change = (end_temp | float - start_temp | float) / duration * 60 %}
{% if change > threshold %}
increasing
{% elif change < -threshold %}
decreasing
{% else %}
stable
{% endif %}
See Nick’s answer.
You won’t succeed just with a single template, because you won’t be able to get a state “from the past” (unless you save it in a helper).