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
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:
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:
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.