Specifying a special character in a payload string

i would like to add a special character (0xdf) to a payload string instead of the “****”:

payload: '{"x":0,"y":3,"txt":"Wh: {{trigger.payload_json.t[:-1]}}**** {{trigger.payload_json.h[:-1]}}% {{trigger.payload_json.i[:-1]}}"}'

how do i escape this character?

It’s all about Jinja

i tried to look for it but didn’t find how to do it - anyone knows how to do this?

Have you tried something like

payload: '{"x":0,"y":3,"txt":"Wh: {{trigger.payload_json.t[:-1]}}chr(0xdf){{trigger.payload_json.h[:-1]}}% {{trigger.payload_json.i[:-1]}}"}'

?

just did - this was the output:

{"x":0,"y":3,"txt":"Wh: 29.0chr(0xdf) 35.0% 28.2"}

well, then substitute that **** with {{ chr(0xdf) }}

that was my 1st thought, but it makes the configuration invalid with the error:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘xdf’) for dictionary value @ data[‘action’][0][‘data_template’][‘payload’]. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 40). Please check the docs at Automation - Home Assistant

ok. why don’t you run that chr(0xdf) in python and then paste that special character into your template?

i’m not sure what you mean - i also tried this but got the same error

payload: >
  {% set ch = chr(0xdf) %}
  '{"x":0,"y":3,"txt":"Wh: {{trigger.payload_json.t[:-1]}}{{ch}} {{trigger.payload_json.h[:-1]}}% {{trigger.payload_json.i[:-1]}}"}'
ß

Try and copy paste that ^^

That’s because Jinja does not have chr function, it’s not Python.
I meant that you can get character with the code above by running Python interpreter and executing that chr(0xdf).
Just use the symbol nickrout provided where you need it.

since i couldn’t solve it, i’ve decided to use a workaround - sending a special (valid) character which won’t be used, and replacing it on the arduino with 0xdf char.

didn’t copy/paste of that special character work?
and how do you perform the replacement on arduino?

sorry - didn’t understand what needs to be done here - can you explain?. what i did was to send char (*) and scanning/replacing the string at the arduino side.

I meant to send something like this

payload: '{"x":0,"y":3,"txt":"Wh: {{trigger.payload_json.t[:-1]}}ß {{trigger.payload_json.h[:-1]}}% {{trigger.payload_json.i[:-1]}}"}'
1 Like

use {{ '\xdf' }}

payload: >
  {% set v = trigger.payload_json.t[:-1] + '\xdf ' + trigger.payload_json.h[:-1] + '% ' + trigger.payload_json.i[:-1] %}
  '{"x":0,"y":3,"txt":"Wh":"{{v}}"}'

2 Likes

That will only work if the encoding of the file (where the template resides) can handle that character.

Thanks! works fine :slight_smile:

1 Like

it would be great if you could mark a post with the solution as a solution :wink:

sorry - thx - done