If an event is happening today, do this

Hi, sorry for what may be a simple question! I’ve managed to get an automation to read the next event in my calendar but really struggling to impliment an if statement as to whether it is read out or not. I’ve copied the ‘if’ statement below but any advise would be really helpful thank you. this is currently in the Action section.

Ideally, if there is an event on my calendar in the next 12 hours than read this. struggling to get the ‘if’ function to come back correctly.

- if:
      - condition: template
         value_template: "{{ state_attr('calendar.birthdays', 'start_time') == (as_timestamp(utcnow().day) + (14400)) }}"
    then:

I thought the above code checked for any events in the next 24 hours and returned true if it found any, however that doesn’t seem to be the case on my assistant

thank you!

Time templates can be confusing… there are many different methods and formats. In your case, you are mixing data types and methods.

The left half of your comparison returns a time string. If it was constructed properly, the right half would return a float. You would need to perform at least one more conversion to be able to compare them.

{% set next_event = state_attr('calendar.birthdays','start_time')|as_datetime|as_local %}
{% set now_plus_12 = as_timestamp(utcnow()) + (60*60*12) %}
{{ as_timestamp(next_event) <= now_plus_12 }}

Instead, try the following for a 12-hr time span:

{% set next_event = state_attr('calendar.birthdays', 'start_time')|as_datetime|as_local %}
{{ now() + timedelta(hours=12) >= next_event   }}

“All-day” events usually start at “00:00:00”, so you may also be able to use:

{{ today_at() + timedelta(days=1) == state_attr('calendar.birthdays','start_time')|as_datetime|as_local }}
3 Likes

Thank you so much!!