Trying Local Calendar and receiving error at Action (yaml)

Hello, I am following a tutorial from BeardedTinker: Local Calendar event automation in Home Assistant
https://youtu.be/phzvdnea13s

But when i add the Action like this:

service: notify.persistent_notification
data:
  message: "note: {{state.attr("calendar.youtube", "description") }}" is starting

Then i get this error message:

Message malformed: template value should be a string for dictionary value @ data['action'][0]['data']

I used he example BeardedTinker put in his youtube video.
Does anyone knows what is wrong here?

You need to use single quotes on the outside if you use double quotes on the inside…

service: notify.persistent_notification
data:
  message: 'note: {{state_attr("calendar.youtube", "description") }} is starting'

Hi @Didgeridrew Thank you for trying to help me!
I did try to change the double quotes for single quotes but not tried your suggestion. I will try this right now.

EDIT:
This is the syntax which gave no errors:

service: notify.persistent_notification
data:
  message: "note: {{state.attr(\"calendar.youtube\", \"description\") }} is starting"

The combination with single quotes and double quotes gave also an error…
Still have a lot to learn about yaml and templating …

I missed it on the first look, but the function should be state_attr() not state.attr()… I’ve fixed the template in my previous post.

Yes you are right, I had it discovered also. Errors are gone! Now i can try make a useful automation using local calendar. Thanks for helping me!

No idea why the video tutorial chooses to use the state_attr() function when the desired value is already available from the trigger variable.

service: notify.persistent_notification
data:
  message: "note: {{ trigger.calendar_event.description }} is starting"

Reference: Templating - Calendar

To add to @123 's post… If you use significant offsets in your triggers or your calendar has concurrent/overlapping events, it is imperative that you use the trigger variable in your template as demonstrated to get the correct data.

Ok thanks @123 for your input. I tried it like this.
Created a calendar event TestBert for today for times 11:30 - 11:45.
And this is the automation:

alias: Test notification uit calendar Bert
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:10:0"
    entity_id: calendar.bert
condition:
  - condition: state
    entity_id: calendar.bert
    attribute: message
    state: TestBert
action:
  - service: notify.persistent_notification
    data:
      message: >-
        note: {{ trigger.calendar_event.description }} Event TestBert is
        begonnen!
mode: single

But when i look in the trace i see this error:

Executed: December 19, 2022 at 11:14:37 AM
Error: Error rendering data template: UndefinedError: 'dict object' has no attribute 'calendar_event'

EDIT:
Testing from Developer Tools :: Services with this:

service: notify.persistent_notification
data:
  message: bla, bla, bla...

Works. But i can not add Templates in message: in this Services test field…
If I am correct, the field Description in the Calendar corresponds with the field state: in the Automation?

Because you have used a colon (:) in your message, this is one of the cases where you do need to be careful with which multi-line quotation indicator is used…

Try the following version without a -:

action:
  - service: notify.persistent_notification
    data:
      message: >
        note: {{ trigger.calendar_event.description }} Event TestBert is
        begonnen!
mode: single

It seems to work either way for me but YMMV.

Hi Drew (@Didgeridrew), I still keep having difficulties with the new local calendar.
When i create a calendar entry like thisL

And an automation for this like:

alias: Test notification uit calendar Bert
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "-0:0:0"
    entity_id: calendar.bert
condition:
  - condition: state
    entity_id: calendar.bert
    attribute: message
    state: TestBert
action:
  - service: notify.persistent_notification
    data:
      message: >-
        note {{ trigger.calendar_event.description }} Persistent notification TestBert
mode: single

So, I left out all special characters… But it does not give me a notification in the HA UI,
I did test the persistent notification with Developer Tools :: Services, and that works. Also notify.notify sends notifications to my iPhone.
I would love to have this working, so i can hopefully transform lots of node-RED automations with scheduler BigTimer to HA.

What do you mean by this? Is the automation not firing? Or, is it firing and the action is failing?

When i watch the trace, nothing seem to hapen: No traces found.

Ok, are you reloading the automation after you add/modify the calendar event? The calendar events are only re-read every 15 minutes or when the automation is reloaded…

It works for me. How did you test it?

Here’s how I tested it. I created a calendar event scheduled for 10:23 today:

I created an automation closely based on your automation (no offset, slightly different text):

alias: Test notification uit calendar Bert
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0"
    entity_id: calendar.test
condition:
  - condition: state
    entity_id: calendar.test
    attribute: message
    state: TestEvent
action:
  - service: notify.persistent_notification
    data:
      message: >-
        note: {{ trigger.calendar_event.description }} Event TestEvent is
        begonnen!
mode: single

At 10:23 I received the following notification:

1 Like

Yes, every time a made a change in the automation, or added a new calendar event, i reload Automations via Developer Tools :: YAML
I just created 4 entries in my calendar and reloaded Automations:
image

OK, Thanks @123 I will create the same calendar Test and the TestEvent Hello World and your automation, and see what’s happening. Choose a time about 16:50 - 17:00.
Removed all other calendar events.

Yes! that works:


Ok, now i have a working situation. Have to find out wat went wrong in the first place.
And, thank you both @Didgeridrew and @123 for helping me so far.

1 Like

I would recommend not using the calendar entity state, and instead looking at the contents of the trigger payload as the second example does in Calendar - Home Assistant

  condition:
    - condition: template
      value_template: "{{ 'Front Lights' in trigger.calendar_event.summary }}"

The reason is because the state example does not work if there are multiple calendar events firing at the same time or if there are overlaps and I worry about people copying these examples from the forums.

1 Like

@allenporter Good that you mention this!
However, my problem with your link to the documentation is that it is not clear that this was also for the new local calendar. Because the richt side shows:

The Calendar entity was introduced in Home Assistant 0.33,

BTW, I would like to thank you and other developers for the local calendar. Hopefully the developments continue, fi the possibility to edit a calendar entry…

Yep, local calendar is implemented using comm components that apply to all calendars including Google calendar, caldav, etc.

Edit is under review right now, and hopefully makes it into the next release.

@allenporter Thank you very much for the Edit possibility in the 2023.1 release of HA!
Enjoyed your contribution to the release party… :wink:

1 Like