Converting String to Float?

I’m not even sure that the problem is as simple as just converting a string to float to resolve the issue, but here’s the situation I’m in:

I’ve got a sensor that checks the difference between the min and the max temperature in a list of sensors.

      e4_temperature_differential:
        friendly_name: "ΔT"
        value_template: >
          {% set e4_temps = states("sensor.e4_2ndfl_temperature") | float, states("sensor.e4_mainfl_temperature") | float, states("sensor.e4_bsmt_temperature") | float %}
          {{ (e4_temps | max) - (e4_temps | min) }}
        unit_of_measurement: "°C"

and then there is a binary sensor that will determine whether a circulation fan is turn on or off if the difference is greater than 1.5 degC.

      circulation_fan:
        friendly_name: "Circulation Fan"
        value_template: >-
          {% if states.sensor.e4_temperature_differential.state >= 1.5 %}
            on
          {% else %}
            off
          {% endif %}

However, when i check in the developer’s tools, i see that binary_sensor.circulation_fan is “unavailable”. And in the Template section, i see that the error is:

TypeError: '>=' not supported between instances of 'str' and 'float'

I figured that means the calculation where i do the subtraction is outputting a string and not a float, so i modified that line to say:

{{ ((e4_temps | max) - (e4_temps | min)) | float }}

The error persists and i’m not sure what the real issue is anymore. Can anyone shed some light?

Probably one of the temperatures that was unavailable.

all the temperatures are there, even the evaluation of the temperature differential is there too.
It’s only the comparison of the temperature where it breaks …

image

Have you thought about the min max sensor.

sensors:
- platform: min_max
  name: "Max Temp"
  type: max
  round_digits: 1
  entity_ids:
    - sensor.e4_2ndfl_temperature
    - sensor.e4_mainfl_temperature
    - sensor.e4_bsmt_temperature

- platform: min_max
  name: "Min Temp"
  type: min
  round_digits: 1
  entity_ids:
    - sensor.e4_2ndfl_temperature
    - sensor.e4_mainfl_temperature
    - sensor.e4_bsmt_temperature

e4_temperature_differential:
        friendly_name: "Delta T"
        value_template:  {{ states('sensor.max_temp') | float - states('sensor.min_temp') | float }}
        unit_of_measurement: "°C"

It also makes adding additional sensors easier. You get this benefit as well:

Sensors with an unknown state will be ignored in the calculation. If the unit of measurement of the sensors differs, the min_max sensor will go to an error state where the value is UNKNOWN and unit of measurement is ERR.

i don’t see any " binary_sensor.circulation_fan " defined in above ( but maybe you cut that out of the context )

Have you tried the “double quote” around "

"states.sensor.e4_temperature_differential.state"

… or single

Change this:

To this:

        value_template: "{{ states('sensor.e4_temperature_differential')|float(0) >= 1.5 }}"

All states are strings. Including your newly created differential sensor. You must convert them to numbers before performing mathematical operations.

Also the binary sensor template should resolve to true/false, not on/off.

Also also:

2 Likes

Typo alert: flaot(0)

1 Like

Ta. Fixed.

1 Like

didn’t consider doing it like this … but doesn’t this mean i’d have to add sensors to both the min and max function?

thanks, i did try that and i seem to run into that problem a lot … but at least that didn’t resolve it this time around. also, yeah i did cut off the first part where the binary_sensor was located …

1 Like

thanks @tom_l that resolved the issue!

based on your explanation, my problem was converting from string to a float in the wrong spot? I tried to do it in the sensor thinking that it would stay as a float when i used it later in the binary_sensor. But instead, convert it wherever you plan to do the comparison is what should be done?

Thanks for also catching the on/off versus true/false and also pointing to warning as well.

1 Like

I look forward to your final solution, as i need the same for 2 Fan’s :slight_smile:

You have to do it in the differential sensor so you can subtract one from the other. However once this state is calculated and you go to use it elsewhere it is a string. States are always strings. So you need to convert it again in your binary sensor template. Only attributes can have other types.

I know this is already solved but here is a post I wrote in a thread about something similar a few days ago.

maybe it will help clarify. or maybe not… :laughing:

hi @boheme61,

With all the fixes applied, this is what I have in my config:

sensor:
  - platform: template
    sensors:
      e4_temperature_differential:
        friendly_name: "ΔT"
        value_template: >
          {% set e4_temps = states("sensor.e4_2ndfl_temperature") | float, states("sensor.e4_mainfl_temperature") | float, states("sensor.e4_bsmt_temperature") | float %}
          {{ (e4_temps | max) - (e4_temps | min) }}
        unit_of_measurement: "°C"


binary_sensor:
  - platform: template
    sensors:
      circulation_fan:
        friendly_name: "Circulation Fan"
        value_template: "{{ states('sensor.e4_temperature_differential') | float(0) >= 2.0 }}"

You’ll need to do something with that binary sensor in an automation, like turn on/off the fan or the power to an outlet controlling the fan, but i’ll leave that part to your imagination.
The last piece is what’s shown on the frontend which is using the uptime card to show the status of the circulation fan or outlet:

image

Thx alot , yes i had already copied it all, even tom_l’s commends :slight_smile: … the overall “setup” and automation i’ve already figured out … but new to HA, yaml, py and very “rusty” in .js … actually tom_l is 1 of my top “teachers” in here, he just don’t know :laughing:
Thnx again ( 1 less in a big pile of “todo” , well beside actually doit )