How can I compare a input date_time with current time in autiomation

I’m trying to make sure my alarm does not get disarmed because I get WiFi signal on my way out of the house.

My current setup is that I turn on the alarm manually, but when my device tracker sees me as home it should disarm the alarm.
I have one device tracker that is ping type, and I use the Home assistant app geo tracking.
Those two are the triggers.

Then I have the condition, which is that the alarm has to be armed, and now I want to be sure a sudden wifi or geo track within “home” does not disarm the alarm.

What I did was create a input date time:

Which is updated by any state change of the alarm.

I can add a time sensor, https://www.home-assistant.io/integrations/time_date , but that does not include timestamp (?).

So how can I add a condition in the automation to not disarm the alarm if the last state change is less than say 5 minutes ago?
The date time sensor is a string, and my input date time is a different formatted string or timestamp.

This is my current automation:

- id: '1234'
  alias: Alarm off, arrive home
  description: ''
  trigger:
  - entity_id: device_tracker.andreas
    platform: state
    to: home
  - entity_id: device_tracker.nem_l21
    platform: state
    to: home
  condition:
  - condition: state
    entity_id: alarm_control_panel.sector_alarm_01234
    state: armed_away
  action:
  - data:
      code: 1234
    entity_id: alarm_control_panel.sector_alarm_01234
    service: alarm_control_panel.alarm_disarm

Ok… Maybe I have found something.
At the bottom of the page I see that you can use now().timestamp().
Does that mean this should work?

  - condition: template
    value_template: '"{{ now().timestamp()+300 > input_datetime.alarm_last_state_change.timestamp 
 }}"'

No this will not work, you need to use state_attr to get the timestamp attribute from the input_datetime, you can’t get it like you wrote it.

Try something like this, that’s how I do it normally:

- condition: template
  value_template: "{{ (as_timestamp(now()) + 300) > as_timestamp(states('input_datetime.alarm_last_state_change')) }}"

Didn’t work.
The automation fired even when it was within 3 minutes

I just adapted your example, however I see now that the logic in your example is wrong, you are checking whether now + 5 minutes is bigger than the last state change, this will always be true, the + 5 minutes needs to be on the other side of the comparison

- condition: template
  value_template: "{{ as_timestamp(now()) > (as_timestamp(states('input_datetime.alarm_last_state_change')) + 300) }}"

That is quite obvious now that I think about it…

Now it doesn’t disarm at all, neither within 5 min or after.
That is unexpected…

looking at the templating page: Templating - Home Assistant
I see this:

as_timestamp() converts datetime object or string to UNIX timestamp. This function also be used as a filter.`

So it doesn’t say if it returns a string or int. Do you know?

I see now that there was a double single quote which looked like a double quote.
Let me try this again, just have to wait for my router to stop believing I’m home.

EDIT again.
Yes it worked. It was the double single quotes that tricked me.

try this:

    - condition: template
      value_template: '{{ as_timestamp(trigger.from_state.last_changed) + 300 < as_timestamp(now()) }}'

I got it working with the other solution posted by Burningstone.
It was all my fault :-/

I will try this too and see if it works.

Here’s another way to do the same thing. It takes advantage of the fact that the now() function has a timestamp property and an input_datetime has a timestamp attribute.

- condition: template
  value_template: "{{ now().timestamp() > state_attr('input_datetime.alarm_last_state_change', 'timestamp') + 300 }}"
2 Likes