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.
You can’t view the next scheduled event, only the current one.
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.
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.
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.
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.
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:
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?
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.