Template - Multiply with -1 returns string

Hey, i want configurate a template, which return the positive value of a negative value. For example:

-34 -> 34

but I get a string as return value

-34 -> 34’

example

What do i have to do to get a number as return value?

Here is my template:

battery_charge:
        value_template: >
                        {% set batter_cons = states('sensor.powerwall_battery_now') | float %}
                        {% if batter_cons < 0 %}
                           {{ batter_cons | float * (-1)}}
                        {% else %}
                           {{0 | float}}
                        {% endif %}'
        device_class: power
        unit_of_measurement: kW

Ok never mind, after hours I found my mistake at the end of the statements, i have a " ’ " at the end (facepalm)

You can simply this template with the absolute value filter, | abs Which always returns a positive value.

value_template: "{{ states('sensor.powerwall_battery_now') | float | abs }}"

Thx for the tip :slight_smile:

1 Like

Though I just realised you are returning 0 for positive values. If this is required you are better off with your original template.