Google Calendar Automation with Entity in Calendar Entry

I am trying to set up an Automation that will pull events from Google Calendar and turn on and off the entity based on the calendar even start and stop time. I’d like to retrieve the entity_id of the target entity from the calendar event itself. I think I should be using the “message” attr, but I must have a problem with how I am building the action template.

- id: '1570635579387'
  alias: Calendar On
  trigger:
  - entity_id: calendar.homeassistant
    platform: state
    to: 'on'
  condition: []
  action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: "{{ state_attr(trigger.entity_id, 'message') }}"
    

Well, as it turns out, I think it is actually working. I may not have restarted HA. Who knows.

I’m going to leave this here, though, because I can’t find anyone else who is doing this. With the following two Automations, I am able to create a calendar event called, for example, switch.kitchen_lights (or any other entity), and it will turn the device on when the event starts and off when it ends. I have recurring calendar events set up to automate my lighting.

It would be easy to add a condition, maybe on the Location attr, that would allow the event to only apply during certain conditions. For example, I am planning on adding a condition where Location equals “vacation” to allow me to only automate when the system is in Vacation mode.

- id: '1570635579387'
  alias: Calendar On
  trigger:
  - entity_id: calendar.homeassistant
    platform: state
    to: 'on'
  condition: []
  action:
  - data_template:
      entity_id: '{{ state_attr(trigger.entity_id, ''message'') }}'
    service: homeassistant.turn_on
- id: '1570641132088'
  alias: Calendar Off
  trigger:
  - entity_id: calendar.homeassistant
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - service: homeassistant.turn_off
    data_template:
      entity_id: '{{ state_attr(trigger.entity_id, ''message'') }}'

I did uncover a problem with Google Calendar when using it this way. It appears that HA doesn’t see two events that are happening at the same time, so you only get the one that was added/updated last. I am trying to change this script to use the Description field so that I could put multiple entities in the description and have it iterate over the list to turn them all on.

If anyone has any experience on how to do this in a data template, splitting a field by some separator and then doing something on each index of that array, I would love to hear it. I haven’t found a way to make that work.

Did you ever find a solution to this, i am experiencing the same issue