Hello,
I want to create a sensor with long-term maximum temperature from another sensor.
By now, I have this in an input_number
instead of a sensor, with next code in a yaml package:
input_number:
ta_maxima_exterior:
name: Tª máxima exterior
icon: mdi:thermometer-chevron-up
unit_of_measurement: ºC
# initial: 10
min: 10
max: 48
step: 0.05
input_datetime:
fecha_ta_maxima_exterior:
name: Fecha Tª máxima exterior
has_date: true
has_time: true
automation:
- id: update_ta_max_exterior
alias: "Update Tª máxima exterior"
description: "Updates max temperature and date"
trigger:
- platform: template
value_template: >-
{{states('sensor.pws_temp')|float >
states('input_number.ta_maxima_exterior')|float}}
condition: []
action:
- service: input_number.set_value
data:
value: >-
{{ states('sensor.pws_temp')}}
target:
entity_id: input_number.ta_maxima_exterior
- service: input_datetime.set_datetime
data:
timestamp: '{{ now().timestamp() }}'
target:
entity_id: input_datetime.fecha_ta_maxima_exterior
mode: single
I’ve tried to create a sensor with the same behaviour (last_update will be stored in an atribute, but not coded until sensor works as intended) with template triggering like this:
template:
- unique_id: 'ta_maxima_exterior'
trigger:
- platform: template
value_template: >-
{{states('sensor.pws_temp')|float >
states('sensor.ta_maxima_exterior')|float}}
sensor:
- name: "Tª máxima exterior"
state: "{{ states('sensor.pws_temp')}}"
unit_of_measurement: 'ºC'
But it doesn’t work. I don’t know it the problem is using the sensor state inside the trigger.
In both cases, initialization of the maximum value is neccesary. In the ‘input_number’ solution, through manual triggering the automation, and in the sensor one, with update entity service.
Any help will be welcome.
I’m seeing that input_number
values are set to their initial value each time HA is restarted, so I’ve changed my actual solution removing initial
property of input_number
.
I’ve been able to make what I want whith this sensor:
template:
- trigger:
- platform: template
value_template: >-
{{states('sensor.pws_temp')|float >
states('sensor.ta_maxima_exterior')|float(-10)}}
sensor:
- name: ta_maxima_exterior
unique_id: "ta_maxima_exterior"
state: "{{ states('sensor.pws_temp') }}"
unit_of_measurement: "ºC"
icon: mdi:thermometer-chevron-up
attributes:
last_update: "{{ now() }}"
The only problem is with initialization. If I initialize it manually in Developer Tools -> States -> Set State
. After the initialization it works fine.
My question now is whether there is a way to automatically (or with a script, or something like that) initialize this sensor after creation (or reset manually after some time).
I’ve found a solution to inizialization/reset with an input_boolean
, and a new trigger with platform state like this:
input_boolean:
reset_max_min:
name: Reset max min
initial: False
template:
- trigger:
- platform: template
value_template: >-
{{states('sensor.pws_temp')|float >
states('sensor.ta_maxima_exterior')|float(-10)}}
- platform: state
entity_id: input_boolean.reset_max_min
from: "off"
to: "on"
sensor:
- name: ta_maxima_exterior
unique_id: "ta_maxima_exterior"
state: "{{ states('sensor.pws_temp') }}"
unit_of_measurement: "ºC"
icon: mdi:thermometer-chevron-up
attributes:
last_update: "{{ now() }}"
The likelihood is that the reason it doesn’t work the way you want, is because you need a default value for BOTH floats - as sensor.pws_temp
probably doesn’t have a value when Home Assistant is loading in the YAML files - and will complain that it cannot convert ‘unknown’ to a float value.
{{ states('sensor.pws_temp')|float(-99) >
states('sensor.ta_maxima_exterior')|float(-10) }}
May go some way to solving some of your issues.
1 Like
It doesn’t work for me. The problem is that the sensor on triggers when the condition in the trigger passes from false
to true
.
A working solution is in my previous post, with the input_boolean
.
Yeah if it was me - for the automation - I would have triggered on every state change of the sensor, and then used the condition to check if it was above the maximum always recorded.
Something like:
automation:
- id: update_ta_max_exterior
alias: "Update Tª máxima exterior"
description: "Updates max temperature and date"
trigger:
- platform: state
entity_id: sensor.pws_temp
condition:
- platform: template
value_template: >-
{{ states('sensor.pws_temp')|float(-99) >
states('input_number.ta_maxima_exterior')|float(-99) }}
The problem is that template triggered sensor don’t have a condition clause.
That’s why I would have stuck with the automation - the example that I have posted would work fine, on the first state change of sensor.pws_temp
Just in case anybody finds it useful, here is the last version of the sensor.
It has these features:
- It does not need initialization.
- It removes wrong sensor values when the difference between new value and last one is higher than
input_number.max_temp_step
, which can be changed through Lovelace.
- It can be reset by activating
input_boolean.reset_max_min
, which works as a dry contact through an automation.
- It stores last update as an attribute.
The code is:
template:
- trigger:
- platform: state
entity_id: sensor.pws_temp # Source sensor
- platform: state
entity_id: input_number.max_temp_step
- platform: state
entity_id: input_boolean.reset_max_min
from: "off"
to: "on"
sensor:
- name: "Tª max Exterior"
state: >-
{% if ((state_attr('sensor.ta_max_exterior','last_valid_sensor_value')|float(states('sensor.pws_temp'))|float
- states('sensor.pws_temp')|float)|abs < states('input_number.max_temp_step')|float)
and (states('sensor.pws_temp')|float > states('sensor.ta_max_exterior')|float(-10)) %}
{{ states('sensor.pws_temp') }}
{% else %}
{{ states('sensor.ta_max_exterior') }}
{% endif %}
unit_of_measurement: "ºC"
icon: mdi:thermometer-chevron-up
attributes:
last_update: >-
{% if ((state_attr('sensor.ta_max_exterior','last_valid_sensor_value')|float(states('sensor.pws_temp'))|float
- states('sensor.pws_temp')|float)|abs < states('input_number.max_temp_step')|float)
and (states('sensor.pws_temp')|float > states('sensor.ta_max_exterior')|float(-10)) %}
{{ now() }}
{% else %}
{{ state_attr('sensor.ta_max_exterior','last_update') }}
{% endif %}
last_valid_sensor_value: >-
{% if ((state_attr('sensor.ta_max_exterior','last_valid_sensor_value')|float(states('sensor.pws_temp'))|float
- states('sensor.pws_temp')|float)|abs < states('input_number.max_temp_step')|float) %}
{{ states('sensor.pws_temp') }}
{% else %}
{{ state_attr('sensor.ta_max_exterior','last_valid_sensor_value') }}
{% endif %}
input_number:
max_temp_step:
name: Max temp step
min: 0
max: 20
initial: 5
input_boolean:
reset_max_min:
name: Reset max min
initial: False
icon: mdi:alert
automation:
- id: toggle_reset_min_max
alias: "Toggle reset_min_max"
description: "When input_boolean.reset_max_min is set to on, automatically set it to off after 2 seconds"
trigger:
- platform: state
entity_id: input_boolean.reset_max_min
from: "off"
to: "on"
condition: []
action:
- delay: '00:00:01'
- service: input_boolean.turn_off
target:
entity_id: input_boolean.reset_max_min
mode: single
A long-term minimum temperature sensor can be done in a similar way:
- trigger:
- platform: state
entity_id: sensor.pws_vallad_temp
- platform: state
entity_id: input_number.max_temp_step
- platform: state
entity_id: input_boolean.reset_max_min
from: "off"
to: "on"
sensor:
- name: "Tª min Exterior Valladolid"
state: >-
{% if ((state_attr('sensor.ta_min_exterior_valladolid','last_valid_sensor_value')|float(states('sensor.pws_vallad_temp'))|float
- states('sensor.pws_vallad_temp')|float)|abs < states('input_number.max_temp_step')|float)
and (states('sensor.pws_vallad_temp')|float < states('sensor.ta_min_exterior_valladolid')|float(45)) %}
{{ states('sensor.pws_vallad_temp') }}
{% else %}
{{ states('sensor.ta_min_exterior_valladolid') }}
{% endif %}
unit_of_measurement: "ºC"
icon: mdi:thermometer-chevron-up
attributes:
last_update: >-
{% if ((state_attr('sensor.ta_min_exterior_valladolid','last_valid_sensor_value')|float(states('sensor.pws_vallad_temp'))|float
- states('sensor.pws_vallad_temp')|float)|abs < states('input_number.max_temp_step')|float)
and (states('sensor.pws_vallad_temp')|float < states('sensor.ta_min_exterior_valladolid')|float(45)) %}
{{ now() }}
{% else %}
{{ state_attr('sensor.ta_min_exterior_valladolid','last_update') }}
{% endif %}
last_valid_sensor_value: >-
{% if ((state_attr('sensor.ta_min_exterior_valladolid','last_valid_sensor_value')|float(states('sensor.pws_vallad_temp'))|float
- states('sensor.pws_vallad_temp')|float)|abs < states('input_number.max_temp_step')|float) %}
{{ states('sensor.pws_vallad_temp') }}
{% else %}
{{ state_attr('sensor.ta_min_exterior_valladolid','last_valid_sensor_value') }}
{% endif %}