Local Calendar Event Automation - Need help

Hi all,

I’ve followed the instruction to create automations based on the local calendar, but for some reason I can’t seem to get it to work and I don’t know why.

My goal is to be able to validate if there are certain holiday dates for which to then perform certain actions. To test the automation I have created a “all day” event in my local calendar called “TEST” but the automation doesn’t seem to catch it properly.

Any help is welcomed. Thanks!

Here’s the YAML code:

alias: Calendar Test
trigger:
  - platform: sun
    event: sunset
    offset: 0
  - platform: calendar
    event: start
    entity_id: calendar.global
  - platform: calendar
    event: end
    entity_id: calendar.global
condition:
  - condition: template
    value_template: "{{ trigger.calendar_event.summary == 'TEST' }}"
action:
  - if:
      - condition: template
        value_template: "{{ trigger.event == 'start' }}"
    then:
      - action: light.turn_on
        entity_id: light.library_lights
        data:
          kelvin: 6000
          brightness_pct: 100
    else:
      - action: light.turn_on
        entity_id: light.library_lights
        data:
          kelvin: 2500
          brightness_pct: 35
mode: parallel

The way a calendar works is that when an event starts, it’s state turns on. When it ends it turns off. An all day event it just stay on. So you could use it as a condition if you use an all day event.

Hi George, options to troubleshoot this:

  • check the condition/template in the developer tools
  • check the traces from that automation
  • test the action in a script

What seems strange to me: you have used the same entity_id twice for different triggers.
Also, why don’t you just use the entity_id in the IF condition instead of that template.

well I based my code off the sample code shared in the explanation on local calendar on Home Assistant website, found here:

So I checked the traces and it returned as below:

In developer tools I checked it in theory it should be working.

I’m new to programming in home assistant so could you tell me how I can test it in a script? Thanks!

It looks like you used the “Run” options from the expansion menu… That only runs the actions, it does not populate the trigger variable, so your If will always be false.

Ok. so if I remove the if condition from the action, it should work? or do you suggest something else?

The light will turn on, but you won’t actually be testing anything…

You should test it by triggering it with an actual calendar event.

Also, be aware that your sunset trigger will not populate trigger.calendar_event.summary, so you need to provide an option in the general condition to make that trigger work. One option would be to use a trigger ID as follows:

alias: Calendar Test
trigger:
  - platform: sun
    event: sunset
    offset: 0
    id: sunset
  - platform: calendar
    event: start
    entity_id: calendar.global
  - platform: calendar
    event: end
    entity_id: calendar.global
condition:
  - condition: template
    value_template: "{{ trigger.id == 'sunset' or trigger.calendar_event.summary == 'TEST' }}"
action:
  - if:
      - condition: template
        value_template: "{{ trigger.event == 'start' }}"
    then:
      - action: light.turn_on
        entity_id: light.library_lights
        data:
          kelvin: 6000
          brightness_pct: 100
    else:
      - action: light.turn_on
        entity_id: light.library_lights
        data:
          kelvin: 2500
          brightness_pct: 35
mode: parallel

Thanks for the help. It’s actually working now!