Offset input_datetime variable in time condition

I am trying to create a condition which checks if the time is before an input_datetime variable. However, I want to negatively offset the input_datetime variable with one minute.

Example: when input_datetime.bed is set to 23:00, I want to create the condition before 22:59.

I tried using “before_offset”, but that does not seem to work with “time” condition, only with a “sun” condition.

How can I do this?

As far as I know, you currently have to use a Template trigger instead.
This should work for you:

{{ states('sensor.time') == ( state_attr('input_datetime.bed', 'timestamp') | int - 60 ) | timestamp_custom('%H:%M', False) }}
1 Like

Thank you for the swift reponse!

I first had to add the time_date sensor platform by adding a few lines to configuration.yaml to be able to use sensor.time. With a minor adjustment to your code snippet (changing ‘==’ to ‘<’), it now does the job as expected:

{{ states('sensor.time') < ( state_attr('input_datetime.bed', 'timestamp') | int - 60 ) | timestamp_custom('%H:%M', False) }}

It is not a very elegant solution. How can I submit a feature request to add support for before_offset and after_offset for time conditions?

The Feature Request Category is probably a good place to start. Maybe someone is already working on it, you can search through the GitHub issues and pull requests if you want to.

1 Like