Not able to build condition with {{ data_template }} in script

Hello Homies,
at first, thanks a lot for this incredible community and project.

I think my problem is simple af, but after 2 days trying and failing I give up and start my first thread. I read neraly every page with topic condition, ifelse but theres nothing about data template.

What I want to do:

A button starts a script

 - show_name: true
   show_icon: true
   type: button
      tap_action:
         action: call-service
              service: script.heizung_set_temperatur_template_wohn_ess
              data:
                state: 'ON'
              target: {}
   icon: mdi:radiator
   name: An (20°C)
   show_state: false
   hold_action:
   action: none

The submit of Data:STATE template to the script works correctly, checked on multiple levels.

The Script:

heizung_set_temperatur_template_wohn_ess:
  sequence:
    - service: climate.set_temperature
      data:
        entity_id: climate.essen_heizung
        temperature: >
            (% if '{{ state }}' == 'ON' %)
              {{states('variable.heizung_tmp_on') | float}}
            (% elif '{{ state }}' == 'OFF' %)
              {{states('variable.heizung_tmp_off') | float}}
            (% endif %)
  mode: single

Everthing but the if condition is working. Its not possible for me to check the state in the condition. I tried a lot, i only show this example because I think this is the best example to explain.

Hope anybody can help me.

Grüße

I don’t recognise that syntax at all. Where did you find (% ... %)? I think it should be:

        temperature: >
            {% if state == 'ON' %}
              {{ states('variable.heizung_tmp_on')|float(0) }}
            {% elif state == 'OFF' %}
              {{ states('variable.heizung_tmp_off')|float(0) }}
            {% endif %}

This will fail if state is not available. You could use {% else %} instead of your {% elif... %} (defaulting to the OFF temperature), or alternatively:

        temperature: "{{ states('variable.heizung_tmp_' ~ state|lower)|float(15) }}"

…with a sensible default temperature (15) for when the state isn’t available for whatever reason.

References:

’on’ and ’off’

Hi Troon,

thanks for ur reply. (% if … %) is used in many automation samples I found in the forum.

{{ state }} is my custom data template and available. {{ states(‘variable.heizung_tmp_off’)|float(0) }} is also working. When I set this in the (% else %) - part, the value of this var is set. Only the ‘comparing’ of {{ state }} is not working. Tried it with decimals, string. Nothing worked for me.

Please link to one.

@Troon @tom_l

Thanks, I tried your solution and it works. Its {…} not (…). Checked different links I visited. Absolutely tunnel vision after 2 days struggling. Grrrr, I knew it, simple af.

Thanks a lot!!

2 Likes