ThaNerd
(Turbo Tronix)
1
I am trying to compare 2 scripts last triggered timestamps in order to find out which one was the last triggered from the 2.
My code is:
- platform: template
sensors:
sleep_mode_status:
friendly_name: "Sleep Mode Status"
entity_id: sensor.time
value_template: >-
{% if (state_attr('script.moisture_sensor_sleep_off','last_triggered') | float) > (state_attr('script.moisture_sensor_sleep_on','last_triggered') | float) %}
OFF
{% else %}
ON
{% endif %}
but I keep getting âunavailableâ even though both scripts do have a timestamp from their last trigger.
Dujith
(Charles Evers)
2
In the dev tools / template does it give a valid value?
{{ state_attr('script.moisture_sensor_sleep_off','last_triggered') }}
Edit: nvm
(state_attr('script.moisture_sensor_sleep_off','last_triggered') | float)
float isnt possible with a timestamp. Remove that from both and your good.
Also a good reminder to check logs and test in the dev tools.
ThaNerd
(Turbo Tronix)
3
It didnât work without the float either, my first attempt at this was without the float.
Dujith
(Charles Evers)
4
Well, you cannot convert a timestamp to a float. So thats a no go anyway.
What did the dev tools say for the value?
ThaNerd
(Turbo Tronix)
5
For this: {{ state_attr(âscript.moisture_sensor_sleep_offâ,âlast_triggeredâ) }}
I get this: 2022-06-04 22:44:06.482419+00:00
in dev/tools
and this:
2022-06-04 22:47:41.717538+00:00
for:
{{ state_attr(âscript.moisture_sensor_sleep_onâ,âlast_triggeredâ) }}
Dujith
(Charles Evers)
6
{{ state_attr('script.moisture_sensor_sleep_off','last_triggered') > state_attr('script.moisture_sensor_sleep_on','last_triggered')}}
And when you try this whole line? Should give a True or False
ThaNerd
(Turbo Tronix)
7
Yes it gives me âfalseâ
ThaNerd
(Turbo Tronix)
8
I see this:
And in the template for:
- platform: template
sensors:
sleep_mode_status:
friendly_name: "Sleep Mode Status"
entity_id: sensor.time
value_template: >-
{% if state_attr('script.moisture_sensor_sleep_off','last_triggered') > state_attr('script.moisture_sensor_sleep_on','last_triggered') %}
OFF
{% else %}
ON
{% endif %}{{ state_attr('script.moisture_sensor_sleep_off','last_triggered') > state_attr('script.moisture_sensor_sleep_on','last_triggered')}}
I get:
- platform: template
sensors:
sleep_mode_status:
friendly_name: "Sleep Mode Status"
entity_id: sensor.time
value_template: >-
ON
False
ThaNerd
(Turbo Tronix)
9
Nevermind it works, just needed to wait a little
Dujith
(Charles Evers)
10
Thats already there if you use the integration time.
And i think you shouldnt use entity_id in the template anyway?
- platform: template
binary_sensor:
sleep_mode_status:
friendly_name: "Sleep Mode Status"
value_template: {{ state_attr('script.moisture_sensor_sleep_off','last_triggered') > state_attr('script.moisture_sensor_sleep_on','last_triggered') }}
Since the True/False is already 0/1 - Off/On
1 Like