Template warning: 'float' got invalid input 'unknown'

I have the following in my configuration.yaml, used to extract some values from a home battery:

  # Sonnen Battery via REST API 
  - platform: rest
    name: JSON sonnen
    json_attributes:
      - Consumption_W
      - GridFeedIn_W
      - Production_W
      - USOC
      - Uac
    resource: http://192.168.99.168:8080/api/v1/status
    value_template: '{{ value_json.sonnen is defined }}'
  - platform: template
    sensors:
      sonnen_power_consumption:
        friendly_name: 'Power Consumption W'
        value_template: '{{ states.sensor.json_sonnen.attributes["Consumption_W"] (0) }}'
        unit_of_measurement: watt
      sonnen_power_supply:
        friendly_name: 'Electricity Feed In W'
        value_template: '{{ states.sensor.json_sonnen.attributes["GridFeedIn_W"] }}'
        unit_of_measurement: watt
      sonnen_power_generation:
        friendly_name: 'Power Generation W'
        value_template: '{{ states.sensor.json_sonnen.attributes["Production_W"] }}'
        unit_of_measurement: watt
      sonnen_electricity_storage:
        friendly_name: 'Remaining Charge %'
        value_template: '{{ states.sensor.json_sonnen.attributes["USOC"] }}'
      sonnen_alternating_current:
        friendly_name: 'UAC'
        value_template: '{{ states.sensor.json_sonnen.attributes["Uac"] }}'
        unit_of_measurement: volt

Although this is working it does cause a lot of warnings in the logs:

2021-12-28 00:35:56 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '-{{ states('sensor.sonnen_power_consumption')|float}}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-12-28 00:35:56 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '-{{ states('sensor.sonnen_power_consumption')|float}}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-12-28 00:35:56 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '-{{ states('sensor.sonnen_power_consumption')|float}}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2021-12-28 00:35:56 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'unknown' when rendering template '-{{ states('sensor.sonnen_power_consumption')|float}}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

So apparently I need to change this value template in some way to stop the warnings. However I don’t know what to change? Has anybody got any advice on this?
Thanks in advance.

I believe the quickest way to eliminate the warning, if you want the current behaviour, is to change | float to | float(default=0.0)

Edit: Just noticed that the template sensors listed above don’t appear to have the float filter at all, so it looks like it is a different template sensor or binary sensor (or maybe an automation) which is using it and you’d need to find that and change it as required.

why is there that extra “(0)” at the end?

why is there that extra “(0)” at the end?

@finity
Sorry, that’s a typo from experimenting with this float issue. The 0 is not in the production config so please ignore it.

The first part of this:

  - platform: rest
    name: JSON sonnen
    json_attributes:
      - Consumption_W
      - GridFeedIn_W
      - Production_W
      - USOC
      - Uac
    resource: http://192.168.99.168:8080/api/v1/status
    value_template: '{{ value_json.sonnen is defined }}'

returns the following from the battery:

{"Apparent_output":104,"BackupBuffer":"0","BatteryCharging":false,"BatteryDischarging":false,"Consumption_Avg":556,"Consumption_W":632,"Fac":50.016002655029297,"FlowConsumptionBattery":false,"FlowConsumptionGrid":true,"FlowConsumptionProduction":true,"FlowGridBattery":false,"FlowProductionBattery":false,"FlowProductionGrid":false,"GridFeedIn_W":-472,"IsSystemInstalled":1,"OperatingMode":"2","Pac_total_W":-7,"Production_W":167,"RSOC":9,"RemainingCapacity_Wh":888,"Sac1":104,"Sac2":null,"Sac3":null,"SystemStatus":"OnGrid","Timestamp":"2021-12-27 22:06:56","USOC":2,"Uac":242,"Ubat":48}

But I don’t see any ‘float’ declarations anywhere so don’t know what to set to 0.0?

There are other templates that I’ve managed to fix but I’ve never been able to nut this one out!

Thanks for your help.

I’ve tried this:

  - platform: rest
    name: JSON sonnen
    json_attributes:
      - Consumption_W|float(default=0.0)
      - GridFeedIn_W|float(default=0.0)
      - Production_W|float(default=0.0)
      - USOC|float(default=0.0)
      - Uac|float(default=0.0)
    resource: http://192.168.99.168:8080/api/v1/status
    value_template: '{{ value_json.sonnen is defined }}'

But nope, doesn’t work!

You need to edit wherever you are using those sensors, the code
{{ states('sensor.sonnen_power_consumption')|float}}
just needs to be updated to
{{ states('sensor.sonnen_power_consumption')|float(0)}}

That’s the issue. Nowhere in my configuration is there such a statement. This is all that I can find:

      sonnen_power_consumption:
        friendly_name: 'Power Consumption W'
        value_template: '{{ states.sensor.json_sonnen.attributes["Consumption_W"] }}'
        unit_of_measurement: watt

If I search configuration.yaml for sensor.sonnen_power_consumption it doesn’t exist, and float is nowhere in this block of configuration, only the above.

How do I change this to set a default of 0?

Did you check your automations/scripts?

Yes, no automations or scripts that I can see.

The data from this is used to produce this:

That code definitely exists somewhere in your config. Do a search for that and you will find where your problem is.

Change that code to.
states('sensor.sonnen_power_consumption')|float(0)

It is working now, but will cease to work in v2022.1.

This is the configuration that produces the gauge:

type: custom:card-templater
entities:
  - sensor.sonnen_power_supply
  - sensor.sonnen_power_generation
  - sensor.sonnen_power_consumption
card:
  type: custom:canvas-gauge-card
  card_height: 125
  entity: sensor.sonnen_power_supply
  title_template: '{{ state_attr(''sensor.sonnen_power_supply'',''friendly_name'') }}'
  font_size: 1em
  gauge:
    borders: false
    height: 220
    highlights:
      - color: rgba(21,242,27,0.75)
        from: 0
        to_template: '{{ states(''sensor.sonnen_power_generation'')|float}}'
      - color: rgba(245,24,57,0.75)
        to: 0
        from_template: '-{{ states(''sensor.sonnen_power_consumption'')|float}}'
    majorTicks:
      - '-5000'
      - '-4000'
      - '-3000'
      - '-2000'
      - '-1000'
      - '0'
      - '1000'
      - '2000'
      - '3000'
      - '4000'
      - '5000'
    maxValue: 5000
    minValue: -5000
    minorTicks: 2
    startAngle: 90
    strokeTicks: true
    ticksAngle: 180
    title: Solar (W)
    type: radial-gauge
    valueBox: true
    width: 220

This is using HACS canvas-gauge-card and a js script called lovelace-card-templater which allows three measurements in one gauge.

  1. Consumption in red to the left
  2. Production in green to the right
  3. and grid feed-in or feed-out which is the needle swinging from positive to negative.

However these three metrics all come from the configration.yaml:

  # Sonnen Battery via REST API 
  - platform: rest
    name: JSON sonnen
    json_attributes:
      - Consumption_W
      - GridFeedIn_W
      - Production_W
      - USOC
      - Uac
    resource: http://192.168.99.168:8080/api/v1/status
    value_template: '{{ value_json.sonnen is defined }}'
  - platform: template
    sensors:
      sonnen_power_consumption:
        friendly_name: 'Power Consumption W'
        value_template: '{{ states.sensor.json_sonnen.attributes["Consumption_W"] }}'
        unit_of_measurement: watt
      sonnen_power_supply:
        friendly_name: 'Electricity Feed In W'
        value_template: '{{ states.sensor.json_sonnen.attributes["GridFeedIn_W"] }}'
        unit_of_measurement: watt
      sonnen_power_generation:
        friendly_name: 'Power Generation W'
        value_template: '{{ states.sensor.json_sonnen.attributes["Production_W"] }}'
        unit_of_measurement: watt
      sonnen_electricity_storage:
        friendly_name: 'Remaining Charge %'
        value_template: '{{ states.sensor.json_sonnen.attributes["USOC"] }}'
      sonnen_alternating_current:
        friendly_name: 'UAC'
        value_template: '{{ states.sensor.json_sonnen.attributes["Uac"] }}'
        unit_of_measurement: volt

It’s not used anywhere else?

Perhaps

 - platform: template
    sensors:
      sonnen_power_consumption:

should be

 - platform: template
    sensors:
      sonnen_power_consumption|float(0):

It’s right there in your config.
from_template: '-{{ states(''sensor.sonnen_power_consumption'')|float}}'
Change that to
from_template: '-{{ states(''sensor.sonnen_power_consumption'')|float(0)}}'

Anywhere you’ve used |float or |int where there is ANY possibility it can return a non numerical value (example - ‘unknown’) you need to give it a default value. In this case I suggested a default value of zero.

No, that is not where you use a default value. That is the template sensor name. It’s also the old method of creating template sensors. There is now an updated way to create templates.

Great thanks.
The hunt goes on.
Regards
Rob

I don’t think that is right.

I think it should be:

{{ states(''sensor.sonnen_power_consumption'')|float(0)}}

notice no “|” after float.

1 Like

Of cause, in the canvas gauge card in lovelace. Can’t see for looking. Thanks

D’oh! Yeah…

Gotta stop playing on the net half asleep. Got it right the first time I guess that count for something?

2 Likes

Of course! :laughing: