I’ve struggled quite some time to figure out how to fulfil float requirement setting the temperature of a climate entity. I have script to do this but I get this error in the log file:
set_daikin_normal: Error executing script. Invalid data for call_service at pos 3: expected float for dictionary value @ data['temperature']
I understand the error message is raised because the variable setpoint_std is not evaluated as a float. In the notification message, the setpoint_std is evaluated as states.climate.gamla_huset.attributes.temperature|float(0), i.e a string - not the temperature value. I guess this is the problem. The variable holds a string instead of the value.
But how do I get this right?
My first attempt was to use the template {% if … %} in the temperature assigment, but I ended up with the same problem. Then I introduced the local script variable setpoint_std without success. So obviously there are some basic stuff I don’t understand.
Have you tried putting a float filter in the temperature tag?
temperature: "{{ setpoint_std | float(0) }}"
To make sure the subparts are correct the template editor in developer toos is the best way to see what the various things you use evaluate to (before you apply the float filter).
It could also be temperature does not support templating at all.
- variables:
setpoint_std: >-
{% set c = 'climate.gamla_huset' %}
{% set t = state_attr(c, 'temperature')|float(0) %}
{% if not is_state_attr(c, 'hvac_action', 'off') %}
{{ t + states('input_number.eco_sankning')|float(0) }}
{% else %}
{{ t }}
{% endif %}
In your version, the statements following if and else are strings (not templates performing a computation). Look at this screenshot of your version in the Template Editor. Instead of producing a numeric value it’s producing a string of the calculation itself because you overlooked to include braces to make it a template.
Your suggestion will always report the default value (0) because the template for the setpoint_std variable is incorrect (it always reports a string which float will convert to 0).
Finally I’ve discovered the error message (about float) in the log is actually false! The script works perfectly well. I can just ignore the message.
I discovered this by removing the variable section and added the templates to the set temperature part directly. Still the same error message. This confused a lot since I know this code worked before. I then replaced the templates with temperatures (20.0 and 18.0). The error message disappeared. However, the scripts works equally well as with templates. The only difference is I get an error message in the log using templates…
This is quite fishy to be honest. Looks like a bug somewhere.
Your if-else didn’t have templates, it had alphanumeric strings because you made a syntax error (you forgot to add double-braces).
{% if not is_state_attr('climate.gamla_huset', 'hvac_action', 'off') %}
(states.climate.gamla_huset.attributes.temperature|float(0) + states('input_number.eco_sankning')|float(0))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This entire line is not a template because it is missing opening and closing double-braces {{ ... }}
The screenshot I posted above clearly shows what you created failed to produce numeric values because you forgot to add braces.
You are right. I missed your point since I was a bit lost when you introduced the Jinja2 variables as well.
However, I was right as well. The script works perfectly well even with the syntax error! But you get an error in the log file. When I add the extra curled brackets, the error disappears. And the result is still OK…