Act on one of multiple calendar entries

I have a HA calendar that I use to set my WLED presets (the next few weeks will be fall colors and then in December will shift to Christmas). This works great if I don’t have overlapping events. I want to have exceptions, for example in the middle of December I might want to change the preset to a football team’s colors on the day they play. The automation I have needs there to be only one event per day (which makes me have to change the Christmas event to not occur on football days).
Here is what I’m using now.

condition:
action:

  • choose:
    • conditions:
      • condition: state
        entity_id: calendar.lighting_holidays
        attribute: message
        state: Eagles
        sequence:
      • service: notify.mobile_app_scott_iphone_15_pro
        data:
        message: Setting preset to Eagles
      • device_id: bf6453e078f4ac144042d859fbb5e40e
        domain: select
        entity_id: 0bccd4f724596189f104c05ba8be3110
        type: select_option
        option: Eagles
    • conditions:
      • condition: state
        entity_id: calendar.lighting_holidays
        attribute: message
        state: Thanksgiving
        sequence:
      • service: notify.mobile_app_scott_iphone_15_pro
        data:
        message: Setting to Thanksgiving
      • device_id: bf6453e078f4ac144042d859fbb5e40e
        domain: select
        entity_id: 0bccd4f724596189f104c05ba8be3110
        type: select_option
        option: Thanksgiving

This is doing exactly what the state says. State only shows “Thanksgiving”. But if I call the service “List events” against that entity, I get two events “Eagles” and “Thanksgiving”.

So…I tried to call “List events” and store the response in a variable and then act conditionally on the response, but I can’t get it to work. Maybe I’m just not referencing the sections of the variable correctly. Here is the response from “List events” and the automation I have.
events:

  • start: “2023-11-14”
    end: “2023-11-17”
    summary: Thanksgiving
    description: “”
  • start: “2023-11-15”
    end: “2023-11-16”
    summary: Eagles
    description: “”

action:

  • service: calendar.list_events
    data:
    duration:
    hours: 24
    minutes: 0
    seconds: 0
    target:
    entity_id: calendar.lighting_holidays
    response_variable: calendar_events
  • choose:
    • conditions:
      • condition: template
        value_template: “{{ calendar_events.events.summary == ‘Eagles’ }}”
        sequence:
      • service: notify.mobile_app_scott_iphone_15_pro
        data:
        message: Set to Eagles
    • conditions:
      • condition: template
        value_template: “{{ calendar_events.events.summary == ‘Thanksgiving’ }}”
        sequence:
      • service: notify.mobile_app_scott_iphone_15_pro
        data:
        message: Set to Thanksgiving

My thinking was that if there are multiple entries, I could order the condition options to select the less frequently used presets first. For example…I see Christmas would be set for most of the month of December with a few days of a football team. I don’t think I need to account for two different special case settings on the same day.

Important stuff, I know. But thanks for any input.

Please follow Community Guideline #11, by formatting code blocks properly… it makes it much easier for us to read and to respond with examples.

The following template selects for the event with fewer days between start and end, so it will pick the game-day events over the multi-day Thanksgiving or Christmas events.

...
  - variables:
      summary: |
        {% set ns = namespace( e_len = [])%}
        {% for ev in calendar_events.events %}
        {% set ns.e_len = ns.e_len + [{'summary':ev.summary, 'days': (ev.end|as_datetime - ev.start|as_datetime).days }] %}
        {% endfor %}
        {{ (ns.e_len|sort(attribute='days'))[0]['summary'] }}
  - service: notify.mobile_app_scott_iphone_15_pro
    data:
      message: Set to {{ summary }}

Hi Drew.
Sorry for the late reply. This worked perfectly.
Thank you

I saw that it was complaining about formatting and it offered to fix, but obviously it didn’t. Will read up on formatting and hopefully it won’t happen again.

1 Like