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:
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.
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.
- 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:
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’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
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.