Filtering non letter characters with template

Hello

I am trying to make a very simple automation to add stuff to my shopping list. The automation uses the matrix integration, and the idea is that i write a message into a special room, and items should be added to my shopping list. I have gotten the adding part working, but all my entries still have " ’ " characters encapsulating them. All other non letters seems to filter out fine, and it looks right in the developer tools. I am not sure what I am doing wrong at this point. Config below

 - id: '1573410787444'
  alias: 'Add items to shopping list'
  trigger:
    platform: event
    event_type: matrix_command
    event_data:
      command: add
  action:
  - service: shopping_list.add_item
    data_template:
      name: "{{ trigger.event.data.args | regex_replace(find="[^\w]", replace="") }}"
  - service: notify.matrix_notify
    data_template:
       message: "Added {{trigger.event.data.args | regex_replace(find='[^\w]', replace='') }} to the shopping list"

It is the name and message part of the data template that is problematic for me. Please keep in mind that i would like that umlaut characters work, since my language has those. Any help would be immensly helpfull

Thank you

1 Like

At the very least I think you’re not entering the strings correctly. Try this:

- id: '1573410787444'
  alias: 'Add items to shopping list'
  trigger:
    platform: event
    event_type: matrix_command
    event_data:
      command: add
  action:
  - service: shopping_list.add_item
    data_template:
      name: "{{ trigger.event.data.args | regex_replace(find='[^\\w]', replace='') }}"
  - service: notify.matrix_notify
    data_template:
       message: "Added {{trigger.event.data.args | regex_replace(find='[^\\w]', replace='') }} to the shopping list"
1 Like

Thanks! That seems to have solved it. So, I take it as that using the double quote to encapsulate the “find” and “replace” strings was incorrect use, and that i also did not properly escape the backslash in the expression?

Or was it something more subtle I messed up?

Anyway, thanks again!

You can use double-quote for the find and replace strings, but if you’re using double-quotes inside a string quoted with double-quotes, you need to escape them:

"f(abc=\"123\")"

It’s just easier to use single-quote inside a double-quoted string (like you did in message template):

"f(abc='123')"

And, yes, the backslash character is the escape character, so if you want a backslash you need to escape it:

"\\"

I think I understand now. That will probably save me a lot of frustration going forward.

Thank you for taking your time to help me. It is very much appreciated!
Happy holidays

1 Like