Use only positive value


I have a sensor that gives me positive value one discharging from my battery and negative value when charging it.

I would like to use only positive value in order to implement in the new Energy management system.


1 Like

{{ state('something') | abs}}

Should give you positive values, but not sure that is what you are actually asking about.

Thanks hellis, i thinks that abs will give me the absolute value of a number, so if i have a negative value i would convert in positive one.

That is correct.
Your question is not explaining what you actually want so it’s hard to help you.

i try to explain better.

I have a sensor that can give me positive and negative value, i would like to create a sensor template that will consider only the positive values.

{% if states('something') | float >= 0 }
{{ states('something') }}
{% endif %} 
1 Like

could you put all the syntax?

the name of my sensor is : sensor.batteria_storage_power


Hi, did you solve this? How does your Sensor look like?

This is an example of my mqtt sensor working, I hope it helps you.

      - unique_id: freedsgrid
        state_topic: "freeds_1ec8/wgrid"
        value_template: >
            {% if value | float <= 0 %}
            {{ value | multiply (-1) }}
            {% else %}
            {{ '0' }}
            {% endif %} 
        name: solar_gridpower
        unit_of_measurement: "W"

maybe “else” is not necessary but it works fine

1 Like