How to trigger automation one hour before calendar event

Hi,
I have set a Google Calendar Event in HA.
How do I trigger an automation one hour before the calendar event ?
In the documentation I can see how to trigger automation exactly on the event time and not before.
Thanks in advance.

Hi there. I use this addon in NodeRED to query events in the future. I am very satisfied with that. Maybe that will help you.

There is the offset option which would apply for all events on that entity id. Useful if you pair down an entity with a filter such that all matching events would get this offset.

Else, you’ll need to add a time sensor if you don’t already have one. Make sure to add the one with both date and time at least.

Then you can set a trigger that compares the time sensor to the calendar sensor start_time and fires when it’s within range.

I can add an example later, but it’s super hard on mobile

Please, add an example. :pray:

Take a look at this topic. It’s the same base problem of triggering an automation before a set time (in this case the calendar event). There should be a complete example in there as well.

Thanx. Will try it

Come back if you face some issues or don’t understand something. I should be able to help, because I posted the solution xD

2 Likes

Not sure if this helps (also - I’m no coder so this is all found stuff that I use), but I use this flow in an alarm clock. It basically uses and offset that may be the sort of thing that you are looking for:

Screenshot 2020-02-10 at 12.52.41

[{"id":"d2bad75.095dd28","type":"trigger-state","z":"7342fdca.164ea4","name":"alarm time","server":"cfa79f02.db9d4","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"input_datetime.alarmclock_time","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":true,"state_type":"str","x":130,"y":100,"wires":[["4719f95.75ab108"],[]]},{"id":"4719f95.75ab108","type":"function","z":"7342fdca.164ea4","name":"set time - offset","func":"varAlarmTime = msg.payload;\nmsg.payload = {};\nmsg.payload.ontime = varAlarmTime\nmsg.payload.offtime = varAlarmTime\nmsg.payload.onoffset = -10\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":100,"wires":[["d96d39ad.ed5278"]]},{"id":"d96d39ad.ed5278","type":"schedex","z":"7342fdca.164ea4","name":"alarm radio","suspended":false,"lat":"","lon":"","ontime":"07:00","ontopic":"","onpayload":"on","onoffset":0,"onrandomoffset":0,"offtime":"07:01","offtopic":"","offpayload":"","offoffset":"","offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":false,"sun":false,"x":590,"y":100,"wires":[["17905c3e.7d4484"]]},{"id":"cfa79f02.db9d4","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

Hi @jocnnor,
How do I set the offset to be 1 hour before the event ?
Is it in the google_calendars.yaml ?
Should I add offset: 01:00 ?
Also, the trigger to the automation should be the change of the state of the calendar entity from off to on ?

Oh, I had it wrong.

You have to put it in the title of the events you want to offset.

By default, it will look for the following string:
offset: “!!”

So, if some particular event you want a notification 15 minutes early, just title the event:

Event title. Random Stuff. !!-15

This should be in the format of HH:MM or MM.

New Event !!-01:15 TEST

This will now turn on the event trigger 1h15 min early.

offset_reached : If set in the event title and parsed out will be on / off once the offset in the title in minutes is reached. So the title Very important meeting #Important !!-10 would trigger this attribute to be on 10 minutes before the event starts.

So any event you create, if you want to add an offset, add it to the title and you’re good.

Now you’ll have to trigger on this attribute:

# Handle all events with an offset
- alias: Google Calendar offset trigger
  trigger: 
    platform: state
    entity_id: sensor.google_calendar
  condition:
    platform: template
    value_template: "{{ is_state_attr('sensor.google_calendar', 'offset_reached', 'on') }}"
  action:
    # Insert yo
2 Likes

care to help a pleb? it beats me completely, even if i read all here.

I know this is probably a little late now but I managed to use the time node to trigger an event for Formula 1 in advance using the google calendar integration.
But I spent a fair bit of time searching and this thread kept coming up so I thought I would add the info for anyone else looking
I havent included the full flow but hopefully the bits i have help someone like me that was searching

[{"id":"b741c218.6ab328","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"9e160e7d.94183","type":"api-call-service","z":"b741c218.6ab328","name":"Notification","server":"ff18536e.15029","version":3,"debugenabled":false,"service_domain":"persistent_notification","service":"create","entityId":"","data":"","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":710,"y":160,"wires":[[]]},{"id":"7145e222.46748c","type":"api-render-template","z":"b741c218.6ab328","name":"Anouncement","server":"ff18536e.15029","version":0,"template":"\n\"The F1 Qualifying is starting\"\n","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":720,"y":260,"wires":[["b3a42fdd.a5a338"]]},{"id":"137ae069.bbde","type":"api-render-template","z":"b741c218.6ab328","name":"Anouncement","server":"ff18536e.15029","version":0,"template":"\n\"The F 1 is about to start\"\n","resultsLocation":"payload","resultsLocationType":"msg","templateLocation":"template","templateLocationType":"msg","x":720,"y":320,"wires":[["b3a42fdd.a5a338"]]},{"id":"b3a42fdd.a5a338","type":"link out","z":"b741c218.6ab328","name":"Alexa Announcement","links":["653a3d19.62c894","d862c98c.ee29d8"],"x":1020,"y":280,"wires":[],"l":true},{"id":"d5e998e7.7b043","type":"switch","z":"b741c218.6ab328","name":"","property":"payload.data.message","propertyType":"msg","rules":[{"t":"cont","v":"Qualifying","vt":"str"},{"t":"cont","v":"Grand Prix","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":490,"y":320,"wires":[["7145e222.46748c","9e160e7d.94183"],["137ae069.bbde","9e160e7d.94183"]]},{"id":"d06732da.e3093","type":"template","z":"b741c218.6ab328","name":"Template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n\"data\": \n    {\n    \"message\": \"{{data.attributes.message}}\"\n    }\n}    ","output":"json","x":360,"y":380,"wires":[["d5e998e7.7b043"]]},{"id":"26f18713.b4246","type":"ha-time","z":"b741c218.6ab328","name":"F1 ","server":"ff18536e.15029","version":0,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityId":"calendar.f1_calendar_2019","property":"attributes.start_time","offset":"-90","offsetType":"num","offsetUnits":"minutes","randomOffset":false,"repeatDaily":false,"payload":"","payloadType":"jsonata","debugenabled":true,"x":210,"y":380,"wires":[["d06732da.e3093"]]},{"id":"ff18536e.15029","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Not sure if this was resolved, but this is how I did it:

Problem:
Calendar auto-updated via roster mobile app, cannot change change title to use offset. Event created in calendar ‘on-time’ and I needed my automations to trigger 60 & 30 mins prior (switch on a few devices etc)
Solution:
Calendar event has a start_time attribute that I used to convert the timestamp into a sensor which tracks the remaining minutes. Then used this sensor to trigger automations at 60 & 30 mins before my start time using the Numeric State triggers from Lovelace. Below is the sensor I used:

  - platform: template
    sensors:
      reporting_time:
        friendly_name: "NAME OF SENSOR"
        unit_of_measurement: 'm'
        value_template: "{{((state_attr('calendar.NAME_OF_CALENDAR_ENTITY', 'start_time') |as_timestamp|int - now()|as_timestamp|int)/60)|int}}"

Hope this helps!!

3 Likes

Found this thread after I’ve found a solution for my approach but nevertheless:

  alias: 'Abfallkalender Erinnerung: Schadstoffmobil'
  trigger:
  - platform: template
    value_template: '{{ (as_datetime((now().timestamp()) | timestamp_custom("%Y-%m-%d%H:%M:00"))) == (as_datetime(state_attr("calendar.YOUR_CALENDAR", "start_time" | timestamp_custom("%Y-%m-%d %H:%M"))) - timedelta( days = 0, hours = 8, minutes = 00 )) }}'
  condition: []
  action:

I went this way because I wanted to easily tweak the offset and the timedelta comes quite handy to do so.

Cheers!!!

This seems to be a bit overcomplicated?

{{ now().replace(second=0)|as_timestamp|int -(state_attr('calendar.andy_calendar','start_time')|as_datetime - timedelta(hours=8))|as_timestamp|int }}

I’m not sure what the need is for the custom timestamps?

1 Like

Going with the custom timestamps was just my way to get rid of the seconds and to make both sides comparable.
The complexity is surely given. I went this way because I’d rather have an easy way to tweak the timedelta.

Thanks - exactly what I was looking for.
A note - I’ve was going mad as the calendar was not updating but I just read that Google Calendar updates only at some 10 or 15 minutes frequency. Just had the patience to wait… and it did work.

Hi,
A bit long ago but you mentionned using kalender-events in nodered. I’ve got it, configure and I see data triggered but I lack understanding on how to create a simple alarm 10 mins before any even starts. I’m even unclear what’s the difference between the sensor, trigger and upcomming as the doc is pretty similar. Any lights?