Google Calendar: Next Event via Template

Hey all,

Thanks in advance for any help anyone can provide.

I’ve been strumbling my way through templates, and I think I’ve got something that almost works, but it’s only giving me information on the current event, rather than the future event. To elaborate, I have this string: {{ ((state_attr('calendar.todoist','start_time') | as_datetime | as_local).date() - now().date()).seconds/60 }} of which the result is 0.0, because the event is active. Ideally, I’m looking for it to show me how long it is until the next event is active.

Belatedly, I’m editing this, and realizing that the templating I have for the event {{ state_attr('calendar.todoist','message')}} also calls the current event. So, is there a way to call the next event?

My use case is that this will be displaying a Nest Hub in a bedroom, for my caretakers when I have major surgery. One of them had requested an easy way to be able to see when my next medication dose was, or when I needed other scheduled medical assistance.

Much obliged to anyone who can offer suggestions!

  1. You can’t view the next scheduled event, only the current one.

  2. Even the current event isn’t necessarily the actual current event. If there’s an All Day event, it can dominate the calendar’s attributes and prevent displaying any other scheduled events for that day.

  3. Use the Calendar Trigger, not a calendar’s attributes.

I’m not looking to create an automation. I’m looking to have this information displayed on a view.

Is “next event” possible with the new HA calender, or am I SOL?

Calendar entities don’t show future events, only the next event (or current event if you’re in the middle of the event).

Point #1. “You can’t view the next scheduled event, only the current one.”

In other words, you can’t do what you want to do.

What you can do is create an automation (or a Trigger-based Template Sensor) with a Calendar Trigger employing an offset. It will trigger in advance of the scheduled event.

I think we might all be getting mixed up, perhaps because of me not using the “proper” terminology.

Next event is what I’m looking for (and that calendar I’m using wasn’t probably the best choice as it nearly constantly has “ongoing” events, but I needed a dummy to play with), so from what you’re saying, @petro, if I’m understanding correctly, is I CAN have what I want to display (the NEXT event).

Using a slightly modified string from my OP, {{ ((state_attr('calendar.back_basement_2','start_time') | as_datetime | as_local).date() - now().date()).seconds / 60 | int(0) }} … in developer tools, that’s still giving me a result of 0.0, even though the event doesn’t start until this coming Saturday. I have also tried with 'end_time' instead of 'start_time' but still a result of 0.0.

Is my template configuration incorrect? I’m admittedly still struggling to figure out how templating works, so I wouldn’t be surprised if I made an error somewhere.

You can have next event if no event is currently happening.

If you’re trying to get minutes to that event…

{{ ((state_attr('calendar.back_basement_2', 'start_time') | as_datetime | as_local - now()).total_seconds() / 60) | int }}

As a side note: If you have the same medicines every day, you might want to ditch the calendar and just use input_datetimes that only have a time.

However, if you do want to use the calendar, what I do is make multiple calendars. I’d probably do 1 for each medicine. Then you’re always viewing current or next event and don’t have to worry about other events trumping your next event.

I’ll just leave this here:

The author of Local Calendar, Calendar Trigger, etc has actively discouraged the use of a calendar’s attributes for templating purposes (as in a Template Condition).

Why? Because the calendar’s attributes may not reflect the actual (current or upcoming) scheduled event, thereby causing the template to report incorrect results. It can occur when there are concurrently scheduled events, All Day events, etc.

The author’s recommendation is to rely on the Calendar Trigger to provide consistently accurate results.


FWIW, I’ve helped at least two users who opened Github Issues complaining about templating problems involving a calendar’s attributes. In both cases, the problem was that the calendar’s attributes did not report the actual current, or upcoming, scheduled event.

This basically did the trick, though because of how that calendar is, I had to use end_time instead of start_time.

With regards to your edit, @petro - I’d prefer a calendar as that’s something even my caretakers should be able to set up post-surgery without mucking too much with HA, if I’m not able to set it up myself. I don’t think I’ll know a medication schedule or other medical needs until after the surgery occurs - but perhaps the surgeon can give me that information in advance.

My intention is to call this as conditional card - if I did the multiple calendars as you suggest, is there a way for that card to cycle through those calendars? I’m afraid we’re exiting the scope of my (limited) knowledge. As it is, I can’t even get the conditional card to display, perhaps because I’m trying to use a calendar as an entity? Things like this make me feel like I’m too daft for HA, in general. I get things to work, but sometimes it feels like I’m stumbling into the solution rather than doing it elegently.

Which is why, @123, I’ve not been ignoring your thoughts, but what you’re suggesting doesn’t make sense to me - not in that it’s not logical, I’m quite sure it is - but in that I have no idea how to employ it in the method I want/need. I don’t know/understand how to use a calendar trigger on a card, basically. In my automations, they all rely on calendar triggers, but even those aren’t as fine tuned as I’d like (which was why I was going to explore templating there, as well, but that’s neither here nor there).

Feel free to employ whatever you want, just be aware that you’ve creating a meds notifier using an unrecommended technique that can use a potentially incorrect scheduled event.

I’m open to alternative methods, but I don’t know how to employ them with cards. Templating, while not a strong suit for me, I know at least DOES work with cards. That said, If you’d like to direct me on the preferred technique, and it’s usable with cards, I’d be happy to go that route.

None of Home Assistant’s standard cards support templates (except the Markdown card). Are you using a custom card?

Yes, custom. In this case, a mushroom chip card, with card mod also installed.

The original code that I’m borrowing from was:

            - type: template
              content: >-
                Ends in {{states.sensor.kodi_br_time_left.state}} at
                {{states.sensor.kodi_br_ends_at.state}}
              icon: mdi:kodi
              style: |
                ha-card {
                  --ha-card-border-width: 0;
                  --chip-background: none;
                  --chip-height: 95px;
                  --chip-padding:12px;
                  --chip-box-shadow:none;
                  --chip-border-width:0;
                  border-width: 0;
                  }

My version, however isn’t working at all. :cry:

          - type: template
            content: >-
              {{ state_attr('back_basement_2','message')}} in 
              {{ ((state_attr('calendar.back_basement_2', 'end_time') | as_datetime | as_local - now()).total_seconds() / 60) | int }} minutes.
            style: |
              ha-card {
                --ha-card-border-width: 0;
                --chip-background: none;
                --chip-height: 75px;
                --chip-padding:12px;
                --chip-box-shadow:none;
                --chip-border-width:0;
                border-width: 0;
             }

Knowing me, it’s probably something dumb, but, yeah.

So, wanted to follow up, because there were a few issues with what I posted above, that someone in a Mushroom thread caught. The first was that the first state_attr referenced back_basement_2 instead of calendar.back_basement_2 … so as I suspected, something stupid. The second wasn’t something that I wouldn’t have thought, because it was working without elsewhere, but it was actually needing to call card_mod before the style. Here’s the fixed version:

- type: template
            content: >-
              {{ state_attr('calendar.back_basement_2','message')}} in  {{
              ((state_attr('calendar.back_basement_2', 'end_time') | as_datetime
              | as_local - now()).total_seconds() / 60) | int }} minutes.
            icon: mdi:calendar-alert
            card_mod:
              style: |
                ha-card {
                  --ha-card-border-width: 0;
                  --chip-background: none;
                  --chip-height: 75px;
                  --chip-padding:12px;
                  --chip-box-shadow:none;
                  --chip-border-width:0;
                  border-width: 0;
                }

Leading to result of this:

Overview-–-Home-Assistant (10)

So thank you, @123 and @petro for your time and assistance.

If we use triggers. Can we get details about the event from the trigger? e.g. what themessage is? thanks

Not quite sure what you’re asking?

Maybe @petro will be able to help though.

Basically I have been using the trigger event but want to check the entry before continuing. That entry is sometimes stale. Can I use {{trigger.details…}} somehow?

question? I have my template setup like this:

{% set next_event=as_timestamp(state_attr('calendar.school_days','start_time')) | timestamp_custom("%Y-%m-%d",true) %}
{% set tomorrow=(now().date() + timedelta(days=1)) | string %}
{{ next_event == tomorrow }}

I get the state as off when my calendar clearly shows school for tomorrow. i cant figure out what is wrong on my template.

There have been many changes to Calendars since this thread started.

The calendar entity’s state object only holds the current event (or the next event if there isn’t a current active event). So, if you have an all-day event active currently, the value for next_event will be the current date… which is not equal to tomorrow’s date.

Please refer to the current Calendar docs for information on using calendar.get_events service calls to retrieve future event data.

Example using all-day events and searching for specific terms.