File output with curly braces

Yes that’s it.

In any case, this is only a way of learning what can be done, I’m not using in my configuration.

I made some progress. When I try this:

Screenshot from 2021-08-30 18-04-34

This is what gets written to file1.txt

{'temperature': 21.0, 'humidity': 13.6}

See what this does for you:

sequence:
  - service: notify.file1
    data:
      message: >
        {% set t = states('input_number.temperature') | float %}
        {% set h = states('input_number.humidity') | float %}
        {{ '{{{ "temperature": %f, "humidity": %f }}}' | format(t, h) }}
mode: single

The hurdle might be that some JSON interpreters insist upon the use of double, not single, quotes. So it will depend on whatever is consuming this data.

2 Likes

Yes, now the output is what you said.

What is the meaning of the three curly braces around the message?

Escapes the meaning of the brace for more than one level of processing.

If you use only two braces, the Jinja2 interpreter thinks they belong to it but will then complain because it finds two nested sets of double braces (which is invalid).

1 Like

Glad to hear it.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. It will also place a link under your first post that leads to the solution. All of this helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

1 Like

Of course. Done.

Thank you for your explanation.

1 Like

Clever :slight_smile:

I don’t understand why

{{ { "temperature": 23.780000, "humidity": 78.000000 } }}

renders as

{'temperature': 23.78, 'humidity': 78.0}

though. Why the double to single quote, and why the number formating?

And yeah, unfortunately, JSON mandates double-quotes for string, albeit some libraries might be lenient.

Because

renders a python dictionary, not JSON.

Ok for the number formatting, but the single quotes totally evade me…

That’s just what python does

python doesn’t care if a string starts with a ’ or ", it uses them interchangeably wherever it see’s fit.