Float default value, cant get it done

Hello,

I keep getting this error.

ValueError: Template error: float got invalid input ‘unknown’ when rendering template ‘{{ ((states(‘sensor.solaredge_lifetime_energy_kwh’)|float + 22030))|float(1) | round(1) }}’ but no default was specified.

sensor:
  name: SolarEdge (Lifetime energy - kWh including missing years)
  unit_of_measurement: "kWh"
  icon: "mdi:solar-power"
  state: >
    {{ ((states('sensor.solaredge_lifetime_energy_kwh')|float + 22030))|float(1) | round(1) }}

Also, if I do not use the (( )) it does not round, do i need to add these in every state template?

But you are aware, that you have another float in your template?

BTW with this, you don’t need the second one.

The second float i thought to set the default with, or is default always an int?

    {{ ( states('sensor.solaredge_lifetime_energy_kwh')|float(0) + 22030 )| round(1) }}
1 Like

the default value is the number in brackets so, for you example above…

|float(1)

the default is 1.

With your template…

{{ ((states('sensor.solaredge_lifetime_energy_kwh')|float + 22030))|float(1) | round(1) }}

…what you are essentially saying is:

  • States(): get the state of sensor.solaredge_lifetime_energy_kwh
  • |float: now make it a real number but with no default
  • +22030): now add 22030 to it
  • |float(1): now make it a real number with a default value of 1
  • round(1): now round it to 1 decimal place

I think you can reduce this to just:

{{ (states('sensor.solaredge_lifetime_energy_kwh')|float(1) + 22030) | round(1) }}

note: I have removed a set of redundant brackets as well.

The following should also work as round() now has a default and it implies the float anyway (otherwise there would be nothing to round):

{{ states('sensor.solaredge_lifetime_energy_kwh')|round(1,default=1) + 22030 }}
1 Like

Thank you both!

1 Like

will need to redo some templates, i used a lot of double brackets everywhere

If you use jchh’s suggested template, it will eliminate the error message but your Template Sensor will report 22031 every time sensor.solaredge_lifetime_energy_kwh is unknown.

If that’s how you want the Template Sensor to behave, then fine. Otherwise, you may wish to consider using the availability option in order to skip computing a value whenever sensor.solaredge_lifetime_energy_kwh is unknown and report unavailable.

sensor:
  name: SolarEdge (Lifetime energy - kWh including missing years)
  unit_of_measurement: "kWh"
  icon: "mdi:solar-power"
  state: "{{ states('sensor.solaredge_lifetime_energy_kwh')|round(1,default=1) + 22030 }}"
  availability: "{{ has_value(states('sensor.solaredge_lifetime_energy_kwh')) }}

Alternately, you can make the Template Sensor report its existing value whenever sensor.solaredge_lifetime_energy_kwh is unknown.

sensor:
  name: SolarEdge (Lifetime energy - kWh including missing years)
  unit_of_measurement: "kWh"
  icon: "mdi:solar-power"
  state: >
    {% if has_value(states('sensor.solaredge_lifetime_energy_kwh'))  %}
      {{ states('sensor.solaredge_lifetime_energy_kwh')|round(1,default=1) + 22030 }}
    {% else %}
      {{ this.state | default (0) }}
    {% endif %}
2 Likes

one more question :see_no_evil:
Long time ago i made this script

---
#
# Power Consumption Grid Negative only = Teruglevering
# Bereken Energie verbruik - Opbrengst Zonnepanelen = Netto Verbruik, show positive only dus alleen Teruglevering
#

sensor:
  name: Power Consumption Grid (Neg)
  unit_of_measurement: "W"
  icon: "mdi:transmission-tower-export"
  state: >-
    {% set PowerConsumption = states('sensor.power_consumption_w') | float(0) %}
    {% set SolarPower = states('sensor.solaredge_current_power') | float(0) %}
    {% set PowerConsumptionGridNeg = (PowerConsumption - SolarPower) | int %}
    {{ ( [-10000, PowerConsumptionGridNeg, 0] | sort ) [1] }}

But now i think i can also use this one instead, same results, looks more simple:
But i have no idea anymore what this dit: {{ ( [-10000, PowerConsumptionGridNeg, 0] | sort ) [1] }} changing the 10000 to something els wil result in thet given number instead of the 10000

sensor:
  name: Power Consumption Grid (Neg)
  unit_of_measurement: "W"
  icon: "mdi:transmission-tower-export"
  state: >-
    {% set PowerConsumption = states('sensor.power_consumption_w') | float(0) %}
    {% set SolarPower = states('sensor.solaredge_current_power') | float(0) %}
    {{ ((PowerConsumption - SolarPower)) | round(0) }}

Or have a default of -22030.

My post was meant to make Ricks88 think about what value the Template Sensor should report when sensor.solaredge_lifetime_energy_kwh doesn’t have a numeric value.

Yes, it can report zero but that’s likely to skew the long-term results of a “Lifetime energy” sensor. Rick88 will need to decide what’s the best choice of value for his needs (unavailable, existing value, zero, or something else).

2 Likes

A time ago I reset my Solaredge, not so smart because it lost all data, the “22030” is the amount of kWh that was lost. So I created a new sensor that added the 22030 to the current kWh value of the Solaredge “lifetime energy”.
So not really sure what the best default value should be, maybe 22030, or 0, when Lifetime energy is loaded it will add the 22030 to the current amount by the lifetime energy sensor.