#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:
- Do I have an event that starts at 08:00?
- 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.