Compare 2 triggered timestamps

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.

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.

It didn’t work without the float either, my first attempt at this was without the float.

Well, you cannot convert a timestamp to a float. So thats a no go anyway.
What did the dev tools say for the value?

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’) }}

{{ 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

Yes it gives me “false”

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

Nevermind it works, just needed to wait a little :slight_smile:

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