Custom sensor: Filter out negative values

I have this custom sensor which converts Watts (W) into KWh. It works, but sometimes there are being falsely negative values being transmitted. Is there any way to ignore negative values completely by just setting them to a value of zero or something similiar? Thank you in advance!

sensor:
 # SolarMax:
  # SM3000s: W (Power) to kWh (Energy) converter:
  - platform: integration
    source: sensor.solarmax3000s_01_channel_a_power
    name: solarmax3000s_01_channel_a_energy
    unit_prefix: k
    round: 2

You have to filter the source sensor, sensor.solarmax3000s_01_channel_a_power.

How is that created?

Thank you for having a look at this.
Here is the original sensor:

template:
  - sensor:
    - name: solarmax3000s_01_channel_a_power
      device_class: power
      unit_of_measurement: W
      state: >
        {{ states('sensor.solarmax3000s_01').split(';')[6] | regex_findall_index('(?<=\=)(.*?)(?=\|)') | int(base=16) | int / 2 | int }}

Do you wan the value set to zero or hold the last value when the result of your template is negative?

Zero:

template:
  - sensor:
    - name: solarmax3000s_01_channel_a_power
      device_class: power
      unit_of_measurement: W
      state: >
        {% set value = states('sensor.solarmax3000s_01').split(';')[6] | regex_findall_index('(?<=\=)(.*?)(?=\|)') | int(base=16) / 2 %}
        {{ value if value >= 0 else 0 }}

Hold last value:

template:
  - sensor:
    - name: solarmax3000s_01_channel_a_power
      device_class: power
      unit_of_measurement: W
      state: >
        {% set value = states('sensor.solarmax3000s_01').split(';')[6] | regex_findall_index('(?<=\=)(.*?)(?=\|)') | int(base=16) / 2 %}
        {{ value if value >= 0 else this.state }}

Also there is no need to do this: 2 | int , 2 is already an integer. There was another redundant int filter I’ve removed as well, after the |int(base=16). That already converted your hexadecimal string to a decimal integer.

It would be interesting to see what the result of this is:

{{ states('sensor.solarmax3000s_01').split(';')[6] | regex_findall_index('(?<=\=)(.*?)(?=\|)') }}

When your negative numbers occur.

Could be something amiss with your regular expression.

Hmm…the problem seems to be elsewhere.
Let me try to get this together:

Your first suggestion (zero) gives me the following output when I put it into the templates mask:

template:
  - sensor:
    - name: solarmax3000s_01_channel_a_power
      device_class: power
      unit_of_measurement: W
      state: >
        
        380.0

Your second suggestion (hold last value) gives me the following output when I put it into the templates mask:

template:
  - sensor:
    - name: solarmax3000s_01_channel_a_power
      device_class: power
      unit_of_measurement: W
      state: >
        
        405.0

The result of your third code snippet is:

36A

So there doesn’t seem to be any kind of negative value here.

What I mentioned, when I checked the state of the following sensor (which is the one that I showed in my initial post) is this:

So there you have it. Somehow this sensor is strangely putting out a negative value, even when in fact the source sensor input is obviously positive.

Now let’s move further: Within the energy dashboard I am using this sensor for collecting solar production values, obviously, on the energy configuration menu, HA is complaining about negative values:

Another strange thing is that the energy dashboard graph itself is showing only positive values, even if the input values are obviously negative:

I don’t know where so start troubleshooting right now, I can’t get my head around this. Seems there is an issue within the conversion operation within the solarmax3000s_01_channel_a_energy sensor, but I cannot imagine where the issue comes from.

Just for completion:
I ended up replacing the integration sensor by a Riemann sum integral sensor (Left Riemann sum) which solved the issue.

It is still a misery why the value was ever getting negative, but well…it works now.