Sensor with positive and negative range of data to be cut off at 0 value (grid consumption)

Hi community

First of all sorry if I ask a stupid and probably redundant question as this is my first thread in the forum. So please bare with me.

I have a sensor which provides me the live energy consumption from the grid.
As I also have a solar system I give something back to the grid in case my consumption is lower.
As a result this sensor turns into a negative value which I don’t want to have (I use a different sensor for that.

Now my question is how I can get a solution to only get the positive values and stop measurement at 0 once it turns into negative.
I was hoping for a template but I struggle to find the right solution.

This is the sensor I am talking about:
- name: “Strom-aktuell”
unit_of_measurement: “W”
device_class: “power”
state_class: “measurement”
state: >
{{ state_attr(‘sensor.poweropti’, ‘Watt’) }}

Thank you for your answers and support already! :slight_smile:

{{ [state_attr('sensor.poweropti', 'Watt'), 0]|max }}
1 Like

that was way too easy! :stuck_out_tongue:
Thank you for this prompt and helpful comment.

1 Like