Can't get my head round Templates

Hi
My first post here, so please go easy.
I’ve set up hassio HA on RPI B+ 3, 15 Tasmotized Sonoffs on MQTT (Mosquito running on NAS). It’s all working fine. I’ve got few Automations, they work fine. I’ve executed few scripts from Mac Chrome, and Tasker (REST, HTTP Post). All ok so far.

The reason I’m posting here is: I can’t get my head around Templates. I’m a competent programmer. Programmed in many languages, just concentrating on Java/Kotlin at the moment. May be I’m looking at Templates from a different perspective. Eg the following code…

  • platform: template
    switches:
    lrtv:
    value_template: “{{ is_state(‘switch.LRTV’, ‘on’) }}”
    turn_on:
    service: switch.turn_on
    data:
    entity_id: switch.LRTV
    turn_off:
    service: switch.turn_off
    data:
    entity_id: switch.LRTV

In normal programming, value returned by value_template(“ON” or “OFF”) would be stored in a variable, and based on it, turn_on or turn_off would be executed. Here, it looks as if value_template is an orphan statement. Where does its value go?

Maybe a simple idiots’ guide could do the trick. I have been reading the docs for the past 3 days, as I say can’t get my head round to it.

Any and all help is greatly appreciated.

Is it Iam or Mr Gullible?
Your info says you joined 49 mins ago and have read 11mins
You don’t say where you got that bit of code and to be honest I can’t read it, as it’s not formatted correctly (formatting changes many things)
You are supposed (if you read the big sticky thread by tinkerer at the top of the threads, it also tells you lots of other important stuff)… You are supposed to insert code as preformatted (use the </> button to create such an area)
This code also looks weird, it has actions but is not set up with either trigger or action, so where does this live.?
The template bit given, equates to true or false dependent upon the on or offedness :wink: of your Living Room TV
Usually this is used in a condition or in jinja to branch an action
Have you found the template editor yet ?

Hi Mutt
Apologies, the code is unformatted. I haven’t put it in my configuration.yaml, it was just an example(Eg.) to extract info, as I stated in my post. The emphasis wasn’t on the code not working, but really to get some info behind the workings of Template, and how the Templates fit into the whole scenario. I hope you get the drift.

A value_template is going to return true or false and will perform an action if true generally… but it’s useless just posting a thought… much better to have a go and get help with errors.

There is a GREAT tool in dev-tools that you can enter in your template and see what it returns - that will be your best friend.

Hi David
Thanks
I HAVE been playing with templates in Dev-tools for a while. I normally do not resort to troubling nice people like you to answer silly questions ;), normally I get my hand dirty, and just dive in. Its just that I really did get stumped here with Templates. I’m getting there slowly, maybe I’ve been programming in normal lingos for far too long, needed a dose of Templating :slight_smile:

Think of the YAML part of the configuration as being a form with fields. You get to pick the fields based on what you want the form to accomplish. If it were a survey about customer satisfaction for a dishwasher it would require different fields than a survey for opinions about a political candidate.

Some of these fields can only accept information verbatim.
Do you like the candidate’s proposal for mitigating climate change? Yes or No.

Other fields allow you to create logic that can manipulate the supplied information (or generate it based on other information).
What is your overall impression of the dishwasher? {{ redact all four-letter words from supplied information }}

This logic is implemented using the Jinja2 template engine. So there are two separate ‘technologies’ in use here: YAML and Jinja2 (each having its own syntax rules).

The documentation contains an overview of how Home Assistant employs Jinja2.

I provided another member with a Jijna2 primer in the following post:

2 Likes

Hey Taras

Brilliant. Your post is just what I needed. Gives me the starting point to delve further in Templating.
Sterling job matie, kudos :slight_smile:

1 Like

I know this is already marked as solved but…

Just to re-iterate that you need to get into the habit of posting code in the correct format. It makes things very difficult (or impossible) to help if we can’t see the code in the way you have it entered into your config.

or just start and end the code with three back-ticks ( ``` ) on their own lines.

and to answer the specific question you asked, in the switch configuration you posted the “value_template” is the returned value of the switch itself. It can be either “true” (on) or “false” (off). That value doesn’t “go” anywhere but it’s just a representation of the switches state. The turn_on/turn_off services are the things that will happen when the switch toggle on the frontend is moved from on to off/off to on.

In the case of the switch when you turn the toggle from off to on, then the switch turn_on action is performed. Then the switch’s “value_template” is examined to see if it’s value is “true” or “false”. if it’s “true” then the switch state stays in the “on” state and the switch status toggle display stays in the “on” position. But if the switch value_template still shows that the value returns “false” then the switch state will go back to “off” and the switch display toggle will go back to the “off” position. Even tho the device that you physically acted upon is still physically on.

value_templates can also be used to set the state of a sensor.

Or they can be used to test for a trigger or a condition in automations. those value_templates results aren’t stored outside of the entity. Once the automation has completed then the results of those value_templates are gone. There’s nowhere that value is stored to provide any type of “RLO” (“result of logical operation” in Siemens PLC speak) table. You can’t look anywhere and see "yes, that condition value is “true” ".

Hi finity

Following on from Taras post, I had already worked out quite a bit about Templates, and have utilised two Templates in my config, and they are working fine, so perhaps your your post is a little superfluous and a little late, but thanks just the same :slight_smile:

Just to re-iterate that the code that I had posted in my post wasn’t entered in my config as you assert, I had made it clear to in my subsequent replies. I have read the forum rules, and I’m no rookie, having administered a few fora myself, so I know what is the pre-requisite for posting code here. I say this because I don’t need reminding ad-infinitum about the rules by any subsequent post :stuck_out_tongue_closed_eyes:

Ok, sorry that my mind reading machine is in the shop for repair this week. I didn’t see where the specific questions that you had asked in your first post were answered in this thread. So instead of assuming you already figured out the answers independently I figured i’d give them here. I’ll try to limit my “superfluous” posts in the future. :wink:

And whether the code was actually posted in your config or not is irrelevant in this case. I was just trying to be helpful in explaining why the “rule” is there in the first place. And because my aforementioned mind reading machine is on the fritz I failed to realize how much experience you had in forum management or that you actually knew the “rule” but just decided in this case to completely ignore it. :smiley:

And it ultimately did end up causing confusion with at least one person who, finding it difficult to read the improperly formatted code, mistook your code for a portion of an automation instead of the template switch that it was.

You’d be surprised at how many people need reminding almost to the point of “ad-infinitum” of the need to post their code correctly. That’s why I made that gif in the first place and why it has been used countless times by me and others. I didn’t think that another friendly nudge in that direction could do much harm.

:slightly_smiling_face:

hi finity

LOL we are touchy this morning, aren’t we?

I think I’ll say no more, as I have seen countless times, these kind of to and fro posts deteriorate into silly keyboard wars. ENDOF