Automation based on calendar event title

I’m trying to create an automation but hitting some brick walls. My aim is to have the automation run when a google calendar event contains ‘Red bin day’ for example, as the title.
I would then like to ‘post’ a message via MQTT that contains the calendar event title to my AWTRIX flashed clock.

Here’s what I’ve got so far :

alias: "Awtrix calendar "
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:30:0"
    entity_id: calendar.gmail
condition:
  - condition: state
    entity_id: calendar.gmail
    attribute: message
    state: Red bin day
action:
  - service: mqtt.publish
    data:
      topic: awtrix_7a7cf8/notify
      payload_template: |-
        {
        "text" : "Pull calendar title",
        "rainbow" : true,
        "duration" : 150,
        "icon" : "Tetrisdeletline"
          }
mode: single

You should probably change your condition to be based on the trigger variable instead of the calendar entity. The state of the calendar entity is not reliable if you ever have overlapping events in the calendar.

alias: "Awtrix calendar "
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:30:0"
    entity_id: calendar.gmail
condition:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary == 'Red bin day' }}
action:
  - service: mqtt.publish
    data:
      topic: awtrix_7a7cf8/notify
      payload_template: |-
        {"text" : {{ trigger.calendar_event.summary }}, "rainbow" : true, "duration" : 150, "icon" : "Tetrisdeletline"}
mode: single

If you plan on doing the same thing with multiple calendar titles use the in test with a list of titles in the template condition:

  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary in ['Red bin day', 'Title 2', 'Another Title' ] }}

Thank you:) I’m going to get chance today hopefully yo take a look at this. Fingers crossed!!

Having tried that the trigger happens but then the and if on the trigger calendar summary doesn’t appear to work.

I saw another one of your comments on another post saying to try the persistent notification to see if it’s passing correctly and it does. But in the automation it either fails at the and if or the then do element. I’m not sure which or if there’s a way of testing each element.
I pressed the 3 dots to test the and if but it says condition did not pass. Not sure if that’s because it’s based on a trigger that hasn’t;t technically been run thus no calendar event? Or something else?

UPDATE:
I tried looking at traces, not knowing too much about them but they showed the error “ Error: Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘calendar_event’”

i changed the then do text just to “123” and that passed through to my Awtrix clock fine. So it looks like it’s something to do with passing the {{ trigger.calendar_event.summary }} into the MQTT text?
END UPDATE

I’m fairly new to HA Automations at this level.

Do you still use the calendar state trigger? because you should not use that one if you want to use the event data.

You also mentioned you used the test button. You cannot use the test button for this, because then there will be no calendar event either.

Also note that if you put a new event in the calendar, it might take up to 15 minutes before the new event is seen by automations.

I used Digeridrew’s suggested code which all works other than passing the calendar event to my awtrix clock. As per my updated commment above. Showing the error.

What should I use instead of calendar trigger?

Turns out I just needed “ “ around {{ trigger.calendar_event.summary }}

To take this a step further I’m thinking about 2 things that hopefully one of your knowledgeable folk will be able to help with.

  1. Instead of == can i do contains?

  2. I have a blueprint that displays the weather, moon phase etc as an ‘app’ on the AWTRIX flashed clock which appears within the cycle of apps on the clock around 9:30pm for around 60 minutes.
    I would like this message to do similar. As opposed to just currently display for 150 seconds. I’d like it to rotate in / out of the display for 1-2 hours. As it’s not always guaranteed I’ll be near my clock. Hope that makes sense :slight_smile:

Yes, contains() is a valid filter:

{{ trigger.calendar_event.summary | contains("Red Bin Day") }}

For multiple terms, you can use search():

{{ trigger.calendar_event.summary is search("Red Bin Day|Blue Bin Day") }}

You would have to alter your automation config and maybe the Blueprint itself… you would need to share both of those for us to advise on that.

Thank you for this. I do code (web stuff so html/ xsl / css/ less etc) for my job but never yaml so didn’t think a simple contains would be the answer! What language code is this similar too?

The blueprint, I hadn’t realised has it’s own automation yaml code with it, it’s quite extensive so not sure the best way to share /even if you have time to explore (‘just let me know obviously absolutely fine but would rather not be hanging on haha). It is this blueprint.

As complex as that looks, the actual automation part of it is relatively straight-forward. All it should take to add your action is to use the “Take control” option from the 3-dot/expansion menu at the top right when editing the automation. This will convert the blueprint-linked automation into a stand-alone automation and you can add your MQTT publish action.

But, since you will not be relying on the Calendar trigger, you will need to decide how you want to store the payload data for the Weather, Moon automation to retrieve it. If the json string of payload will always be less than 255 characters you can store it in an Input text helper… just add an input_text.set_value action to your existing automation:

alias: "Awtrix calendar "
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:30:0"
    entity_id: calendar.gmail
condition:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary == 'Red bin day' }}
action:
  - variables:
      payload: |
        {{ {"text" : trigger.calendar_event.summary, "rainbow" : true, "duration" : 150, "icon" : "Tetrisdeletline"} | to_json }}
  - action: input_text.set_value
    target:
      entity_id: input_text.EXAMPLE
    data:
      value: "{{ payload }}"
  - action: mqtt.publish
    data:
      topic: awtrix_7a7cf8/notify
      payload: "{{ payload }}"        
mode: single

… if you need more space you would be better off using a trigger-based template sensor and storing it in an attribute.

1 Like

Here’s hoping you might get a ping. I’ve had it running for a while now and it’s great :slight_smile:
Update time … I’m thinking it’s be even better if it could collect all my calendar events for the day and display them. No matter the time it starts or the name etc. any idea if that’s possible. I couldn’t fathom it out over the past few days.

Can you expand on the idea, it’s not clear to me exactly how you’re visualizing it should work.

For example, should it still trigger 30 minutes before the start of the calendar events, but cycle through the days remaining events instead of just showing the upcoming one? That’s seems do-able. I don’t have an Awtrix, so I’m not 100% versed in how it handles the flow of what it displays, but I’m sure we could figure it out… it just might take some experimenting.

Apologies i missed your reply on this…

so i think in terms of Awtrix so long as i can gather the data i can display near enough what ever I want. Below is a copy of my current yaml for the automation. The triggers being;

  • every 20 minutes
  • 15 mins before a calendar event albeit full day events are taking over
  • HA restarts

essentially at the start of everyday i would like HA to ‘collect’ the days calendar event summary’s into a single variable which then Awtrix via the automation (every 20 minutes) would display them.

alias: Send Calendar to Awtrix. TEST
description: CALENDAR
trigger:
  - platform: time_pattern
    minutes: /20
  - platform: calendar
    event: start
    offset: "0:15:0"
    entity_id: calendar.gmail_com
  - platform: homeassistant
    event: start
    enabled: true
  - platform: event
    event_type: automation_reloaded
    enabled: true
condition:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary != '' }}
action:
  - choose: []
    default: null
  - alias: Variable Entity
    variables: {}
  - variables:
      alias: Variable Entity
      variables: {}
      device_ids:
        - 5de967b419adb63dece3ab07f4b0fca8
      user_data: {}
      value_template: "{{ user_data.get('value_template', states(entity_id)) }}"
      icon_template: "{{ user_data.get('icon_template', app_icon) }}"
      prefix_text: >-
        {{states.calendar.gmail_com.attributes.message}}.
        {{states.calendamail_com.attributes.start_time}}
      user_options:
        duration: 30
      payload: |-
        {
          {{('"icon"' if icon_template) ~ (":" if icon_template) ~ ('"' ~ icon_template ~ '"' if icon_template) }}
          {{"," if icon_template}} "text":"{{prefix_text}}"
        }
  - repeat:
      for_each: "{{ device_ids }}"
      sequence:
        - variables:
            awtrix_device_name: >-
              {{ iif( device_attr(repeat.item, 'name_by_user') != none,
              device_attr(repeat.item, 'name_by_user'), device_attr(repeat.item,
              'name') ) }}
        - if: []
          then:
            - data:
                name: "{{app_name}}"
                data: "{{ dict(payload, **user_options) }}"
              action: awtrix.{{awtrix_device_name}}_push_app_data
          else:
            - data:
                name: "{{app_name}}"
                data: {}
              action: awtrix.{{awtrix_device_name}}_push_app_data
mode: single
variables:
  entity_id: calendar_event.summary
  app_name: awtrix_app
  app_icon: calendarsmall
  additional_conditions: []

Hi thanks for this.

I’m still having problem automating mine. Would you be able to help? Below is my code with the calendar entity. Today is a Day Shift and below automation is for Night Shift

Looking the trace, it looks like the trigger and condition are skipped so the automation is passed, which is incorrect.


triggers:
  - event: start
    offset: "-0:0:0"
    entity_id: calendar.christina
    trigger: calendar
conditions:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary == 'Night Shift' }}
actions:
  - data: {}
    action: input_boolean.turn_on
    target:
      entity_id: input_boolean.christina_night_shift
  - delay:
      hours: 16
      minutes: 0
      seconds: 0
      milliseconds: 0
  - action: input_boolean.turn_off
    target:
      entity_id:
        - input_boolean.christina_night_shift
    data: {}
mode: queued
max: 10

The trace says it was triggered Manually, which just runs the Actions; bypassing the trigger and conditions.

However, a 16 hour delay is not a good idea, set a second trigger with an offset:

triggers:
  - id: "on"
    event: start
    offset: "-0:0:0"
    entity_id: calendar.christina
    trigger: calendar
  - id: "off"
    event: start
    offset: "16:0:0"
    entity_id: calendar.christina
    trigger: calendar
conditions:
  - condition: template
    value_template: |
      {{ trigger.calendar_event.summary is search('Night Shift') }}
actions:
  - data: {}
    action: input_boolean.turn_{{ trigger.id }}
    target:
      entity_id: input_boolean.christina_night_shift
mode: parallel

So I ran manually to check if the automation works or not, since when I test only the condition in the automation page, it never gives me pass/fail result.

However is the syntax correct? Reason I ask is because it didn’t trigger my “Day Shift” automation that has similar syntax with this one, and the condition is “Day Shift” instead of “Night Shift”.

Previously I used a separate calendar entity for Day and Night shift that just turns “on” and “off”. I want to simplify my calendar and to use the calendar title to run the automation, however it hasn’t worked well for me.

For your reference below is my calendar


Yes, the syntax is fine. The condition can’t accurately be tested within the Automation editor because it relies on the trigger variable, which isn’t populated until the actual trigger fires.

One thing to keep in mind is that == is literal… so your titles like Night Shift (Day 1/2) would cause the condition to fail. If you will be using a variety of titles, you need to use either a more specific regex find function or the search() function like:

{{ trigger.calendar_event.summary is search('Night Shift') }}
1 Like

Thanks mate. I’ll see if the automation will trigger tomorrow and report. Previously I used my email address as the calendar entity, not Christina calendar entity.

And thanks for the regex function. Will update them accordingly.

Hey man, just letting you know the code works perfectly. Many thanks again!

1 Like

Hi again mate, upon testing, the code works fine for a one day event. If the event is longer than 1 day, the boolean is turned off by the offset. for this case it turns off after 16 hours.

Do I just have to change the Event to Event end and put 3 hh Before from the visual editor?