Template condition in automation not working

Hello,

I’m trying to set a template condition on a calendar triggered automation.
I want to perform actions only when my calendar event contains a specific word.

This is working :

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

But this is id only matching the exact WordToFind. I’d like to match a regex which will permit more flexibility.

So I tried

condition:
  - condition: template
    value_template: >-
      {{ trigger.calendar_event.summary is
      search('[Ww]ord[Tt]o[Ff]ind',ignorecase=False) }}

To be complete, in my WordToFind I’m using caracters like ‘é’ and ‘è’ but I don’t think this is the problem.

This is not working. My automation did not start anymore, and I’ve no trace.
trigger.calendar_event.summary is not available when I test this template with dev tool, so I tried with a random state value and I got it working.

I tried to put all in one row from the automation YAML editor :
value_template: "{{ trigger.calendar_event.summary is search('[Ww]ord[Tt]o[Ff]ind',ignorecase=False) }}"
But this is not persistant, when I save, I’m back with the multiline “>-”

May somebody point me what I missed please ?

if you go with search, you’d want ignorecase=True and just put everything in lowercase. As for

That 100% could cause your issues as it depends on the string encoding. You need to use the correct characters for what HA sees.

Thank you for your help. Very appreciate.
So I tried :

    value_template: "{{ trigger.calendar_event.summary is search('wordtofind',ignorecase=True) }}"`

which became :

    value_template: >-
      {{ trigger.calendar_event.summary is search('wordtofind',ignorecase=True)
      }}

as soon as I saved when I edit as YAML.
Same problem.

For now don’t mind for ‘é’ and ‘è’ but with the first example I gave it was working. So I’m pretty sure that HA is correctly configured to uses those. But, first things first…

Your template is correct in both instances. It’s 100% the word you’re trying to match.

The only other possibility is that you’re not using the correct trigger. Or you’re trying to test your condition in the UI (which you can’t do because it doesn’t have a trigger).

I’m not trying to test the condition in the UI as trigger.calendar_event.summary doesn’t exist if not launched by the calendar trigger.
So for testing purpose, I’m creating an event into the calendar I’m using.

Here is the full automation code :

alias: Test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.xxx
  - platform: calendar
    event: end
    offset: "0:0:0"
    entity_id: calendar.xxx
condition:
  - condition: template
    value_template: >-
      {{ trigger.calendar_event.summary is search('wordtofind',ignorecase=True)
      }}
action:
  - service: notify.mobile_app_MyMobile
    data:
      message: "{{ trigger.calendar_event.summary }}"
mode: parallel
max: 10

I have open an issue related to this subject. There is something going wrong with template conditions I think. And not just in UI evaluation as it is already known (I actually do not retrieve that issue).
But this is not what would explain why I didn’t got it working if my template is correct…
Could you give it a try ?

NB: It’s not the word I search the cause. If it was, I’ll have a trace of execution of my automation sayin that the condition where false, which infortunately I’ve not…I have no trace of execution (cf. related issue).

No there isn’t. I have literally hundreds of template conditions. I use search in many places. There are issues with a few functions currently, but that’s about it.

That means it’s not triggering.

It is triggering, when I edit the automation, while I have an event into my calendar, I see this :


But I have no trace of this execution.

I think this is a side effect of the condition that is not being interpretated correctly.

No, if there’s a trigger but the condition fails, you’ll have a trace. No trigger, no trace.

I think I’m becoming a little bit crazy with this thing right now.
I removed any special caracter as ‘é’ or ‘è’ from my calendar event summary
I’m now using the home assistant calendar integration (just in case)
I have deleted any previous existaing automation based on calendar event.

I started over with the value_template in one single line version. And I got it working.
This morning I replaced the value_template with the multiline version which is more appropriate regarding my purpose.
This is not working anymore. Automation do not get launched anymore.
I’ve rolled back my template_value… And this is still not working.
I didn’t get what is going on.

But I still see the blue notification on my trigger when I’m editing my automation saying “launched”. And I didn’t get any trace of this execution.

Did anybody can try this ?

alias: Calendar test
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.calendrier
  - platform: calendar
    event: end
    offset: "0:0:0"
    entity_id: calendar.calendrier
condition:
  - condition: template
    value_template: "{{ 'Wordtofind' in trigger.calendar_event.summary }}"
action:
  - if:
      - condition: template
        value_template: "{{ trigger.event == 'start' }}"
    then:
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.test
    else:
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.test
mode: parallel
max: 10

what does the trace say? Are you building this in the UI? If no, then add id: to it so you can see the trace. Lets try to stop playing these games, you’re focusing on the template but the template is fine.

If you really are convinced it’s your template, move each piece into variables so you can view each trigger in the automation trace variable section.

id: my_unique_id_for_my_calendar_automation_so_traces_work
...
variables:
  word_to_find: wordtofind
  searching: "{{ trigger.calendar_event.summary }}"
  continue: "{{ word_to_find in searching }}"
condition:
- condition: template
  value_template: "{{ continue }}"
...

Personally, I think you’ve spent so much time focusing in the wrong area that it’s something simple like the formatting in your automation or the calendar entity_id.

Thank you again.
I’ll check that.

I’m creating my automation from UI. And I sometime edit it in YAML.

For now, and with no specific action of mine, I got it working with the simple condition. I’m not specialy interested in broking it again …

I saw that somebody else is getting the same troubles as I am : Calendar trigger not working · Issue #85596 · home-assistant/core (github.com)
And that is exactly what I’m experiencing.