Template Guide: Days away from calendar event

well I made updates to the original post to account for the new way calendars work. So, you aren’t stupid, the original code was using the old calendar entity style.

Hey maybe I was to hasty. I noticed that the days don’t get updated at midnight. Only when I restart home assistant. Do you have an idea on why this could be?

you’ll have to use it with a trigger or write an automation that updates the sensor at a specific time using the homeassistant.update_entity service.

Okay thanks it should work now

Hi,
I know this is an old thread but hoping somebody can offer some advice.
I’m trying to use a sensor to show me what color trash can is going out tomorrow. I do this by reading an entry in a local calendar. Using the snippet in this thread I can get it working like this:

{% set midnight = today_at() %}
{% set event = state_attr('calendar.trash_calendar', 'start_time') | as_datetime | as_local %}
{% set color = is_state_attr('calendar.trash_calendar', 'message', "Blue Bin") %}
{% set delta = event - midnight %}
{% if delta.days == 0 and color == True %}
  true
{% elif delta.days == 1 and color == True %}
  true
{% elif delta.days > 1 %}
  false
{% endif %}

This works perfectly and updates my sensor accordingly. However, I have two bins going out this week, Blue and Brown, and my template seems to only show True to the first item in the calendar.
My “Brown Bin” sensor is identical except for swapping “blue” with “brown” in the message.
If I delete the Blue Bin from the calendar, the Brown Bin sensor works. If I then re-add Blue Bin, the Brown will continue to work but Blue will not.

Any advice would be greatly appreciated

maybe this thread will help? multiple sensors

You have to keep separate calendars for each bin. Calendars only show the next event.

1 Like

@petro - The Garbage Collection integration is no longer supported as the maintainer says its functionality is now almost entirely native in HA.

I have a “House” calendar in Google that I want to use for house maintenance reminders, including trash and recycling. I have created all-day events on the correct days for both collections.

I am trying to replicate the template sensor shown below pulling the number of days to the next “Trash” or “Recycling” full day event from the “House” calendar… Your quoted post would imply I need to create a separate calendar for each maintenance task which I definitely do not want to do… Is that the case? Is there some way around it?

I am trying to adapt {% set color = is_state_attr('calendar.house', 'message', "Recycling") %} to make it work in the set event portion of your example but haven’t figured it out.

- sensor:
    - name: "Recycling Day"
      icon: "mdi:recycle"
      state: >
        {% if is_state_attr('sensor.recycling', 'days', 0) %}
          Bring recycling in!
        {% elif is_state_attr('sensor.recycling', 'days', 1) %}
          Put recycling out!
        {% elif state_attr('sensor.recycling', 'days') | int(0) < 5 %}
          This Friday
        {% elif state_attr('sensor.recycling', 'days') | int(0) < 12 %}
          Next Week
        {% elif state_attr('sensor.recycling', 'days') | int(0) < 14 %}
          Week After Next
        {% endif %}

Do the different pickups follow a set pattern? If there is a pattern, you can use templating to create a list of dates to populate a calendar using the calendar.create_event service with a Repeat for each action.

Here are a couple examples: HA - Create calendar events for all trash nights · GitHub

Yes, that’s true if you want to count the number of days to any event after the next event. If you just want the next event, then you can use 1 calendar.

create multiple calendars or use the custom integration that pulls future events out of HA.

EDIT: Drew’s solution gets around that by creating the calendars for you it seems.

@Didgeridrew - Yes, trash is every Friday and recycling is every other Friday. This is the one that trips me up as everyone on my street gets it wrong… but Petro mentioned this solution also creates multiple calendars and I’d like to avoid that given I am using a Google calendar for all house/family events and I would end up with many sub-calendars (name?) making the feature less usable… right now I can select “Kids” to see theirs, or “House” to see maintenance, and so on.

@petro - Can you point me to the integration? I only see these in HACS:

I already have the Google Calendar Integration installed for this calendar. By any chance, is that the one you were referring to?

I have trash and recyclables on the same calendar, however I don’t separate the two. I just notify myself on the events that day.

Sorry, I don’t remember what it was called or where I saw it.

1 Like

Off topic maybe, but I use the excellent Waste Collection Schedule plugin (in HACS) to do what you want: GitHub - mampfes/hacs_waste_collection_schedule: Home Assistant integration framework for (garbage collection) schedules

If your community is listed, I suggest using it! If not listed, you can add it.

1 Like

The method I proposed only creates events, not new calendars. It’s been a while since I used Google Calendar regularly, but my understanding is that the calendar.create_event service still works with HA-connected Google calendars.

1 Like

When you first set it up with your code example, does the sensor only become available after midnight?

Honestly, I don’t remember. I’ll have to look at it in the morning. Until then, you might want to check out the macro library I made that just does the calculations for you.

I modified your code for my instance and the statement example works for me, but for some reason the list example does not. Not sure why. This also relates to my prior question about the date. The sensors I created showed unavailable.

Yeah, it had an issue in it. Updated original post

1 Like

@petro I used your helpful example (1st post) to create two date sensors that calculate days to next event in the calendars for trash and recycling. It all works as expected but I keep getting an error in the logs that I can’t decipher.

Templates:

- sensor:
    - name: "Trash Day"
      icon: "mdi:trash-can-outline"
      state: >
        {% set midnight = today_at() %}
        {% set event = state_attr('calendar.trash', 'start_time') | as_datetime | as_local %}
        {% set delta = event - midnight %}
        {% if delta.days == 0 %}
          Bring trash in!
        {% elif delta.days == 1 %}
          Put trash out!
        {% elif delta.days <= 5 %}
          Friday
        {% elif delta.days > 5 %}
          Next Week
        {% endif %}

#Controls image of trash can even though for now it is the same one for the 3 states
- sensor:
    - name: "Trash"
      icon: "mdi:trash-can-outline"
      state: >
        {% set midnight = today_at() %}
        {% set event = state_attr('calendar.trash', 'start_time') | as_datetime | as_local %}
        {% set delta = event - midnight %}
        {% if delta.days == 0 %}
          0
        {% elif delta.days == 1 %}
          1
        {% elif delta.days > 1 %}
          2
        {% endif %}

Error in log:

Logger: frontend.js.latest.202305033
Source: components/system_log/__init__.py:257
First occurred: 10:46:45 (6 occurrences)
Last logged: 15:49:57

https://xxxxxx.ui.nabu.casa/frontend_latest/14845--QdpkFp3oUQ.js:5:56702 Uncaught TypeError: Cannot read properties of null (reading 'sensor.trash_day')
https://xxxxxx.ui.nabu.casa/frontend_latest/14845--QdpkFp3oUQ.js:5:56702 Uncaught TypeError: Cannot read properties of null (reading 'sensor.recycling_day')

Could it be complaining about a missing default? If so, I could not find anything on how and where to add one. Do you have any suggestion on what to do?

That’s a ui error, has nothing to do with your templates