If tomorrow a certain calendar event is planned, perform action

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.

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

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

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.

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.

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

That code is so old, it won’t work anymore

Thanks for your reply.
If this code does not work anymore, is there another solution that works?

There are calendar triggers where you can offset the time of the next calendar event. So you don’t need conditions like this.

Thanks. I have not seen this option because I was looking at the conditions.