Condition comparing two time fields using Input_datetime

Well folks i’m frustrated and tired. Spent two days researching and trying to build this an i got extremely close but failed :frowning:

My goal is to use a boolean switch and helper time field to track medications. My son takes meds 3 times daily about every 4 hours. I want to set a notification system for the family that tracks that last time the meds were taken and remind us via notification system for the next time.

I have a system in place and rebuild this new one where everything is working with the exception of being able to enter a time manually and trigger the automation once we hit 4+ hours since the last time the time field was updated.

Right now the condition is as follows:
{{ now().timestamp() - as_timestamp(states.input_datetime.meds_time_entry.last_changed) > 14400 }}

But as you can see this is using the last change of when the field was updated, not the actual hour i entered into the field.

What i need to compare is current time, has it past 4 hours since the time that exists in the input_datetime field and then once it passes the 4 hours trigger. The challenge is the time field i created via Input_datetime was added as a time field only. I don’t want to have to worry about updating the date.

Hopefully this makes sense. Really appreciate any guidance here as i’m at the stage of giving up…

Try this:

{{ now() > now().replace(hour=state_attr('input_datetime.meds_time_entry', 'hour'), minute=state_attr('input_datetime.meds_time_entry', 'minute')) + timedelta(hours=4) }}

The right hand side takes the current datetime and changes its hour and minute to match the ones in your input_datetime. The result, which is a datetime object, is advanced by 4 hours (by simply adding a timedelta object set to 4 hours). Now the right hand side is “meds_time_entry + 4 hours”.

The left hand side is simply the current date and time (as a datetime object). If the left hand side is greater than the right hand side, the template reports true. If used as a Template Trigger, it will trigger the moment the current time is 4 hours after the time set in meds_time_entry.

alias: 'example 999'
trigger:
- platform: template
  value_template: >
    {{ now() > now().replace(hour=state_attr('input_datetime.meds_time_entry', 'hour'), minute=state_attr('input_datetime.meds_time_entry', 'minute')) + timedelta(hours=4) }}
action:
... etc ...
3 Likes

Sweet. Looks like it will do exactly what i hope.

What scripting language does HA use? I’m struggling to find out how people know to use certain attributes etc when calling or looking for certain data. I never studied code but got into scripting for example with a game (ARMA Series) which had a wiki with all valid scripting calls. HA has a nice documented database of integrations but i don’t find anything where it digs deeper into these other attributes. I do use the built in Dev Tools but it doesn’t really share all these additional attributes…

Examples:
*.last_changed
*.attributes.hour
*.replace

Start with learning the properties of the State Object.

Then familiarize yourself with Templating.

Appreciate it. Will review