button
September 22, 2018, 9:51pm
1
I want to run a script every evening (triggered by time-trigger ) if the next day a certain event is planned in my Google calendar .
My google-calendar.yaml:
- cal_id: [email protected]
entities:
- device_id: events
ignore_availability: true
name: events
track: true
search: "Keyword to search for event"
The entitity:
In my scripts.yaml file I have the following as a starting point (it is not finished yet):
checkevent_tomorrow:
alias: 'Check if specific event tomorrow'
sequence:
{% if (state_attr('calendar.events', 'start_time') == (as_timestamp(utcnow().day)+ (86400)) %}
- service: light.turn_on
data_template:
entity_id: light.bedroom
transition: 30
kelvin: 2000
brightness_pct: 100
{% endif %}
My questions:
would it work to use the if-function like that?
is the comparison of time ok? I am mixing something up because it doesn’t work.
petro
(Petro)
September 23, 2018, 3:27am
2
no you can’t use if like that, but you can use a condition. the script will only execute beyond the condition if the condition is met:
checkevent_tomorrow:
alias: Check specific event tomorrow
sequence:
- condition: template
value_template: "{{ (state_attr('calendar.events', 'start_time') == (as_timestamp(utcnow().day) + (86400)) }}"
- service: light.turn_on
data_template:
entity_id: light.bedroom
transition: 30
kelvin: 2000
brightness_pct: 100
button
September 23, 2018, 8:19am
3
you are pointing me in the right direction, thanks! Somehow I got:
Invalid config for [script]: [condition] is an invalid option for [script]. Check: script->script->check_event_tomorrow->sequence->0->condition. (See /config/configuration.yaml, line 94). Please check the docs at https://home-assistant.io/components/script/
Edit: It was a syntax error in my statement! Thanks, it is working now!
1 Like
Hi,
can you post the correct statement for the condition?
thanks in advance
petro
(Petro)
September 26, 2018, 5:37pm
5
That is the correct syntax, he must have been missing the sequence: line for his script.
didn’t work, same message:
Failed config
script:
- Invalid config for [script]: [condition] is an invalid option for [script]. Check: script->script->licht_biotonne->sequence->0->condition. (See /home/homeassistant/.homeassistant/configuration.yaml, line 516). Please check the docs at https://home-assistant.io/components/script/
licht_biotonne:
sequence:
- condition: template
value_template: "{{ (state_attr('calendar.biotonne', 'start_time') == (as_timestamp(utcnow().day) + (14400)) }}"
- service: light.turn_on
data_template:
entity_id: light.magic_schuhschrank, light.magic_telefonschrank, light.magic_spiegel
effect: 'green_fade'
i think the condition isn’t correct.
petro
(Petro)
September 26, 2018, 8:35pm
7
Ah, yep. Extra parenthesis.
- condition: template
value_template: "{{ state_attr('calendar.biotonne', 'start_time') == (as_timestamp(utcnow().day) + (14400)) }}"
perfekt, works like a charm.
Thank you verry much.
JZiZu
February 1, 2023, 9:25pm
9
Hi
I was trying out your code but it gave me an error. Can you help me?
In 'template' condition: ValueError: Template error: as_timestamp got invalid input '1' when rendering template '"{{ state_attr('calendar.my_calendar', 'start_time') == (as_timestamp(utcnow().day) + (14400)) }} "' but no default was specified
petro
(Petro)
February 1, 2023, 10:13pm
10
That code is so old, it won’t work anymore
JZiZu
February 2, 2023, 7:10am
11
Thanks for your reply.
If this code does not work anymore, is there another solution that works?
petro
(Petro)
February 2, 2023, 2:45pm
12
There are calendar triggers where you can offset the time of the next calendar event. So you don’t need conditions like this.
JZiZu
February 2, 2023, 3:53pm
13
Thanks. I have not seen this option because I was looking at the conditions.