Problems with Conditions for Calendar Automation

Hi Community,

I´ve setup a simple automation for testing with google calendar integration. In documents I´ve found, that yaml configuration isn´t possible with new releases. So I´ve tried a simple automation triggered by calendar event.

The automation should show in the events, if there are a specific word included for automation. But the description has more words…

It is possible to filter only this word, and ignoring other words in this case?

Here my configuration:

alias: test01
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:10:0"
    entity_id: calendar.privat_mm_pool
condition: []
action:
  - condition: state
    entity_id: calendar.privat_mm_pool
    attribute: description
    state: AutoStart1
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.mediarack_switch
mode: single

here are my trace results:
image

I´ve also tried wildcards ’ % " eg.

When using Calendar triggers you should really be using trigger variable based template conditions, not State conditions… especially when using offsets or calendars that may have concurrent events. This will also allow you to use a search function.

alias: test01
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:10:0"
    entity_id: calendar.privat_mm_pool
condition: []
action:
  - condition: template
    value_template:  >
      {{ trigger.calendar_event.description is search('AutoStart1') }}
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.mediarack_switch
mode: single
3 Likes

Thank you very, very much. I´ve found in docs this template conditions, but not understand it correctly.
This code is working correctly.

alias: test01_condition-before-action
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.privat_mm_pool
condition:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.description is search('AutoStart1') }}
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.mediarack_switch
mode: single

1 Like

Maybe I don´t need this description in my automation…
Do you have some ideas for another case?

  1. I have an automation 5 hours before the event is starting. As example, My Switch is turning on.
  2. At the end of my event, this switch is turning off, normally.
  3. BUT, if another event is in calender within 5 hours, this switch has not to turned off, at the end.
  4. When the next event starts, normally, the automation is turning the switch to on. But in this case, the automation has performed before, so nothing have to do.

The Condition here is not any description, message or something, only if there an event in this calendar.
I hope, that is not to confused…

If I understand correctly, you are asking whether there is a way to check if there are future events in your calendar. The calendar integration doesn’t have that functionality, at least not currently. However, that function can be added using the HASS Calendar Addon.

Thank you very much.
I´ve tried to install the addon, and found this thread which helps me, to install and configure it. I´ve found now a sensor, where I can found my entries.
But I don´t know, how to use this data. Do you have additional informations for me?

From your earlier post, I understand that you have an automation that is triggered 5 hours before an event starts to turn a switch ‘on’; then turns it off at the end of the event.

You want to add a condition to the “turn off” part of the automation so the switch is not turned off when there is another event within 5 hours of the end event.

And you want to add a condition to the “turn on” part so it only runs if the switch is “off”. FWIW, this type of conditional logic is unnecessary for most switch entities.

trigger:
  - platform: calendar
    event: start
    entity_id: calendar.switch_schedule
    offset: "-00:05:00"
    id: 'on'
  - platform: calendar
    event: end
    entity_id: calendar.switch_schedule
    offset: "0:0:0"
    id: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'on'
          - condition: state
            entity_id: switch.YOUR_SWITCH
            state: 'off'
        sequence:
          - service: switch.turn_on
            entity_id: switch.YOUR_SWITCH
      - conditions:
          - condition: trigger
            id: 'off'
          - condition: state
            entity_id: switch.YOUR_SWITCH
            state: 'on'
          - condition: template
            value_template: >
              {{ state_attr('sensor.YOUR_CALENDAR_SENSOR', 'data') 
              | map(attribute="startDateISO") | map('as_datetime')
              | select('le', now() + timedelta(hours=5) ) | list | count <= 1 }}
        sequence:
          - service: switch.turn_off
            entity_id: switch.YOUR_SWITCH
1 Like

Many Thanks. It looks perfect!
I will give it a try later, but in my configuration it looks good.
Will let U know…

Thanks to your fantastic works and explanation.

Tested. And it works!
Thanks to you!

Please can you tell me, where I can find more information about the parameters for value_template, and what this exactly means?

condition: template
value_template: >
  {{ state_attr('sensor.pool', 'data')  | map(attribute="startDateISO") |
  map('as_datetime') | select('le', now() + timedelta(hours=5) ) | list | count
  <= 1 }}

Home Assistant uses the Jinja templating language. There’s no single source that covers all the things you can do with templates in HA, but make sure to check out:

Templating Information Sources

Home Assistant Templating: This is the the most comprehensive source for HA-specific functions

Jinja Docs: These docs are not geared toward new/casual users, but they contain a lot of important information.

Jinja for Ninjas: @skalavala has put together a well organized tutorial and group of examples. It’s been a while since this was updated, so there are better/easier ways to do some of the things shown, but it’s still a good source.

Many Python methods also to work in templates. I’ve never seen a complete list of which methods work and which don’t… But the Official Python docs have come in handy for me multiple times to figure out what’s going on in templates posted by some of the more advanced users on this forum.

For conditions you want something that returns either true or false. In this case I want it to return ‘true’ if there are not any events in the next 5 hours, so that the switch is turned off.

  1. state_attr('sensor.pool', 'data')
    Get all the information from the “data” attribute of the calendar sensor. This is all the sub-attributes organized by the calendar event they belong to.

  2. | map(attribute="startDateISO")
    From all that data about each calendar event, get just the sub-attribute called “startDateISO”.

  3. | map('as_datetime')
    The data from Step 2 is returned as a string, but I needed to be a different type of data to do time math in the next step, so convert it to a datetime object.

  4. | select('le', now() + timedelta(hours=5) )
    From all the those datetime objects, keep any that are within the next 5 hours. ‘le’ means “less than or equal to”.

  5. | list | count
    Put anything that was kept from Step 4 in a list and count how many of them there are.

  6. <= 1
    This is where you ask the actual question to get your true/false answer: “Is the count from Step 5 less than or equal to one?” If the count was 0, then there aren’t any events in the calendar within the next 5 hours… The template will return ‘true’, the automation’s actions will continue, and the switch will be turned ‘off’. Otherwise, if the count was 1 or more, then there is an event in the calendar within the next 5 hours… The template will return ‘false’ and the switch will stay ‘on’.

2 Likes

Thank to you, I will inform me about this, what you have shared to me.

One more question:
In my example, automatisation will start 5 hours before, if there are an event in my calendar.
What will happen, if an event will add maybe 4 hours, before an event is starting?
Is there a way, to configure this automatisation, to start max. 5 hours before the event is starting, but if there are not so much time, this automation has to start direct?

It is unclear what you are asking.

If you want extra triggers to make sure the switch turns on you can just add them to the trigger section. Make sure they all have id: 'on' so that they go down the correct path of the Choose action:

trigger:
  - platform: calendar
    event: start
    entity_id: calendar.switch_schedule
    offset: "-5:00:00"
    id: 'on'
  - platform: calendar
    event: start
    entity_id: calendar.switch_schedule
    offset: "-4:00:00"
    id: 'on'
  - platform: calendar
    event: start
    entity_id: calendar.switch_schedule
    offset: "0:0:0"
    id: 'on'
  - platform: calendar
    event: end
    entity_id: calendar.switch_schedule
    offset: "0:0:0"
    id: 'off'

OK, I understand. I´ve only asked, cause maybe there is another template like “</=” or something else.
Thank you very much, this is working for me.

For Understanding:
This automation will start 5 hours before there is an event in calendar. Sometimes, a new event will added, maybe 3 hours, or 4 hours of the start, before.
So If I understand correctly, Automation will only start 5 hours before event is starting, but if a new entry will insert to calendar, and there isn´t enough time for trigger (in this case 5 hours) so nothing will happen.
I can correct this and add more trigger options with ID on.

Yes. However, be aware that Calendars are only reloaded every 15 minutes. If you add a calendar event less than 15 minutes before it is meant to trigger an automation, the automation will not trigger.