How to use data templates?

I’m trying to call this service (I want to put it in an automation, but I also get the error when I use the service UI):

service: climate.set_temperature
data_template:
  entity_id:
    - climate.daikin_living_room
    - climate.daikin_bedroom
  hvac_mode: heat
  temperature: {{ states('sensor.heater_target') | float }}

I get the following error message:

Failed to call service climate.set_temperature. expected float for dictionary value @ data['temperature']. Got None

If I replace the template with this, for example:

  temperature: 20

Then it works. I also tried {{ 20 }} but I still get the error. What’s the problem? Why is it not accepting my template?

it can’t translate “sensor.heater_target” & you haven’t given the “float” value a default e.g. “float (default =0)”

Also I think the main reason of the error is that the single line template must be put between quotes: "{{ ... }}". Otherwise it is not evaluated, so instead of a number the whole line gets parsed as a string.

You can use data instead of data_template nowadays.

1 Like

yep that would do it

  • The data_template option was deprecated, many versions ago, in favor of simply data.
  • The entities targeted by the service call can be referenced by entity_id, device_id, and/or area_id when specified by the target option.
  • Templates on the same line as their YAML option must be quoted. Reference: Important Templating Rules
  • When using certain filters, like float, one should supply a default value. If you don’t and float is unable to convert a value, it will result in an error. In the following example, float will report 21 if the value of sensor.heater_target is non-numeric like unavailable or unknown.
service: climate.set_temperature
target:
  entity_id:
    - climate.daikin_living_room
    - climate.daikin_bedroom
data:
  hvac_mode: heat
  temperature: "{{ states('sensor.heater_target') | float(21) }}"
1 Like

Quoting solved the problem. I used data_template because data did not work, but apparently that wasn’t the problem.

1 Like

Changing data_template to data won’t fix this error:

expected float for dictionary value @ data[‘temperature’]. Got None

The error is due to the template lacking outer quotes.

If you just add quotes but don’t do the other things I suggested, you’ll have an old-style service call that will fail if the sensor’s value is ever non-numeric.