Use sensors with timestamps for time condition for automations

Hello,
currently, the time condition can use input_datetime to dynamically set a time condition.
I have a sensor that contains a timestamp and I would like to use this for the time condition but I get an error when I do.

When creating time triggers you can use sensors with timestamps to trigger the automation so it only makes sense that you can do the same thing for the time condition.

There is a workaround that is less intuitive and doesn’t follow the same format as time trigger.

It’d help if you explained in more detail what you’re trying to do — your post is very hypothetical.

You can use a template trigger. It’s easiest if you set up sensor.time in your system: see the time & date integration. That will give you a sensor containing the current time e.g. 16:44.

Now you just need to compare that with your sensor. You say it contains a timestamp: if this is a genuine datetime object, then you can do this:

trigger:
  - platform: template
    value_template: "{{ as_timestamp(states('sensor.your_sensor'))|timestamp_custom('%H:%M', true) == states('sensor.time')  }}"

This template line converts your sensor first into a UNIX timestamp (seconds since 1970), then into a time format matching sensor.time to compare it with that sensor. See the documentation here.

Note that this example completely ignores the date element of the timestamp, and will trigger at the given time every day.

I recommend you use the Developer Tools / Template feature to play around and see what is going on here. Here’s an example using a timestamp sensor on my system:

If you need further help, please post a screenshot of Developer Tools / States for your timestamp sensor, showing the contents of the Entity, State and Attributes columns.

Sorry about that, I mostly created this feature request to make the time trigger and time condition work more similarly.

Here are the differences between the two.
Currently, time trigger has 3 possible input values
Time string: the regular schedule
Entity date_time: getting the timestamp from and input_datetime
Sensors with timestamp device class: a sensor that has a timestamp

The time condition only has the first 2 and can not make the use of sensors that have timestamp device class.

I have a sensor with a timestamp that I wanted to use as a condition. I wanted this automation to only run after a timestamp that changes every day. I went with using a template condition with the following template

{{ as_timestamp(now()) > as_timestamp(states('sensor.maghrib_prayer')) }}

This works but I think this feature should be built into the time condition as time trigger can already use sensors with timestamps. This feature request is more about making it seamless rather than requesting something new.

1 Like

Haha, sorry — I missed that this was a feature request. I thought it was a very vague question. :blush:

I believe the Feature Request is to allow a Time Condition to use a sensor with a timestamp device_class just like a Time Trigger permits.

For example, in the Time Condition below, the after and before options use the timestamp values of two sensors as opposed to using hard-coded time strings.

condition:
- condition: time
  after: sensor.start_time
  before: sensor.end_time
  weekday:
    - mon
    - wed
    - fri