Local Calendar Countdown

I have got my local cal o show days until an event, but I would like to add some contextual information. So for Example, my Garbage is “Today” however, I am currently only getting 0 as the countdown.

|-
        {{ (state_attr('calendar.bin_collection', 'start_time') | as_timestamp - today_at('00:00') | as_timestamp) / (60 * 60 * 24) }}
        {% if days == 0 %}
          Today
        {% elif days == 1 %}
          Tomorrow
        {% else %}
          {{ days }} days
        {% endif %}

I would also like to pull the name out as well, instead of a static card title.

Any help would be great.

Thank you

Where do you set the variable ”days”?

 {% set days = …… %}

All comes from State Att. I had the Garbage Collection Add On, and with that being depreciated, I am trying to rework it for Local Cal.

Nope, for your ”if” statement to work you need to set the variable ”days”. Use my example above with your expression instead of ……

The example you posted relies on inspecting the attributes of calendar.bin_collection and assumes they contain information about the next scheduled event. However, it may not always report information about the next scheduled event.

  • If you have an All Day event, it will supercede all other scheduled events for that day. In other words, the calendar’s attributes will contain information about the All Day event and nothing else.

  • If you have two events scheduled at the same time, only one will be reported in the calendar’s attributes.

If you are certain that your calendar will never contain All Day events or concurrent events, then you can continue to use the technique you’re using. Otherwise you should consider using a Calendar Trigger or the new calendar.list_events service call.

Thanks for the reply, I will only be using the Calendar for these events.

@Mats789 Are you able to elaborate anymore?

I’m on vacation, so I hate to type on the phone :sweat_smile: but have you tried the following?

{% set days = (state_attr('calendar.bin_collection', 'start_time') | as_timestamp - today_at('00:00') | as_timestamp) / (60 * 60 * 24) %}
{% if days == 0 %}
  Today
{% elif days == 1 %}
  Tomorrow
{% else %}
  {{ days }} days
{% endif %}

So what you are implying is that the Calendar never contains two All Day events on the same day? For example, because your Calendar only tracks one kind of collection (Recycling or Trash but not both) or it does track more than one kind but they’re never on the same day.

@123 you are correct, it will on track…one trash collection on a one day, never both and never on the same day


@Mats789 This worked wonderfully. I just need to get it to be a single integer but go back to your Vacation!! haha :smiley:

Good, use this:

{{ days | int }} days

Now back to :sunny: :tropical_drink::grinning:

1 Like

FWIW, here’s another way to do the same thing.

The date arithmetic employs datetime objects and a map is used to convert days from an integer value to text.

{% set d = (state_attr('calendar.bin_collection', 'start_time') | as_datetime | as_local - today_at()).days %}
{{ {0:'Today', 1:'Tomorrow'}.get(d, d~' days') }}
2 Likes