Please help with service template and variables

Hi everybody,

I am trying to write an automation (per room) that will either climate.set_temperature or input_number.set_value, depending on the state of a binary_sensor.

This code works under developer-tools/template:

service: >
  {% if states("binary_sensor.zwischenzimmer_fenster_contact") == "on" %}
  input_number.set_value
  {% elif states("binary_sensor.zwischenzimmer_fenster_contact") == "off" %}
  climate.set_temperature
  {% else %}
  {% endif %}
data: >
  {% if states("binary_sensor.zwischenzimmer_fenster_contact") == "on" %}
  entity_id: input_number.helper_zz_fo
  value: {{ states("input_number.zz_abends") | float }}
  {% elif states("binary_sensor.zwischenzimmer_fenster_contact") == "off" %}
  entity_id: climate.zwischenzimmer_heizung
  temperature: {{ states("input_number.zz_abends") | float }}
  {% else %}
  {% endif %}
  

Output:

service: >
  
  climate.set_temperature
  
data: >
  
  entity_id: climate.zwischenzimmer_heizung
  temperature: 19.5

Currently, my automation looks like this

    - alias: "Heizung ZZ Abends"
      trigger:
        - platform: time
          at: input_datetime.zz_abends
      variables:
        temperatur: input_number.zz_abends
        heizung: climate.zwischenzimmer_heizung
      condition:
        condition: not
        conditions:
          condition: state
          entity_id: binary_sensor.zwischenzimmer_fenster_contact
          state: "on"
      action:
        - service: climate.set_temperature
          data_template:
            entity_id: "{{ heizung }}"
            temperature: "{{ states(temperatur) }}"

I was hoping that I could get rid of the condition part, and instead do something like this, but the code below won’t work (I tried using it without variables as well, but not even that works)

    - alias: "Heizung ZZ Abends"
      trigger:
        - platform: time
          at: input_datetime.zz_abends
      variables:
        temperatur: input_number.zz_abends
        heizung: climate.zwischenzimmer_heizung
        fenster: binary_sensor.zwischenzimmer_fenster_contact
        helper: input_number.helper_zz_fo
      action:
        - service: >
            {% if states("{{fenster}}" == "on" %}
              input_number.set_value
            {% elif states("{{fenster}}" == "off" %}
              climate.set_temperature
            {% else %}
            {% endif %}
            data: >
              {% if states("{{fenster}}" == "on" %}
                entity_id: "{{helper}"
                value: "{{temperatur}}"
              {% elif states("{{fenster}}" == "off" %}
                entity_id: "{{heizung}"
                value: "{{temperatur}}"

Basically, I need this to check, whether or not a particular window is open. If yes, send the to-set temperature to an entity (another automation will set the climate entity to this value as soon as the window gets shut), if not, set the temperature directly.

Unfortunately, I must do this! The zigbee thermostats I use keep messing this up (so let’s say one is set to off and temperature changes, it is supposed to stay off, change temperature, then adapt to it once it gets turned to auto. However, this does not work. Even when turned off, some thermostats will start heating when set temperature changes.

So this is my attempt to work around this. I have multiple times per day (morning, noon, afternoon, night) at which the radiator temperatures change. This works, but the zigbee thermostats mess it up by heating even though they are set to off, so I need to work around it. Since I have one per room (times four times), it’d be great being able to use variables, as my current automations are already written with variables, and I’d only have to replace the action part of the automation via copy&paste. Otherwise (of I could not use variables), I’d have to hard-code each helper, window, and climate entity to each automation - which would still work, but be much more code, and kinda annoying to set up.

Thank you in advance for your suggestions :slight_smile:

You can do this:

{% set temperatur = states('input_number.zz_abends') %}

See here: Templating - Home Assistant
Under the header ATTRIBUTES, 2nd example.

And indeed, the Template-tester is your friend :slight_smile: