Template binary sensor based on dates - need help

I really need help. I’m trying to make binary sensor to be set “on” when date is between months and days defined via input number fields in UI


start_day_of_month = 15
start_month = 10
stop_day_of_month = 15
stop_month = 4

Today is 17.03 so switch should be set to “on”. One of problems is that this condition is overlaping “next” year.


Try and play with that
{{ as_timestamp( now()) | timestamp_custom ("%d") | int}}

{{ as_timestamp( now()) | timestamp_custom ("%m") | int}}

I would create two input_datetime’s - one for start & one for end - and then compare those to the sensor.datetime to set the state of the binary sensor.

{{ (as_timestamp(strptime(states.sensor.date_time.state, '%Y-%m-%d, %H:%M')) > states.input_datetime.start_date.attributes.timestamp) and (as_timestamp(strptime(states.sensor.date_time.state, '%Y-%m-%d, %H:%M')) < states.input_datetime.end_date.attributes.timestamp) }}

you might be able to use as_timestamp(now()) instead of the sensor.datetime but I’m not sure that updates correctly so it would definitely be safer to use the sensor.datetime even if it is a little messy.

I’ve done pretty this much for my holidays

Feel free to “steal” my code :blush:
1 Like