Value from Template in Services not Number

I have some issues in getting a number passed as a value to a Service call. For testing I created two input_number helpers called input_number.test and input_number.data. I then execute the following service:

service: input_number.set_value
data:
  entity_id: input_number.test
  value: {{ states("input_number.data") | float }}

I then get the below error:
Could not call the service input_number/set_value. expected float for dictionary value @ data['value']

I would have thought that | float would convert the value to float, but it seems to still be a string.
What am I doing wrong?

Running HA 2021.3.2

Single-line templates must be enclosed in quotes.

service: input_number.set_value
data:
  entity_id: input_number.test
  value: '{{ states("input_number.data") | float }}'
1 Like

Putting single quotes around it does not make a difference, still same result

Try this (sorry a few edits but should be there now!):

service: input_number.set_value
entity_id: input_number.test
data:
  value: '{{ states("input_number.data") | float }}'

With version 2021.3.2 the above is not accepted as a valid service call, the Call Service button is grayed out. If I changed it to the following:

service: input_number.set_value
target:
  entity_id: input_number.test
data:
  value: '{{ states("input_number.data") | float }}'

I can call the service, but I still get the floating error. It is as if the template does not get evaluated, and is just passed on as a text string.

Bizarre. What does {{ states("input_number.data") | float }} show under Developer Tools / Template?

It shows: Resulttype: number and then 3 (No decimal)
EDIT: I have set the the value to 3.0 for input_number.data

Are you trying to do this in the services tab on the ui? If so it doesn’t accept templates, never has.

Templates are for automations and scripts.

1 Like

Hi Marc,
YES I AM - Bummer! I will just create a script and call that. Thanks Guys

1 Like

You wouldn’t be the first person to make that mistake and probably won’t be the last.

Basically the services tab is there to do something ‘now’ so if you want to (for example) set the thermostat to 18, then you’re just going to type 18 in the box. Whereas automations and scripts are templated because they occur ‘when triggered’ rather than ‘now’ so you may want 18 or any other number depending on other factors at that time.

(I should add that I’m not saying I agree with how it’s currently done, just explaining how it’s come to be why it is how it is).

Thanks Marc,
I’ve been running HA for 3+ years, so I should know by now - but I jumped right in to it.
I think it makes sense just to have services like that, as it is easy just to make a script to achieve the templating.

Thanks again.

1 Like