Calendar with multiple events mess up my automations

I’m having issues getting automations trigger correctly based on the correct event from my calendar. A little explanation first: I have a calendar with multiple events on the same day, some are all-day or multiple-day events and other are timed events. For example:
image

I have an automation that should activate a boolean on the start of an event that contains the text “Test” and deactivates it at the end of that event. However due to the fact that other events in the same calendar start the automation is triggered and it finds the “Test for HA” event first (I think?) it switches the boolean.

The automation code is:

alias: "Kalender: Meerdaagse test"
description: ""
triggers:
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: start
    offset: "0:0:0"
    id: "on"
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: end
    offset: "-0:0:0"
    id: "off"
conditions:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ 'Test' in trigger.calendar_event.summary }}      "
      - condition: template
        value_template: "{{ 'Test' in state_attr('calendar.gezins_agenda','message') }}"
actions:
  - action: input_boolean.turn_{{trigger.id}}
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.calendar_test
    enabled: true
mode: single

How do I get this automation to check the correct event? The automation should only switch the boolean at the beginning or end of the event with the “Test” text in it, it should ignore the other events.

By including the condition…

 - condition: template
   value_template: "{{ 'Test' in state_attr('calendar.gezins_agenda','message') }}"

… you are allowing for the possibility that the actions be executed when the trigger event is not the “Test” event. If you only want the action to happen when the “Test” event starts and stops, remove that condition:

alias: "Kalender: Meerdaagse test"
description: ""
triggers:
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: start
    offset: "0:0:0"
    id: "on"
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: end
    offset: "-0:0:0"
    id: "off"
conditions:
  - condition: template
    value_template: "{{ 'Test' in trigger.calendar_event.summary }}" 
actions:
  - action: input_boolean.turn_{{ trigger.id }}
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.calendar_test
mode: single

Sadly that doesn’t work.
As shown in the image in the first post there are more events. The automation should only switch the input_boolean at the beginning or the end of the “Test for HA” event. But the automation also triggers when the event “Schoonmaakster komt” starts or stops and because the condition then checks if the word “Test” is present in the first event it finds, which is “Test for HA” so it switches the boolean accordingly.
I need a way to match the start time, end time and message within the condition.

I don’t think is correct, it should work without that second condition.
The behaviour you describe will be caused by that second condition.

I’ve tested it with only the first condition but it doesn’t work. I keep getting false positives resulting in switching the boolean when I don’t want to. I’ve changed my script to the following but am not sure it will work (not yet tested it):

alias: "Kalender: Meerdaagse test"
description: start en eind schakelt de boolean.
triggers:
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: start
    offset: "-0:0:0"
    id: "on"
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: end
    offset: "0:0:0"
    id: "off"
conditions: []
actions:
  - action: calendar.get_events
    metadata: {}
    data:
      duration:
        hours: 6
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.gezins_agenda
    response_variable: FamEvents
  - if:
      - condition: template
        value_template: |-
          {{
            set events = FamEvents['calendar.gezins_agenda'].events
              | for e in events
                | set st = e.start as_datetime
                | set et = e.end as_datetime
                | set nt = now() as_datetime
                | if nt >= st and nt <= et and select('search', 'Test')
                  | list
              | count >= 1
          }}
    then:
      - action: input_boolean.turn_{{trigger.id}}
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.calendar_test
        enabled: true
      - action: notify.mobile_app_pixel_7a
        metadata: {}
        data:
          title: kalender Test
          message: De schakelaar staat nu {{trigger.id}}
mode: single

This is not a valid Jinja template.

Please describe your testing procedure. I have multiple working automations using that method.

Yes, that’s how the trigger works… you need the right condition to limit which triggers actually cause the action to execute.

That is not what either of those conditions do… The first condition, since it is based on the trigger variable, only has access to the calendar event that caused the trigger to fire this time.

The second condition is based on the state object of the calendar entity. This can be the data of any active calendar event or that of the next upcoming event if none are active… it is not a reliable source of data for what you are trying to do.

Well after quite some testing I think I’ve tacked it.
The script now looks like this:

alias: "Kalender: Meerdaagse test"
description: start en eind schakelt de boolean.
triggers:
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: start
    offset: "-0:0:0"
    id: "on"
  - trigger: calendar
    entity_id: calendar.gezins_agenda
    event: end
    offset: "0:0:0"
    id: "off"
conditions: []
actions:
  - action: calendar.get_events
    metadata: {}
    data:
      duration:
        hours: 6
        minutes: 0
        seconds: 0
    target:
      entity_id: calendar.gezins_agenda
    response_variable: FamEvents
  - if:
      - condition: template
        value_template: |-
          {% set events = FamEvents['calendar.gezins_agenda'].events %}
          {% set foo = namespace(found=False) %}
          {% set nt = as_timestamp(now()) %}
          {% for e in events -%}
            {% set st = as_timestamp(e.start) %}
            {% set et = as_timestamp(e.end) %}
            {% if nt >= st and 'Test' in e.summary %}
              {% set foo.found = True %}
            {% endif %}
            {% if nt <= et and 'Test' in e.summary %}
              {% set foo.found = True %}
            {% endif %}
          {% endfor %}
          {{ foo.found }}
    then:
      - action: input_boolean.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.calendar_test
    else:
      - action: input_boolean.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: input_boolean.calendar_test
mode: single

It probably can be simplified but that will be for another day.