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.