Convert a negative number into a positive number

Hi all,

So far I got this :

  • entity: sensor.zonnepanelen_vooraan_actueel_vermogen (comes as a negative number, eg - 500 Watt)
  • entity: sensor.zonnepanelen_achteraan_actueel_vermogen (comes as a negative number, eg - 500 Watt)
  • helper : sensor.totalsolar : sum of both entities, also comes as a negative number, eg -1000Watt)
  • sensor : sensor.stroommeter_actueel_vermogen : the power I use from the electricity net (comes as a positive number, eg 500 Watt)

Now, I want to use the exact amount of power that I use. So I need the sum of the sensor sensor.stroommeter_actueel_vermogen and the helper sensor.totalsolar.

When sensor.stroommeter_actueel_vermogen is 500 Watt and sensor.totalsolar is 300 Watt, I should get 800 Watt. But with the helper I’ve created (sensor.totalenergy) I get 200 Watt, because sensor.totalsolar is a negative number.

So, to have the helper sensor.totalenergy working, I need to change it from a negative value into a positive value. How should I do this?

Absolute value should work…

Hi BauerPowerTech,

Could you please explain this in the most simple way there is and how I have to arrange this? I’m a new user of HA…

In your configuration.yaml file, add

sensor:
  - platform: template
    sensors:
      new_templatesensor_name:
        value_template: "{{ states('sensor.sensor_your_trying_to_make_positive') | abs }}"

It’ll create a new sensor based on the original sensor with a value that is always positive.

Replace the names of the sensors with what you want.

I tried this

sensor:
  - platform: template
    sensors:
      solarpositive:
        value_template: "{{ states('sensor.totalsolar') | abs }}"

So I’ve opened File Editor, opened configuration.yaml and i’ve putted this at the end, then I’ve reloaded it. But at this moment I can’t find it…

I’m at work but I’ll try to help you more when I get time. Might be easier to delete that out and create it in helpers as a template helper.

1 Like

Hi Jake,
I’ve deleted it, and created it in helpers. Don’t know if that is correct what I did is correct…
Would appreciate if you could help me out with this after your work or later. Trying to figure this out since a few days so a few days extra isn’t that hard :slight_smile:


looks like absolute value only works as a filter instead and not as a function. If it’s always negative and you want it to always be positive, try this instead. But keep your units and other info filled in…

1 Like

It should be able to work with | abs try and add | float first:

{{states('sensor.toothbrush_signal_strength') | float | abs }}

My initial sensor output:

3 Likes

Works as a charm!
Thanks a lot!


1 Like