I have a few template triggers based on some of the attributes on my home alarm keypad. Is there a way to get the duration the attribute changes state using a template trigger?
I’ve had good results with this:
data_template:
message: "Switch was on for {{relative_time(states.switch.your_switch_name.last_changed)}}"
If you use it as a trigger, get the sensor to update based on time.
I have one for HA uptime.
ha_uptime:
value_template: '{{ relative_time(states.input_boolean.ha_start_delay.last_changed)}}'
friendly_name: 'HA uptime'
entity_id: sensor.time
Does relative_time() return value in minutes or seconds? I would like to do something like
{% if relative_time(states.switch.your_switch_name.last_changed) > 5 minutes %}
None of these option work for the attribute I want to track.
I have
Entity ID: sensor.alarm_keypad
With a state attribute “ac_present”.
I am trying:
{% if relative_time(states.sensor.alarm_keypad.attributes.ac_present.last_changed|int > 5) ... %}
But the attribute does not have a “last_changed” value. Last changed seems to only be available at “states.sensor.alarm_keypad.last_changed”
relative_time gives you a string such as “5 minutes” or “2 days”
I’m sure there’s a more elegant solution, but to do what you want I would try:
{{as_timestamp(now()) - as_timestamp(states.switch.your_switch_name.last_changed)}}
which will return the total number of seconds since it changed.
I saw your last message too though. I don’t know how this works with attributes.
I am not sure last update works with state attributes but just with states. I would try making a new template sensor that monitors the ac present attribute. I think the new sensor will then have its own last update based on just the AC present sensor.
Brilliant! Then I can use a normal state platform with the ‘for’ option. Let me give that a try…
@RobDYI your suggestion worked perfectly. I created a binary sensor that tracks the alarm attribute.
@danlucka and @rabittn thanks for the pointers.
Everytime I start messing with my HA config I get sucked down the rabbit hole. Now I’m finding automation that never actually work because these attributes return boolean values.