IF - ELSE clauses in template - and how to use when created?

OK, thanks.

Yes, I’m aware of the meaning of double brackets.

In my case code in configuration.yaml is this:

sensor:
  - platform: template
    sensors:
      temperature_ghall_hvac:
        friendly_name: "Garage_hall_HVAC"
        unit_of_measurement: '°C'
        value_template: >-
          {% set t = states('sensor.pana_halli_outside_temperature') | float(0) %}
          {% if 0.1 > t > -2.1 %} 18.5
          {% elif -2.2 > t > -4.1 %} 19
          {% elif -4.2 > t > -6.1 %} 20
          {% elif -6.2 > t > -8.1 %} 21
          {% elif -8.2 > t > -10.1 %} 22
          {% elif -10.2 > t > -12.1 %} 23
          {% elif -12.2 > t > -14.1 %} 24
          {% elif -14.2 > t > -16.1 %} 25
          {% elif -16.2 > t > -18.1 %} 26
          {% elif -18.2 > t > -20.1 %} 28
          {% elif -20.2 > t %} 29
          {% else %} 17
          {% endif %}

…and the corresponding script would look like this:

set_garage_hall_temp:
  alias: Set Garage hall temp
  sequence:
  - service: climate.set_temperature
    target:
      device_id: c6d59cfa6cf443c19a372b6f317e22e7
    data:
      temperature: {{ states('sensor.temperature_ghall_hvac') }}
  mode: single
  icon: mdi:air-filter

Firstly, creating this kind of script seems not to work if i try to use HA graphical UI for script editing, although in yaml -mode. I needed to add it in text editor directly to scripts.yaml, otherwise it wouldn’t accept it. Script editor also tends to autocorrect things (wrongly). (Is there any documentation of this script editor UI?)

However, when reloading scripts after change, I get this in my log:

invalid key: “OrderedDict([(“states(‘sensor.temperature_ghall_hvac’)”, None)])” in “/config/scripts.yaml”, line 8, column 0

you have to quote single line templates. Make sure to use doulbe quotes because your template contains single quotes.

I’m sorry, but You lost me know… What do I have to double quote?

You say that “…template contains single quotes” Template is the upper one, right? And it validates.
Script is the lower code sample, do You mean that?

Should this:

temperature: {{ states('sensor.temperature_ghall_hvac') }}

…be like this:

temperature: {{ states("sensor.temperature_ghall_hvac") }}

Please look at this post:

1 Like

OK, thanks, I take a look later. Duty calls… :slight_smile:

OK, thank You, it was very enlightening reading.

Everything is working now. Including control for the second HVAC I have there.

However, when creating more scripts, HA seems to autocorrect the syntaxes, if they are even touched with graphical script editor. According to the links You provided, they probably wouldn’t work as such. I did not test this, though. Anyway, from now on, if any new scripts needed, I’d probably better off creating them manually.

Is this a known issue?

You can use the UI, it will change how it looks but it should still work.

Even though e.g.

temperature: "{{ states('sensor.temperature_ghall_hvac') }}"

…is automatically changed to

temperature: '{{ states(''sensor.temperature_ghall_hvac'') }}'

?

yep, that will still work.

OK, I stand corrected :slightly_smiling_face:

Thank You all for great help!

Well well… It used to work just fine, but after latest 2021.12 updates I’m getting awful lot of these:

Error executing script. Unexpected error for call_service at pos 1: Invalid response, status code: 502 - Data: <html> <head><title>502 Bad Gateway</title></head> <body> <center><h1>502 Bad Gateway</h1></center> </body> </html>