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

It was i did (see my first post) but that implies to repeat my “complicated” condition which is much more complicated than in my example so my question here about another solution. But I understand it is not possible the way I imagined due to the template limitations.

Thanks for the others suggestions.

That’s correct. Change your script and you’ll be fine.

Set your ‘complicated conditions’ (we are obviously too stupid to understand) in a binary sensor and then use that sensor in all of your ‘complicated templates’.
As everyone else has said you can only set one value per template.
If you have multiple things to set you are better triggering a script to set such.
It looks like you are trying to control heating which is ‘sooooooo’ complicated that no one here has ever tried to do before.
I think you are obsessing about one particular avenue of approach and thus it’s probably not the best way to do it.

Edit: Sorry was that “tooooo” sarcastic ?

we are obviously too stupid to understand

I never though that !!!
For me, the detail of my condition was not relevant for my question except the fact it was complicated long so I wanted to factorize it. I wanted to keep my question as simple as possible.

It looks like you are trying to control heating which is ‘sooooooo’ complicated that no one here has ever tried to do before.

Again, I don’t think that !

My need is not “soooo” complicated but for a HA beginner it was not so simple. For each room, I want to set the temperature according the interval of time (such as 09:00 < t < 10:00 or 18:00 < t < 20:00), which intervals can vary according some conditions (my wife working or not this day, week day or we, children at home or not, etc…), and also taking in account the duration needed to get the required temperature, duration which vary with the current temperature, in order to anticipate heating (my electric heaters take a long time to heat, such as 2°C per hour max). Since intervals of time (and the associated temp) and current temperature can dynamically change, I have to recalculate each minute the need to anticipate the heating to get the required temperature at the beginning for the next interval of time.

I set it to work, thank to the great help in this forum and to the HA documentation. Now I want to build a new solution as simple as possible in order to facilitate the maintenance. For that I try to factorize things as much as possible and to separate “domains” (conditions to set intervals of time in a different automation/script/… that calculation time to target the required temperature, for example). So my questions here in the forum. I don’t want the solution “out of the box”, just some help when I am blocked on the way. It is a good way to learn more about HA (and it is funny).

Sorry was that “tooooo” sarcastic ?

Yes, it was somewhat rude :sweat_smile: but no problem.

Thanks all for you help and sorry for any inconvenience.
(sorry for my poor english)

Your english is not poor, I think it very good.
you heating explanation is also good, we a;most have enough information to help you.
so you have a heating profile.
is it the same for each room (the times that is) ?
how do you want to set the times ? (ie do you want to tweak the times from the front end ?
What about the heating profile is that the same each day / workday/weekends or different every day ?
when you say IF my wife is home, do you have occupancy figured out ?
I have all the above.
So temp if doors/windows open, temp if out, temp if night, temp if dat time, temp if evening.
So explain what you are after and we’ll then point you in the right direction.

No question is an inconvenience, you just need to proveide the necessary information and context.

Thank you for the help.
As I said, I would prefer to build the solution myself (I am an ex-programmer, retired, and it is funny). I just need some help when I am blocked such as “it is not possible to do this way with template” or “the syntax to do that is …” or “better set an automation instead a script for your case”, etc…

How did you learn to code? Did you read docs/manuals?
If yes, here’s exactly the same - we’re not saying ‘it’s God’s will’ - we point you to the source of information and explain what’s wrong with your approach (as you’re learning, not us?).
Seriously, have a look at Jinja2 as that’s the HA tempting engine.
I do it every time and I believe that’s the way to go. The docs are generally well maintained here :wink:

p.s I just read your use case. Well, I’m glad I didn’t do anything like that when I lived in a flat with electric storage heaters!
Maybe you don’t need to over-complicate your system (unless you truly enjoy the process)? KISS they call it :wink:

Ahmad is right, reading the docs helps but let’s give you something to research.
Okay, so you need to set up a sensor that determines the heating segment you are in (doors open, out, night, day, evening) I specify that order as that increments for me, you can choose differently if you like.
So determine your factors and construct the sensor.
From that you can determine what heating you need according to time and other factors.
Then you need a heating set point value, so create an input number for that. (maybe for each room ?)
Then you’ll need a profile for each room (common profiles mean yo can save on entities and ease the automation)
Catagorise your profiles and create the entities.
When you sensor changes update the heating set point.
When the set point changes update the thermostat (I recommend you use the software generic_thermostat but you may have different plans

How did you learn to code? Did you read docs/manuals?

Of course but nobody read a whole Language Reference Book before to write its first line of code. It is difficult to read all documentation before to begin anything. I try to do that as often as possible but you are right, it is time for me to have a deeper look at Jinja2 doc.

Maybe you don’t need to over-complicate your system (unless you truly enjoy the process)? KISS they call it.

I enjoy it :grin:

they read something anyway to get the idea what is possible to write. then as they go, they learn new words and revisit that reference book to get necessary details. so it’s a long process, yes.
I just wanted to say that there is no such thing as “this stupid Jinja is blocking me” as they did not design it for that - more likely there is something you don’t know yet.
And that fine as soon as you want to learn.

Here is my time of day sensor and the script I use to determine which temperature is appropriate resulting from that.
Use, pull apart or throw away as you see fit.

sensor:
  - platform: template
    sensors:
      heat_set_temperature:
        entity_id: climate.house_heat, input_number.in_heat_temp_control_val
        friendly_name: Heating Set Temperature
        value_template: "{{ state_attr('climate.house_heat', 'temperature') }}"
        icon_template: "{{'mdi:emoticon-cool' if ((state_attr('climate.house_heat', 'current_temperature') | float) >= (states('input_number.in_heat_temp_control_val') | float)) else 'mdi:emoticon-sad-outline'}}"
      heat_day_segment:
        entity_id: sensor.time
        friendly_name: Heating Segment
        value_template: >
          {% set time = states('sensor.time') %}
          {% set slt1start = states('input_datetime.id_heat_day_on') [0:5] %}
          {% set slt1stop = states('input_datetime.id_heat_evening_on') [0:5] %}
          {% set dy = (slt1start <= time < slt1stop) if (slt1start < slt1stop) else (slt1start <= time or time < slt1stop) %}
          {% set slt1start = states('input_datetime.id_heat_evening_on') [0:5] %}
          {% set slt1stop = states('input_datetime.id_heat_night_on') [0:5] %}
          {% set evng = (slt1start <= time < slt1stop) if (slt1start < slt1stop) else (slt1start <= time or time < slt1stop) %}
          {% set slt1start = states('input_datetime.id_heat_night_on') [0:5] %}
          {% set slt1stop = states('input_datetime.id_heat_day_on') [0:5] %}
          {% set nght = (slt1start <= time < slt1stop) if (slt1start < slt1stop) else (slt1start <= time or time < slt1stop) %}
          {% if dy %}Day{% elif evng %}Evening{% else %}Night{% endif %}
        icon_template: >
          {% set seg = states('sensor.heat_day_segment') %}
          {% if seg == 'Day' %}mdi:shovel{% elif seg == 'Evening' %}mdi:glass-cocktail{% else %}mdi:sleep{% endif %}

## resets temperature following any pattern change
  sc_heat_reset_value:
    alias: Heating Reset Value
    sequence:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.in_heat_temp_control_val
        value: >
          {% if false and not is_state('binary_sensor.bs_door_open', 'on') %}
            {{ '6' | float }}
          {% elif not is_state('binary_sensor.bs_occupied', 'on') %}
            {{ states('input_number.in_heat_temp_away_val') | float }}
          {% elif is_state('sensor.heat_day_segment', 'Night') %}
            {{ states('input_number.in_heat_temp_night_val') | float }}
          {% elif is_state('sensor.heat_day_segment', 'Day') %}
            {{ states('input_number.in_heat_temp_day_val') | float }}
          {% else %}
            {{ states('input_number.in_heat_temp_evening_val') | float }}
          {% endif %}
#### end of template

I did a basic interpetation of jinja wrapped python in this post : -

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.