Sensor attribute string: Can't convert to number for graphing

Hello all,

For my first template attempt, I want to access the attribute of a sensor, so I can graph the flow rate setpoint of my Vaillant heat pump.

I have the Home Assistant integration working fine and I can see the value I want in the ‘states’ view:
Sensor: [sensor.my_home_circuit_0_state]
Attribute: [heating_circuit_flow_setpoint: 27.241627]

Here is my template code:

template:
  - sensor:
      - name: "my_home_circuit_0_temperature"
        state:  {{ state_attr('sensor.my_home_circuit_0_state', 'heating_circuit_flow_setpoint') | default(0) | float }}

The additional template settings are here:

No matter what I try, I always get back to the same error:

Sensor None has device class 'temperature', state class 'measurement' unit '°C' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'template: - sensor: - name: "my_home_circuit_0_temperature" state: 27.241627' (<class 'str'>)

Is there a step I’m missing here?
Thanks!

Hello and welcome to the forum. Please edit your post and format your pasted config correctly, see: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Also please post your full sensor config. There is quite a bit of information provided by that error that is not shown in your sensor config.

Now, to your problem. Go to Developer Tools → States and find your sensor. What appears in the State column?

The error is saying this: state: 27.241627’

Notice the quote at the end?

If that is shown in developer tools you need to get rid of it for the state to be considered a number. You can do this:

state: "{{ state_attr('sensor.my_home_circuit_0_state', 'heating_circuit_flow_setpoint')|replace('’', '') }}"

It may be that type of “fancy” quote is there becaues of your lack of formatting. In which case the template should be:

state: "{{ state_attr('sensor.my_home_circuit_0_state', 'heating_circuit_flow_setpoint')|replace(''', '') }}"

Hi Tom,

Thank you so much! I had read that error 20 times, and completely missed the quote! I have corrected my formatting, you are correct that the fancy quote was an artifact.

I tried your replace option, but found that the editor flagged an error, I think it did not like the triple quotes:

I tried snipping the string as another option to get rid of the quote:

state: "{{ state_attr('sensor.my_home_circuit_0_state', 'heating_circuit_flow_setpoint')[:-1] }}"

This fixed the string and got the sensor working for me

Thanks
Andy

Note that if you use the GUI to create a sensor helper, you should only provide the template (without surrounding quotes for a one line template) for the State template* field.
Don’t put YAML code in there, the YAML code is only used when adding a sensor in configuration.yaml (either directly or through includes/packages)

1 Like

Which would also fix the triple single quote issue if you then use double quotes in the template. The triple single quote is probably being interpreted as an escaped quote as you escape single quotes with another single quote in yaml.

{{ state_attr("sensor.my_home_circuit_0_state", "heating_circuit_flow_setpoint")|replace("'", "") }}