Hello and thank you for your answer.
Yes, I wish that brackets were in the string.
I’ve tried also with replacing special chars { " and ’ with its HEX equivalent, but also without success.
In template editor most of those tries are correct, but in automation always get an error.
Error while executing automation automation.zapisz_zuzycie_energii_dzienne: template value should be a string for dictionary value @ data['message']
23:38:10 – Automatyzacja (ERROR)
zapisz_zuzycie_energii_dzienne: Error executing script. Invalid data for call_service at pos 1: template value should be a string for dictionary value @ data['message']
23:38:10 – Automatyzacja (ERROR)
Edit:
I found a simmilar thread, where Petro gives a solution.
After editing I got:
message: >
{% set d = now().year %}
{% set v = states("sensor.daily_energy") %}
{"date":{{d}},"y":3,"txt":"energy":{{v}}}
Above works well, but when I delete the middle and change it to simple:
message: >
{% set d = now().year %}
{% set v = states("sensor.daily_energy") %}
{"date":{{d}},"energy":{{v}}}
but no the following (same as above, but without first and last char which is single quote)
{"date":"2021-04-09","energy":12.52}
Edit:
Thank you septillion for your suggestion, I’ve added second action to this automation and make persistent notification. The result is the same as above, persistent notification works with additional single quotes at the beginning and the end, but doesnt work without it.
- alias: zapisz_zuzycie_energii_dzienne
initial_state: 'on'
trigger:
- platform: time
at: '23:59:55'
action:
- service: notify.zapisz_energia_dzienna
data_template:
message: >-
{% set d = now().strftime("%Y-%m-%d") %}
{% set e = states("sensor.daily_energy") %}
'{'date':'{{d}}','energy':{{e}}}'
- service: persistent_notification.create
data:
message: >-
'{{ {'date': 2021, 'energy':
states('sensor.daily_energy')}|to_json }}'
title: "Zużycie energii"
I think the problem is that the Jinja2 template enging outputs a dict whenever it sees a valid json or a dict, quoted or not, so however you would create the output, Jinja2 converts it back to a dict which is evident if you test it in the template editor, and you need it to be a string to work with notify. As a last resort, you could try calling notify with a python_script instead:
That wouldn’t make a difference because typing is handled after the template is resolved and the to_string wouldn’t be seen. Also, the | string filter already exists.
Other Jinja2 implementations that I have seen has a .str function that actually outputs a string from any object, but that might not be possible to implement here.
that’s what | string does. This is what I was explaining to you. |string converts any output to a string. The problem is that value that gets passed from the template into a ‘typing resolver’ and that changes it to whatever is discovered.
Mm, weird. because when I checked it in the template dev tool it was a string. Like, when I tried an invalid operation on it the error was that the operation wasn’t valid on a str. So I assumed it was a string.
But I do notice since I think 2021.3 the UI yaml editor is a pain to use with Jinja