Solar production sum of two strings: delete comma value

Hi,

I hope someone can help me doing this:

I created a sensor of two photovoltaic strings to sum them up with this addition to the configuration.YAML:

# Sensor für Solarman PV1-2 Summe
  - platform: template
    sensors:
      solar_power_total:
        friendly_name: "Solarman PV1-2 Total Solar Power"
        unit_of_measurement: 'W'
        value_template: "{{(states('sensor.solarman_pv1_Power')|float) + (states('sensor.solarman_pv2_Power')|float)}}"

The result is:
1.200,0 W

I would like to strip (delete) the “,0” from this value.
Could someone please let me know how the full code for this sensor should look like so it looks like this:

1.200 W

Thank you in advance for helping out!

pipe int
| int ?

1 Like

Thank you so much. The new sensor looks likes this now:

# Sensor für Solarman PV1-2 Summe
  - platform: template
    sensors:
      solar_power_total:
        friendly_name: "Solarman PV1-2 Total Solar Power"
        unit_of_measurement: 'W'
        value_template: "{{(states('sensor.solarman_pv1_Power')|float|int) + (states('sensor.solarman_pv2_Power')|float |int)}}"

You only need one of |float or |int per sensor.

The float filter converts the state string to a floating point number (with a decimal part).

The int filter converts the state string into an integer (no decimal part).

Either one is sufficient to perform maths with your states.

value_template: "{{ states('sensor.solarman_pv1_Power')|int(0) + states('sensor.solarman_pv2_Power')|int(0) }}"

Great thanx, I changed the code to your suggestion and it works :slight_smile:

EDIT: question how to avoid rounding errors? I was assuming 1.5 | int would round to 2 but it seems to truncate to 1, above then could lead to 0.8 + 0.8 = 0

{{ (states('sensor.solarman_pv1_Power')|float + states('sensor.solarman_pv2_Power')|float ) | round | int }}

??

Like this:

{{ ( states('sensor.solarman_pv1_Power')|float(0) + states('sensor.solarman_pv2_Power')|float(0) )|round(0)|int(0) }}

int doesn’t perform rounding. It returns the integer value of a floating point number.

For future reference, you can modify the round filter’s behavior.


Reference: Templating - Numeric Functions and Filters