Power sensor template without negativ power

I have a templatesensor with power entities. Somtime i have a wrong negativ value.
Can i fix it in my template?

{{ states('sensor.sma_pv_gedo_pv_power')|float(0) + states('sensor.sma_pv_gedo_metering_power_absorbed')|float(0) + states('sensor.sma_pv_gedo_battery_power_discharge_total')|float(0) - states('sensor.sma_pv_gedo_battery_power_charge_total')|float(0) - states('sensor.sma_pv_gedo_metering_power_supplied')|float(0) }}

Share the whole template sensor config if you are using YAML.

You will likely need to add this: Why an availability template is important for energy template sensors

In your case you only want to perform the calculation if all sensors are reporting valid numbers. If any them is unknown it will be replaced with 0 by your float(0) default. This could lead to strange outputs.

But the availability format is different if you are using the legacy template sensor platform. Which is why we need to see the whole config, not just the template.

If you are using the UI then you will need to move it to yaml for this. You can not do this in the UI.

I use the Homeassistant Helpers. Template helper. Not the yaml.

Which is why I said:

I found a UI soulution.

I used range with 0 - 100000. And it works.

Thats the right way:

{{ [ states('sensor.sma_pv_gedo_pv_power')|float(0), 0.0 ] | max + [ states('sensor.sma_pv_gedo_metering_power_absorbed')|float(0), 0.0 ] | max + [ states('sensor.sma_pv_gedo_battery_power_discharge_total')|float(0), 0.0 ] | max - [ states('sensor.sma_pv_gedo_battery_power_charge_total')|float(0), 0.0 ] | max - [ states('sensor.sma_pv_gedo_metering_power_supplied')|float(0), 0.0 ] | max }}

No it really isn’t. This is the right way:

availability: >
  {{ has_value('sensor.sma_pv_gedo_pv_power') and
     has_value('sensor.sma_pv_gedo_metering_power_absorbed') and
     has_value('sensor.sma_pv_gedo_battery_power_discharge_total') and
     has_value('sensor.sma_pv_gedo_battery_power_charge_total') and
     has_value('sensor.sma_pv_gedo_metering_power_supplied') }}

However it is only available in YAML.