Read Calendar Event Summary To Determine Garbage Type

The city I live in has rotating garbage pickup. Week 1 will be recycling and week 2 will be refuse (non-recyclables). The city publishes a Google calendar so I was able to bring this into HA (created new calendar on Google and integrated that).

I’ve read many posts on here that have been life savers and I’ve been able to get to a place where I can count the number of calendar entries and if there is 1, then it is the garbage rotation schedule. If nore than 1 then the city calendar is broken and if there is no calendar entry then do nothing.

The goal of this automation is to drive a color-coded garbage truck image on my dashboard so the kids know which they have to take care of that day (blue and grey trucks).

Here is the code I have so far that imports the calendar events.

NOTE: The notify mobile device is just being used for testing and will be removed once in production. I also count the calendar entries as a way to trap for errors once this is in production (more than 1 entry means the city calendar is fubar).

alias: Check Garbage Type
description: ""
triggers:
  - at: "01:00:00"
    trigger: time
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - target:
      entity_id: calendar.the_fam
    data:
      start_date_time: "{{ today_at('00:00') + timedelta(days=1) }}"
      end_date_time: "{{ today_at('00:00') + timedelta(days=2) }}"
    response_variable: calendar_events_var
    action: calendar.get_events
  - data:
      message: >
        {% set calendar_events =
        calendar_events_var['calendar.the_fam']['events'] %}

        {% if calendar_events | count == 1 %}
          You have the following events happening tomorrow:
          {% set calendar_events = calendar_events_var['calendar.the_fam']['events'] %}
          {%- for event in calendar_events -%}
          - {{ event.summary }} at {{ (event.start | as_datetime ).strftime('%H:%M') }}
          {% endfor %}
        {% endif %}
    action: notify.mobile_app_slysiphone
  - action: input_boolean.toggle
    data: {}
    enabled: true
    target:
      entity_id: input_boolean.garbage

The goal is…

  • On a day where there is no garbage, I will display a “Welcome” banner on the dashboard.
  • On a day where there is garbage pickup, I want to display the appropriate color truck (blue or grey).

Where I am hung-up is that I need to read the contents of the event summary to determine which image to display. I wonder if someone here who has been at this longer than my 30 days can help me solve this issue. I need to be able to read the event summary text to determine which color. I am able to send myself a notification that inludes the events so I know it is successfully pulling those. The event summary for each is…

  • “Recycling and organics” for blue bin days
  • “Organics and garbage” for grey bin days

I suppose the simplest approach would be to flip a toggle and use the toggle state to determine the banner to display as I’ve shown in my code.

I would probably set up an Input select helper so you could have 3 options i.e. “Welcome”/“Blue”/“Green”… that should make the dashboard control a bit easier.

Before we get too far down the path, the calendar name makes me think that you may be planning on using it for more than just trash. If that is the case we should make sure to construct the automation so you don’t have to come back and fix it when that happens. For example, {% if calendar_events | count == 1 %} will only be true if there is exactly one event during the queried time period, and you have not provided an else or specific selection criteria to insure that only trash events have made it that far.

Pretty perceptive on the name. No, I am using this calendar for testing but in production will use the city calendar that I have imported into Google. It only has single entries on days for garbage collection and the summary states which type so in this case, there will only ever be a single calendar entry however I can use the count to trigger error trapping but that’s not part of this first use-case.

Something like the following is what I was thinking:

alias: Check Garbage Type
description: ""
triggers:
  - at: "01:00:00"
    trigger: time
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - action: calendar.get_events
    target:
      entity_id: calendar.the_fam
    data:
      start_date_time: "{{ today_at('00:00') + timedelta(days=1) }}"
      end_date_time: "{{ today_at('23:59') + timedelta(days=1) }}"
    response_variable: calendar_events_var
  - variables:
      events: "{{ calendar_events_var['calendar.the_fam']['events'] }}"
  - action: input_select.select_option
    target:
      entity_id: input_select.garbage_type
    data:
      option: |
        {% if events[0] is undefined %}
          Welcome
        {% else %}
          {{ 'Blue' if events[0].summary | lower is search('recycling') else 'Green' }}
        {% endif %}

If you wanted to stick with the input boolean, the final action(s) could be:

...
  - action: input_boolean.{{ 'off' if (events[0] is undefined or events[0].summary|lower is search('recycling')) else 'on' }}
    target:
      entity_id: input_boolean.garbage
  - action: input_boolean.{{ 'off' if (events[0] is undefined or not events[0].summary|lower is search('recycling')) else 'on' }}
    target:
      entity_id: input_boolean.recycling

Thanks for this. Let me restate to make sure I understand correctly.

Since I am looking to display one of three images (welcome, blue truck or grey truck) then I suspect I would use the “false” state for both of those booleans.

The logic in the card would look something like…

if input_boolean.garbage = true then show the grey truck
If input_boolean.recycling = true then show the blue truck
else if both nput_boolean.garbage and nput_boolean.recycling = false then show welcome

That sound right?

Yep that’s what I pictured.

In case anyone else finds themselves here, I thought I would share the final code with the help that @Didgeridrew provided.

alias: Garbage Check
description: ""
triggers:
  - at: "01:00:00"
    trigger: time
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - action: calendar.get_events
    target:
      entity_id: calendar.the_fam
    data:
      start_date_time: "{{ today_at('00:00') + timedelta(days=2) }}"
      end_date_time: "{{ today_at('23:59') + timedelta(days=2) }}"
    response_variable: calendar_events_var
  - variables:
      events: "{{ calendar_events_var['calendar.the_fam']['events'] }}"
  - action: >-
      input_boolean.{{ 'turn_on' if (events[0].summary|lower is
      search('recycling')) else 'turn_off' }}
    target:
      entity_id: input_boolean.garbage_blue
  - action: >-
      input_boolean.{{ 'turn_on' if (events[0].summary|lower is
      search('garbage')) else 'turn_off' }}
    target:
      entity_id: input_boolean.garbage_grey

With each of those 2 input booleans, I can then build an if statement in my markdown card to decide what to display based on the on/off status of those 2 values.

Appreciate the quick and helpful replies! Thank you!