I have a serial integration where many different template sensors are set.
Here I have PressureID set to a value and PressureIDTS set to the date/time timestamp the PressureID was set.
I want to set the PressureID to unknown if was not seen in the serial port within the last minute. Here is the code I tried but it does not work. Maybe there is any easier way to do this within the action bit I based it on an example I saw.
´´´
- id: '1686066013547'
alias: Pressure Valid
description: ''
trigger:- platform: time_pattern
minutes: /1
condition: []
action:- service: template
data_template:
entity_id: sensor.PressureID
value_template: >
{% set last_updated = state_attr('sensor.PressureIDTS', 'last_updated') %}
{% set time_diff = (as_timestamp(now()) - as_timestamp(last_updated)) / 60 %}
{% if time_diff > 1 %}
unknown
{% else %}
{{ states('sensor.PressureID') }}
{% endif %}
´´´
- service: template
- platform: time_pattern