Times of Day, using input_datetime helpers for before/after?

I’d like to have a binary sensor that is true when the current time is between two input helper datetime variables.

I’ve tried this but it doesn’t work - but it might explain what I mean:

- platform: tod
  name: Within time window
  after_offset: "{{ states('input_datetime.start_time') }}"
  before: "{{ states('input_datetime.stop_time') }}"

How can I do this in a reasonable way? I use node-red for automations, but I make use of template sensors for combined sensors as I find it easier to reason about.

The use case here is to turn on a lamp outside of the kids bedroom, but only if it’s dark enough and not after bedtime for them (so that the lamp doesn’t turn on while putting them to bed, waking them up). I’d like to change these time windows as needed, without having to edit the template sensor or changing the automation.

Try this:

- platform: tod
  name: Within time window
  after: "{{ states('input_datetime.start_time') }}"
  before: "{{ states('input_datetime.stop_time') }}"

Thanks, but it doesn’t work (but thanks for noticing my typo! :D)

Invalid config for [binary_sensor.tod]: Invalid time specified: {{ states('input_datetime.stop_time') }} for dictionary value @ data['before']. Got "{{ states('input_datetime.stop_time') }}"
Invalid time specified: {{ states('input_datetime.start_time') }} for dictionary value @ data['after']. Got "{{ states('input_datetime.start_time') }}". (See ?, line ?).

Can you show the state of your two entities “input_datetime” ?

You can use a template instead using this:

{{ (states('input_datetime.start_time') < as_timestamp(now()) | timestamp_custom("%H:%M:%S")) and (states('input_datetime.stop_time') > as_timestamp(now()) | timestamp_custom("%H:%M:%S")) }}
1 Like

YES!!! Thank you :smiley:

That was exactly what I needed. Again, thank you!

If the two input_datetimes are time-only then you can use today_at to determine if the current time is between the two values.

{{ today_at(states('input_datetime.start_time')) < now() < today_at(states('input_datetime.stop_time')) }}

Example

1 Like