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…
- 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:
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
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”
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.
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.