Need help: Setting Wled preset based on google calendar event when there are multiple events

I have an automation for setting my Wled roof lights based on a google calendar event. This works fine as long as I only have one event - once I have two (example: all month event called December, then a single day event called Christmas Day) I want to get the event data for the single day event, but it only pulls the event info for the all month event.

I tried to follow the info in this posting (Act on one of multiple calendar entries ) however I could not figure out how to adapt my automation to use this. I am hoping someone can help me out. Get templating to work eludes me.

Here is my current automation yaml

alias: Set Wled Preset
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: "On"
    enabled: true
  - platform: time
    at: "22:30:00"
    id: "Off"
    enabled: true
  - platform: conversation
    command: turn on roof
    id: "On"
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: "On"
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: Canada Day
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: Canada Day
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: New Years Eve
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: New Years Eve
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: Christmas Eve
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: Christmas Eve
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: Christmas Day
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: Christmas Day
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: Thanksgiving Day
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: Thanksgiving Day
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: January
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: January
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: February
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: February
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: March
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: March
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: April
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: April
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: May
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: May
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: June
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: June
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: July
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: July
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: message
                    state: August
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: August
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: September
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: September
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: October
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: October
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: November
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: November
                    target:
                      entity_id: select.wled_preset
              - conditions:
                  - condition: state
                    entity_id: calendar.wled
                    attribute: description
                    state: December
                sequence:
                  - action: select.select_option
                    metadata: {}
                    data:
                      option: December
                    target:
                      entity_id: select.wled_preset
          - action: light.turn_on
            target:
              entity_id: light.wled
            data: {}
          - data:
              message: Wled has turned on
              title: WLED
            enabled: false
            action: notify.notify
          - data: {}
            target:
              entity_id: switch.christmas_indoor_lights
            action: switch.turn_on
      - conditions:
          - condition: trigger
            id: "Off"
        sequence:
          - data: {}
            target:
              device_id: 8827056537b26a7e51609866ff312aba
            action: light.turn_off
          - data: {}
            target:
              entity_id: switch.christmas_indoor_lights
            action: switch.turn_off
          - data:
              message: Wled has turned off
              title: WLED
            enabled: false
            action: notify.notify
mode: single

Hi Kyle. Sorry I didn’t see this sooner. I’ll try to look at in a few when I finish what I’m doing.

There have been a couple changes to getting the calendar event since the solution of the post you linked. You should be able to use the template from the post with a couple alterations, and substituting “description” for “summary”.

Since it looks like you have set up your selector options and the descriptions from the calendar events to match, you can simplify the Choose options you are using to change the selection too.

alias: Set Wled Preset
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: "On"
    enabled: true
  - platform: time
    at: "22:30:00"
    id: "Off"
    enabled: true
  - platform: conversation
    command: turn on roof
    id: "On"
condition: []
action:
  - alias:  "Get today's events from the calendar"
    action: calendar.get_events
    target:
      entity_id: calendar.wled
    data:
      start_date_time: "{{ today_at() }}"
      end_date_time: "{{ today_at('23:59') }}"
    response_variable: calendar_events
  - variables:
      description: |
        {% set ns = namespace( e_len = [])%}
        {% for ev in calendar_events['calendar.wled'].events %}
          {% set ns.e_len = ns.e_len + 
          [{'description':ev.description, 
          'days': (ev.end|as_datetime - ev.start|as_datetime).days }] %}
        {% endfor %}
        {{ (ns.e_len|sort(attribute='days'))[0]['description'] }}
  - choose:
      - conditions:
          - condition: trigger
            id: "On"
          - condition: template
            value_template: "{{ description not in ['', none] }}"
        sequence:     
          - action: select.select_option
            metadata: {}
            data:
              option: "{{ description }}"
            target:
              entity_id: select.wled_preset
          - action: light.turn_on
            target:
              entity_id: light.wled
            data: {}
          - action: notify.notify
            data:
              message: Wled has turned on
              title: WLED
            enabled: false
          - action: switch.turn_on
            data: {}
            target:
              entity_id: switch.christmas_indoor_lights
      - conditions:
          - condition: trigger
            id: "Off"
        sequence:
          - action: light.turn_off
            data: {}
            target:
              device_id: 8827056537b26a7e51609866ff312aba
          - action: switch.turn_off
            data: {}
            target:
              entity_id: switch.christmas_indoor_lights
          - action: notify.notify
            data:
              message: Wled has turned off
              title: WLED
            enabled: false
1 Like

Thank you @Didgeridrew that works wonderfully - And I now have a much better idea how the templating works - which will help a few other automations that I had given up on. Thank you.

1 Like