Automation runs but doesn't write value to device

I’m trying to write my first automation to setback my thermostat at night. I used input variables for the manual setpoints. The thermostat is a z-wave by Trane. I have it working (zwave beta) and I can set the temperature in the HASS UI using Developer Tools | Services and a constant for the temp. When I try it in my automation, it doesn’t complain but it does nothing.

Here is my code:

- id: '1598973686122'
  alias: Setback Thermostat
  description: ''
  trigger:
  - at: 09:00:00
    platform: time
  condition: []
  action:
  - data_template:
      entity_id: climate.thermostat_mode
      temperature: "{% if is_state(\"climate.thermostat_mode\", \"cool\") %}\n  {{states(\"\
        \ input_number.thermostat_coolsp_night\")|float}}\n{% else %}\n  {{states(\"\
        \ input_number.thermostat_heatsp_night\")|float}}\n{% endif %}\n"
    entity_id: climate.thermostat_mode
    service: climate.set_temperature
  mode: restart

Note if I change the “temperature:” pair to “temperature: 18” it still does nothing.

Also, if you could help me with the template syntax. The editor put in all the quotes and escapes and I don’t know why I often see syntax in samples like “temperature: >” (specifically the ‘>’). And why do I need the entity_id twice (under action and also under data_template)?

In Developer Tools | Services, I use:

Service – climate.set_temperature
Entity – climate.thermostat_mode
Service data
1 entity_id: climate.thermostat_mode
2 temperature: 18

and it works

I assume the UI murdered that copy / paste.

Use double quotes outside your templates and use single quotes inside. Otherwise you get this:

temperature: "{% if is_state("

Also do not quote multi-line templates only single line templates.

      temperature: >
        {% if is_state('climate.thermostat_mode', 'cool') %}
          {{states('input_number.thermostat_coolsp_night')|float}}
        {% else %}
          {{states('input_number.thermostat_heatsp_night')|float}}
        {% endif %}

Tried so many things to get it to work. Here is the most basic:

- id: '1598973686122'
  alias: Setback Thermostat
  description: ''
  trigger:
  - at: 09:00:00
    platform: time
  condition: []
  action:
    - service: climate.set_temperature
      data:
        entity_id: climate.thermostat_mode
        temperature: 16
  mode: restart

Nothing happens when I trigger it. No change of temperature and no log in the log file. Nothing.

When I try to do it by Developer Tools | Services, it works.

Any great ideas?

Try with the entity id outside the data:

  action:
    - service: climate.set_temperature
      entity_id: climate.thermostat_mode
      data:
        temperature: 16

Also, is this the correct entity id climate.thermostat_mode ?

Yes, I’ve tried that to no avail.

Well, not sure. When I zwave included the thermostat, zwave (beta) made one device with two entities. The first entity is the room temperature and the second one is just about everything else (mode, setpoint, fan mode…) by using attributes, though the actual state itself is the mode (heat, cool, heat_cool or off). Bit odd but it displayed correctly.

So I’ve wondered about that. The only thing is that if I go to Developer Tools | Services and choose

Service -> climate.set_temperature
Entity -> climate.thermostat_mode
Service Data ->
  1 temperature : 25
  2 entity_id: climate.thermostat_mode

it works. Kinda implies it is the right entity_id.

Finally, I really don’t quite understand “climate”. It seems to be a collection of all climate device types (of which my thermostat is one). If that is the case, shouldn’t I be using the thermostat’s mode entity_id instead of the climate’s mode entity_id? Still, it works from Developer Tools so ???

Got it! My biggest problem was not reloading automations after editing the file. That caused a lot of odd behaviour.

My final code is:

  alias: Setback Thermostat
  description: ''
  trigger:
  - at: 09:00:00
    platform: time
  condition: []
  action:
    - service: climate.set_temperature
      data_template:
         entity_id: climate.living_room_thermostat_mode
         temperature: '{{ states("input_number.thermostat_coolsp_night") | float }}'
  mode: restart

Notes:

  1. I renamed the thermostat device (and entities) from “Thermostat” to “Living Room Thermostat” for future expansion and to see if the climate entity changed (it did).
  2. I didn’t need the entity_id in the service key as long as it was in the data_template key
  3. Pretty sure I don’t need the “float” filter as it coerces it into a float

Time to add more to the logic now that I have my first automation working!

Many thanks for your help, @tom_l. Together we got it working.

1 Like

I’ve seen that mentioned before. Why is that? Also, where is the “>” mentioned for multiline?

> is actually part of yaml not jinja:

https://yaml-multiline.info/