ValueError: could not convert string to float: '- return my sanity please

First question here, have been developing my Homeassistant now for about a year.
Great source of information, thank you all.
However one thing is driving me completely to distraction. Two days banging my head (Glaswegian, you get the picture)

I need to multiply a sensor by 1000 - simple huh, can I do it ? Not a stick of it.
Currently getting this error in logs.
ValueError: could not convert string to float: ‘’

I have a sensor created by SMA Energy Meter that is in Kw and am trying to create a sensor with the value in W. ( I want to do a wee subtraction later) first things first.

This is my code in sensors.yaml

 - platform: template
    sensors:
      day_watts:
        friendly_name: 'Day Watts used'
        device_class: power
        unit_of_measurement: "W"
#        state:  {{ ( states('sensor.energy_meter_mains_power' ) | float(0) * 1000 )}}
        value_template: >
         {% if states('sensor.energy_meter_mains_power') | float(0) > 0 %}
           {{ states('sensor.energy_meter_mains_power') | float(0) * 1000 }}
         {% endif %}

I have also tried various flavours of this value_template too

{{ ( states(‘sensor.energy_meter_mains_power’ ) | float(0) ) * 1000 }}

and nothing gives me the value I want. There is a Kw value in sensor.energy_meter_mains_power

Or should I try the foetal position in the corner and dribble :slight_smile:

What happens if your power is not greater than 0?

You need an else statement.

You should be using the new template integration for new sensors. The legacy template sensor platform is still supported but it is not getting new features.

configuration.yaml, not sensors.yaml

template:
  - sensor:
      - name: 'Day Watts used'
        device_class: power
        state_class: measurement
        unit_of_measurement: "W"
        state: "{{ states('sensor.energy_meter_mains_power' ) | float * 1000 }}
        availability: "{{ has_value('sensor.energy_meter_mains_power' ) }}

Thank you,

I haven’t figured out the new thing yet. Probably as I haven’t figured out the old way.

I don’t want a big configuration.yaml, last time I tried to split it up everything stopped working. I want to !include what I want and not have to trawl through a whole bunch.

Just the way my mind works. Perhaps if I start of with it all in config and then split it out after. I find yaml not particularly intuitive, perhaps because of my lack of understanding.!

Thank you again. I also was able to “fix” it by changing the source to W as well !! duh me

You could easily create this sensor (although not yet the availability template) in the UI under Helpers and not touch any YAML.

1 Like

Ooh I like that idea actually as it is indeed a helper rather than a new variable.

It’s exactly the same thing as far as the system is concerned. It’s a new entity — the only difference is the way it’s defined.

:slight_smile: Sadly no arithmetic takes place and whatever i do the sensor is always unavailable.

I have created a helper -

{{ states('sensor.sma_power') - states('sensor.sma_energy_meter_psupply')
          }}`

unavailable

I have this in my configuration.yaml

      - name: "Day Power"
        unique_id: power_during_the_day
        device_class: power
        unit_of_measurement: 'W'
        state_class: measurement
        icon: mdi:alpha-a
        state: >
          {% set home_generated = states('sensor.sma_power') | float(0) %}
          {% set total_to_grid = states('sensor.sma_energy_meter_psupply') | float(0) %}
          {{ ((home_generated - total_to_grid) ) | round(1, default=0) }}
                     
        availability: >
          {{ 
            states('sensor.sma_power') | float(none) != none and
            states('sensor.sma_energy_meter_psupply') | float(none) != none
          }}                    

unavailable

yes there are data in the orginal sensors

What am I doing wrong, or is it just too much to ask.

It seriously cannot be this hard, I’ll deal with less than zero when I get an actual operation to happen.

{{ states('sensor.sma_power') |float(0) - states('sensor.sma_energy_meter_psupply') |float(0)
          }}`
1 Like

Nope ( EDIT : thank you, I am an idiot)

Stops listening to the sensor then and still unavailable. Tried that lol :slight_smile:

Actually, you sorted it, I was just being myopic. Must have missed something, thank you so much. Wohoo arithmetic.

1 Like