Make a calendar event start an action at a certain hour and day

#Introductions
This sensors and automation, will automate an action based on a certain calendar, in this case “work” at a specific time and date.
Time and date can be changed via automation conditions, (Today, Tomorrow, Day_after_tomorow, In X days). Regarding the hours you can change the template sensor time_for_event
In this case I show an automation that takes place 1 hour and 15 minutes before the event happens.

Considerations

Let’s take in consideration the following situation:

  1. Do I have an event that starts at 08:00?
  2. if yes, turn on a light at 06:45.

First I’m using apple ical from tybritten, It works and this should work with Google calendar too…

4 sensors are created:
0,1,2,3 and 4. being sensor 0 the next appointment o the list. I will check that if it starts at 08:00

Example of output of sensor.ical_work_event_0:

summary: some summary
description: null
location: some location
start: '2021-02-10T08:00:00+00:00'
end: '2021-02-10T12:00:00+00:00'
eta: 1
all_day: false
friendly_name: some friendly name
icon: 'mdi:calendar'

the process

create a couple of template sensors:

## Sensors to use on TEMPLATES ##
    - platform: template
      sensors:


## Elements necessary to controlo de automation of a light based on a calendar event that happens at a specific hour of today, tomorow or when you what it, I this case I use iCal  ##
# Extraction of the hour from the calendar  iCal - Work #
        time_of_event:
            friendly_name: 'Time of event'
            value_template: "{{as_timestamp(strptime(state_attr('sensor.ical_work_event_0', 'start'), '%Y-%m-%d %H:%M:%S')) | timestamp_custom('%-H:%M') }}"
# Verification of how much time is for te event in minutes #
        time_for_event:
            friendly_name: 'Time for the event'
            value_template: >-
                                {% set actual_time = as_timestamp( now()) %}
                                {% set event = state_attr('sensor.ical_trabalho_event_0', 'start') | as_timestamp %}
                                {% set subtraction = ((event - actual_time) // 60) |int %}
                                {% if subtraction == 0 %}
                                Exact_time
                                {% elif subtraction == 75 %}
                                1h15m_to_event
                                {% else %}
                                Outside_event
                                {% endif %}
# Extraction of event day from calendar #
        next_event_day:
            friendly_name: 'Next event day'
            value_template: >-
                                {% set midnight = now().replace(hour=0, minute=0, second=0, microsecond=0).timestamp() %}
                                {% set event = state_attr('sensor.ical_work_event_0', 'start') | as_timestamp %}
                                {% set delta = ((event - midnight) // 86400) | int %}
                                {% if delta == 0 %}
                                Today
                                {% elif delta == 1 %}
                                Tomorow
                                {% elif delta == 2 %}
                                Day_after_tomorow
                                {% else %}
                                In {{ delta }} days
                                {% endif %}

After creating this sensors, I’ve created the next automation:

alias: 'Turn on light at 6:45 on the day of the event'
description: 'this will turn on the light on the day of the event based on iCal os some other calendar'
trigger:
  - platform: state
    to: 1h15m_to_event
    entity_id: time_for_event
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: next_event_day
        state: Today
      - condition: state
        entity_id: sensor.time_of_event
        state: '8:00'
action:
  - type: turn_on
    device_id: 7a327afbf9....b21dae2f4....
    entity_id: light.entrance_lamp
    domain: light
    brightness_pct: 20
mode: single

PS: This was was first written and tested in Portuguese, I’ve changed all variables and Remarks to English for better understanding for non the Portuguese spoken people.
I’ve started to use HA a couple of day ago, and this is my first contribution, I hope it works for you. I’m amazed about HA can do! My thanks to all developers.

If someone is willing to transform this it to a integration, it would be amazing.

This is my automation to notify me that I have to go work :slight_smile:

calender.werk entity:
attributes:

message: LHSW
all_day: false
start_time: 2021-02-12 09:30:00
end_time: 2021-02-12 17:30:00
location: Amsterdam Zuid Station
Amsterdam, Netherlands
description: null
offset_reached: false
friendly_name: iOS NS Werk

this part makes automation run 1 hour before start_time
{{ (states('sensor.time') == (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600 )|timestamp_custom('%H:%M')) }}

#   _____ _____ _____ _____    _ _ _ _____ _____ _____
#  |   __|     |_   _|     |  | | | |     | __  |  |  |
#  |  |  |  |  | | | |  |  |  | | | |  |  |    -|    -|
#  |_____|_____| |_| |_____|  |_____|_____|__|__|__|__|
###########################################################################################
# NOTIFY WHEN I HAVE TO GO HOME FROM WORK
###########################################################################################
- alias: "Notification - Goto Work"
  trigger:
    - platform: template
      value_template: >-
        {% if state_attr('calendar.werk', 'start_time') == none %}
          {{ false }}
        {% else %}
          {{ (states('sensor.time') == (as_timestamp(state_attr('calendar.werk', 'start_time')) - 3600 )|timestamp_custom('%H:%M')) }}
        {% endif %}

  action:
    - service: script.mobile_ha_engine
      data:
        title: "Goto work"
        message: "Within a hour you have to work"
        thread_id: "system_notification"

Much simpler and very nice !

Yours

2021-02-10T08:00:00+00:00'
2021-02-10T12:00:00+00:00'

Mine

2021-02-12 09:30:00
2021-02-12 17:30:00

Little difference, so you should maybe change it a little bit the code…
(i copied the code and worked for me)

You copied ical format with your code and worked? or do you mean, you tested all my “solutions” and worked for you?
What’s the source of your data? Google calendar?

My calendars are ical
I copied my code from the internet and it worked

What about that sensor.time on the template? can you show what that sensor does ?

sensor.time shows the current time. So you can use it to compare
(that is what i know)

That’s what I get with now()
ok. thanks for you information

Hello,

How do you calculate the time before the automation ? 3600 this number? where can i read to know this?

Thank you

3600 are seconds so 1 hour

1 Like