[solved] Data_template with some {% ... %} before

I just read your code. I learnt many things about template. I certainly will use some parts for my new version. Thank you to share it.

PS. In the following line the condition will be ever false, no ? (a copy&paste bug or a magic behavior of template I don’t understand yet):
{% if false and not is_state('binary_sensor.bs_door_open', 'on') %}

This is what is called a placeholder.
I will need to address this in the future so I have a line for it, but until I actually get round to installing all these sensors, I need that part to ALWAYS evaluate to false.
I am quite lazy, haven’t gotten to install all the sensors and have bigger fish to fry at the moment :smiley:

An answer to my initial question:

event_data_template:
  {% ... %} ... {% ... %}   # code to elaborate value of p1, p2, p3
  {% set dic = {'p1':  'v1', 'p2': 'v2', 'p3': 'v3'} %}
  params: {{ dic | to_json }}

and to use

  {% set params = trigger.event.data.params | from_json %}
  {% set p1 = params.p1 %}
  {% set p2 = params.p2 %}
  {% set p2 = params.p2 %}

Anyway, the “off topic” discussion has been very valuable for me : after 3 or 4 weeks with the initial excitation to play with HA and devices, I decided it is time now to stop and read more deeper the doc. The first gift has been to find the json way myself. Thank you all.

1 Like

That looks clever (see my UPDATE2 below). I was born before xxx_json arrived so I rarely use it so didn’t even think about such a possibility :wink:

Do you know what? It’s an interesting solution but from your initial question I personally had an impression that you’re trying to pass data to a python_script (possibly because I overlooked the event bit and focused on the templates) and that subconsciously changed the way I looked at your problem…

First of all, everything coming from a template is a string (and it’d save you a lot of debugging).
Then, in python_scropt there is no such thing as | from_json and your solution won’t work because there is no easy way to parse strings back to JSON (as you cannot import anything and the set of functions available is very restricted to call it Python at all) so you have to deal with separate string input variables…

So my point is - ask better questions to get the answers you’re looking for (I didn’t say that, someone else did).
Again, I’m glad that you solved your issue and shared it with us and I believe we all gained from this discussion.
Let’s keep it going.

UPDATE: I played with your code and have to say that we could do without to_json (as it just creates a safe dict) as this code works

{% set data = '{"p1": "a b", "p2": "c", "p3": "d e f"}' %}
{% set params = data | from_json %}
{{ params.p1 }}

but as we still need to convert a string into something and from_json does that for us so we need to rely on it and it’s a bit tricky. Namely, from_json wants everything enclosed in double quotes and does not seem to accept keys/values in single quotes. It’s important because this code

{{ {"p1": "a b", "p2": "c", "p3": "d e f"} }}

does not preserve quotes and returns

{‘p1’: ‘a b’, ‘p2’: ‘c’, ‘p3’: ‘d e f’}

so we have to use from_json (but it’s not a big deal after all if we understand what’s going on, right?).

UPDATE2:
The initial question was

So basically it’s “How to pass variables to another automation”, right?
The crucial issue there was that complicated condition that we couldn’t evaluate in a separate template.
Therefore the key change was to use one template instead of separate ones.
But in this solution

I can see no v4, v5 and v6 there! :wink:
OP cheated a bit as he had to process his complicated condition and set v1, v2 and v3 based on it, right (he just didn’t show us that part of the answer)?
So the resulting code is complicated, too.

In one of my previous responses I proposed to change the strategy and just pass all necessary data to a recipient and do all processing there.
So it would look like

event_data_template:
  cond: "{{ complicated_condition }}"
  v1: {{ v1 }}
  ...
  v6: "{{ v6 }}"

and to use

{% set params = trigger.event.data.params %}
{% set p1 = params.v1 if params.cond else params.v2 %}
{% set p2 = params.v3 if params.cond else params.v4 %}
{% set p3 = params.v5 if params.cond else params.v6 %}

I kept it similar to the OP’s solution so it’s easier to compare.
All plain logic in one place. No extra filters. Same result.

Make your educated choice!

The example for the description of the json solution is different of my initial example. There are so examples. The only thing asked here is, as you said, how to pass parameters to another automation. Now I have a solution so I am happy.

All plain logic in one place.

It is not my way to code, I prefer separate things. In my case, there is an automation for each room. A room automation is aware of the heating needs of the room (temp, interval of time) according to conditions. Each room automation call a “shared” automation in charge to manage heating according to required temp for the current interval, the required temperature of the next interval of time (and hh:mm of this next interval), the performance of the heater and the metered current temperature.

Anyway, it is a interesting idea to pass a condition as a parameter and I will remember it for others situations.

good luck debugging your code :wink:

It’s up to you, but I saw it many times here.
Just imagine a guy who comes to you and asks for help. You spend your free time on finding a solution and then he says “It doesn’t work” and it turns out he didn’t tell you the whole story so basically you wasted your time and have to spend more on a) finding out what didn’t he tell you and b) solving a new problem.
Would you like it? Would you be keen on helping that particular person again?
And it can happen more than time during one single session - it’s hilarious and horrible in the same time.

you didn’t say that initially, did you? :wink:

Nevermind.

1 Like

You wrote almost all of your “solution” posts after I found a solution myself and after I marked the thread as “solved” so almost your wasted time is about a problem finally clearly exposed (“how to pass several parameters to another automation”) and with a solution found. In fact, almost this time has been to try to find a better solution than mine. Why not, I am grateful for that, but if I am really sorry for the time wasted, I don’t feel so guilty to have not follow your alternative solution. My mistake has been to have the courtesy to answer to your posts even if this subject was closed in my mind.

I hope we have clarified our mind each other and we can forget this misunderstanding. I will do my best the next time I will ask for help.

I didn’t want to make you guilty, it’s a free country.
I wanted to show what can be improved in a way you use this forum as at the end of the day it impacts the whole forum (not personally you but all of us).

I’ll leave out the statement about your solution and timing of my posts but I noted quotation marks.

wow. I think this time I wasted your time or even insulted somehow…

yeah, no problem.

Not at all ! (my english weakness drives me to express my mind wrongly sometime :disappointed_relieved:)

I wanted to express that I should have said “thank you for the help but I don’t need it anymore on this topic since I prefer experiment solutions by myself” in order you don’t waste more time(1). I am guilty for that but I though that was not very polite. It was a mistake. Sorry.

Have a nice day.

1. but I read all your posts and I will remember some ideas such as to pass a condition as a parameter.