If I understand the goal of your time math, you want the binary sensor to be âonâ during the time period from x minutes before the event starts to x minutes after the event ends. Where x is the value of your input number helper. That can be done much simpler. However, you should be aware that this might fail to behave as you expect it to if you have overlapping events, especially all-day events, in the same calendar.
Unless you have a good reason for not doing so, you should be using the current format instead of the the legacy format for template sensors.
Donât rely on a calendar entityâs attributes. They arenât guaranteed to represent the next scheduled event (notably if thereâs an all-day event, because it supersedes all others, or concurrently scheduled events).
Youâre welcome. Itâs something I learned from the software developer who maintains the Google Calendar and Local Calendar integrations as well as the Calendar Trigger.
Initially I was using a Calendar entityâs attributes in conditions but then encountered the situation where it would only report an all-day event even though there were also hourly events scheduled on the same day.
The developer explained thatâs just one example of why I should not rely on the entityâs attributes in a condition. The recommendation was to use a Calendar Trigger and then check the trigger variableâs properties.
Check your Log because it probably contains an error message related to this automation. Based on the following test, a Calendar Triggerâs offset doesnât currently support templates.
Hello,
I checked the log and I see the same error that you got in your test.
And I also see an error with the template code above even though it is working.
It will report an incorrect result when any of these situations occur:
On any day containing scheduled events and an All-day event.
On any day containing more than one event scheduled for the same time.
ChatGPT doesnât know that but the author of the calendar integration does and thatâs why itâs not recommended to employ a calendar entityâs attributes in a condition.