Make automation with google calender

Hi,
I’m trying to make a automation (start 3 lights at 05:30) if I’m start working at 06:30 (in the code, the calendars event should start in 4000secunds from 05:30). If I have a day off or start working later then 06:30, nothing should happen.
I got the google calendar working in my HA. But I can not get the automation to work. I’m new with HA, but my friend could not help me either…

In google_calendars.yaml

- cal_id: [email protected]
  entities:
  - device_id: maria_tjg
    ignore_availability: true
    name: maria_tjg
    track: true
    max_results: 31

In automations.yaml:

- id: '16XXXXX'
  alias: Turn on the lights at 05:30 if Maria starts work at 06:30
  description: Slå på lyset i vardagsrummet kl. 05:30 om Maria börjar kl 06:30
  trigger:
  - platform: time
  at: '05:30:00'
  condition: template
  value_template: "{{ ((as_timestamp(states.calendar.maria_tjg.attributes.start_time) - as_timestamp()) < 4000) and ((as_timestamp(states.calendar.maria_tjg.attributes.start_time) - as_timestamp(now())) > 0) }}"
  action:
  - type: turn_on
    device_id: cXXXX
    entity_id: light.8XXXX
    domain: light
  - type: turn_on
    device_id: 4XXXXX
    entity_id: light.3XXXX
    domain: light
  - type: turn_on
    device_id: dXXXXXX
    entity_id: light.bfXXXXXX
    domain: light
  mode: single

When I add the cod in Developer Tools, I got the answer

Result type: string
condition: - condition: template value_template: “False”
This template updates at the start of each minute.
This template listens for the following state changed events:

  • Entity : calendar.maria_tjg

I found the code here:

You can set an offset of one hour in your google calendar so it will turn on an hour before the event starts.

- cal_id: [email protected]
  entities:
  - device_id: maria_tjg
    ignore_availability: true
    name: maria_tjg
    track: true
    max_results: 31
    offset: '01:00'

Then you could do this, use this condition.

- id: '16XXXXX'
  alias: Turn on the lights at 05:30 if Maria starts work at 06:30
  description: Slå på lyset i vardagsrummet kl. 05:30 om Maria börjar kl 06:30
  trigger:
  - platform: time
    at: '05:30:00'
  condition:
  - condition: template
    value_template: "{{ is_state_attr('calendar.maria_tjg', 'offset_reached', true )}}"
  action:
  - type: turn_on
    device_id: cXXXX
    entity_id: light.8XXXX
    domain: light
  - type: turn_on
    device_id: 4XXXXX
    entity_id: light.3XXXX
    domain: light
  - type: turn_on
    device_id: dXXXXXX
    entity_id: light.bfXXXXXX
    domain: light
  mode: single

Will that not turn on the light one hour before, if I have a event in the calendar like 09:00 or 15:00?

You were to quick :slight_smile:, I was editing it… I believe the last one will do what you want

Thanks, I will try tomorrow. :slight_smile:

alternatively, you could do this too, this one might be less prone to false positives. You needn’t do the offset thing here. Make sure you have the sensors of date and time working in your instance, they can come in handy for various automations:

then use this condition in your automation

- id: '16XXXXX'
  alias: Turn on the lights at 05:30 if Maria starts work at 06:30
  description: Slå på lyset i vardagsrummet kl. 05:30 om Maria börjar kl 06:30
  trigger:
  - platform: time
    at: '05:30:00'
  condition:
  - condition: template
    value_template: "{{state_attr('calendar.maria_tjg', 'start_time')[0:10] == states('sensor.date') and state_attr('calendar.maria_tjg', 'start_time')[11:16] == '06:30'}}"
  action:
  - type: turn_on
    device_id: cXXXX
    entity_id: light.8XXXX
    domain: light
  - type: turn_on
    device_id: 4XXXXX
    entity_id: light.3XXXX
    domain: light
  - type: turn_on
    device_id: dXXXXXX
    entity_id: light.bfXXXXXX
    domain: light
  mode: single

I did not get it to work.
I have tried both have dates and time in config and in sensors.yaml.

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'

Don’t forget to restart HA every time you make changes in any .yaml . Can you show me this?:

and this:

copy and paste the attributes and states prefferably

show me this too, make sure you print screen with the results box from the right


Im on the right place?

Nope, that’s not the right place :slight_smile:.

First off, did you restart HA? perhaps is that.

The information I need can be found in “Developer tools” in the left menu. There you’ll find both “states” and “Template”. First two informations I requested you can be found under “states”. The last one, under “Template”

Copy this in template:

{{state_attr('calendar.maria_tjg', 'start_time')[0:10]}}
{{state_attr('calendar.maria_tjg', 'start_time')[11:16]}}

always restart it, I have learn that at least… =)

1 Like

everything looks fine so far. Now go to “Template” under “Developer tools” and copy and paste the code below like in my last picture. Make sure you print screen the results box from the right too.

{{state_attr('calendar.maria_tjg', 'start_time')[0:10]}}
{{state_attr('calendar.maria_tjg', 'start_time')[11:16]}}

It just take some time when my kids want to do computerstuff at the same time… :confused:

heeehe… Everything’s looking fine. This automation will work only if the event in your google calendar starts exactly at 06:30 and in the same day as the current day. For example, with your next event it won’t trigger, condition will be false.

I assumed that. When you start working at 06:30, there’s an event in your google calendar starting exactly at that hour, isn’t there?

I have tried to add a even in the calendar at 13:50 (that was 06:30) and then change the time it should check to 13:30 (that was 05:30). But it did not work. But I can try it tomorrow morning.

I don’t understand this 13:50 equals 06:30? :S

Try adding this event and send me this once more:

do you speak swedish?