Check in a condition whether a specific calendar entry exists today

Be aware that there’s a potential problem with using any condition to check a calendar entity.

  • If you have an all-day event, its details will appear in the calendar entity and not any of the other events scheduled that day.

  • If you more than one event scheduled at the same time, only one of them will appear in the calendar entity.

That’s why it’s recommended to use a Calendar Trigger to detect the desired calendar event.

In your case, you can create a Trigger-based Template Binary Sensor with a Calendar Trigger that detects if the cleaning lady is scheduled to arrive today. Then your automation can use a State Condition to check the binary_sensor’s state. EDIT see TheFes’s post below

Thank you very much for your feedback. I must admit that I do not understand much of what you are talking about. I am quite new and still trying to find my way around :slightly_smiling_face:

Do you eventually have an example for me how to set up a trigger-based template binary sensor?

Thank you very much in advance.

You don’t need to create a template sensor, you can just run the service call in the automation and use the service response:

I simplified the automation (I assume you want to add more, but now it is overly complicated for what it does)

alias: vacuum_clean
description: ""
trigger:
  - platform: time
    at: "10:30:00"
action:
  - service: calendar.get_events
    target:
      entity_id: calendar.name_of_calendar
    data:
      start_date_time: "{{ today_at() }}"
      duration:
        hours: 24
    response_variable: calendar
  - if:
      - condition: template
        value_template: >
          {{
            calendar['calendar.name_of_calendar'].events
              | map(attribute='summary')
              | select('search', 'Reinigung')
              | list
              | count == 0
          }}
    then:
      - service: vacuum.start
        target:
          device_id: XXXXXXXXXX
mode: single

Note, you need to adjust the entity of the calendar in two places. In the service call to get the events, and in the template of the if action.

BTW, there is no need to censor device_id’s. They are randomly generate unique identifiers for the HA backend, nobody can do anything with them.
But I would advice to use entity_id’s instead of device id’s anyway

Hi @TheFes ,

thank you I will give this a try. But one question. If I set it up like this, then the vacuum will only run when the calendar entry “Reinigung” exists, right? It’s supposed to be the other way around. If calendar entry “Reinigung” exists for today, then do not start cleaning.

No, it only starts the vacuum when there are no entries with Reinigung in the calendar (the count of these items needs to be equal to 0)

1 Like

Okay, I’ll give it a try and give feedback. Thanks a lot

Hi @TheFes ,

after some days of testing I can say this does not work for me. I implemented it like you told me but now the vacuum does not start at all.

- conditions:
    - condition: trigger
      id:
        - "2"
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
  sequence:
    - service: vacuum.start
      metadata: {}
      data: {}
      target:
        device_id: <device_id>
      enabled: false
    - service: calendar.get_events
      target:
        entity_id: calendar.<name of calendar>
      data:
        start_date_time: "{{ today_at() }}"
        duration:
          hours: 24
      response_variable: calendar
    - if:
        - condition: template
          value_template: |
            {{
              calendar['calendar.<name of calendar>'].events
                | map(attribute='summary')
                | select('search', 'Reinigung')
                | list
                | count == 0
            }}
      then:
        - service: vacuum.start
          metadata: {}
          data: {}
          target:
            device_id: <device_id>

Any ideas?

Thx in advance :slight_smile:

With the code above it should always start on the trigger with id 2 and on the right weekdays, as you also have the vacuum.start service call before the if-then action.

But you should be able to tell where it goes wrong from the automation trace

I tried @TheFes way of using the calendar as a condition to turn some lights on at sunset (only if there are guests). But no matter which calendar I use or which word I am searching for, the visual editor always says that the condition is not met.

Tho whole thing looks like this:

description: >-
  turn on lights if 'test' in calendar
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition: []
action:
  - service: calendar.get_events
    target:
      entity_id: calendar.privat
    data:
      duration:
        hours: 24
    response_variable: calendar
  - if:
      - condition: template
        value_template: |
          {{
            calendar['calendar.privat'].events
              | map(attribute='summary')
              | select('search', 'Test')
              | list
              | count == 0
          }}
    then:
      - service: switch.turn_on
        target:
          device_id: light.lampe
        data: {}
mode: single

I waited till sunset today, but it didn’t trigger. Any idea what I did wrong?

@Sk1nk The visual editor won’t be able to check this condition, as the response variable is not defined when that check is done.

Do you currently have a calendar entry with Test in the summary?

Sorry, wasn’t able to tinker since we had guests :slightly_smiling_face:

If I run calendar.get_events it is there.

Then the result of that template will be false. It will be true if the number of calendar entities with Test in the summary is 0

So the solution would be to switch | count == 0 to 1? Or is there a way to trigger, if the - if: - condition: template returns a false?

If you want the condition to pass when there is a calendar event with Test in the summary, you should use > 0 instead of == 0

It worked!
I had a little typo at the name of the light but as I found that it finally worked yesterday! Thank you!

Hi @TheFes
I wanted to use this script to solve my problem (see this post) but it still gives me false triggers. I think it can be solved by checking whether the current date-time is within the start and end date of the found event.
Could you help me out here?

What do you have now?

Please see my other post… (sorry for the cross refs)

Thank you !
That’s exactly what I was looking for.

Is it possible to get info from the event (when count ==1), and use it in the “then” section ?
I want to send a notification with some detail of the event.

I found a solution with {{ calendar[‘calendar.xxx’].events[0].summary }} :slight_smile: