How to transform "2147483647" INT32 Modbus result to "0" from Huawei Luna2000 Battery register

  • Platform information:
    • Hardware: Synology 920+ (INTEL)
    • OS: Linux / Docker Image
    • openHAB version: openHAB 3.1.0
    • Database: PostgreSQL

I am still pretty new to openhab and I made good progress so far in using it to read data from my Huawei solar inverters and battery stack.

However, I ran into one issue with data read from the Luna2000 battery that is giving me problems and I would like to know if you could point me in a direction.

Whenever the battery is offline (e.g. at night), the charge/discharge register alters between “0” and “2147483647”. In Grafana, I can force visualization to convert the maxed INT32 value to “0” for display purposes. This does not however cure the problem in the database where these values make it impossible to use the alternating modbus output for any sensible calculations.

Is there a quick and easy method to transform “2147483647” to “0” when receiving data from the register?

Thanks a lot in advance

See-

Hey, thanks for pointing me towards scripts. I tried using rules with an attached script here but it obviously doesn’t work as intended because the rule execution doesn’t catch all the results.

//if Battery is off
if(itemRegistry.getItem('ModbusData37001ESU1ChargeandDischarge_ValueasNumber').getState() == 2147483647)
  {
    events.postUpdate('ModbusData37001ESU1ChargeandDischarge_ValueasNumber',0);
  }

I’ll try looking into the solution of the thread.

Do it as a read transformation at the Modbus channel, before it gets to the Item, if you want sensible charting.

Yes, I’ll try to place it inside the /transforms folder.

Can I name the number “2147483647” explicitly in the code fragment and have it transformed to “0”? In my case, I’m not dealing with negative numbers but with only one huge positive number.

Do as you wish, that’s the point of transformations.
Why not have it discard any ridiculously big number over some threshold.

2147483647 is 7FFFFFFF in hexadecimal, maximum positive 32 bit integer, to explain where it comes from

Hi @rossko57 , sorry for asking again - I’m not really apt at Javascript.

My transform would look like:

(function(i) {
    if(i == 2147483647) return 0;
    return i;
})(input)

That should return “0” for every “2147483647”, correct?

Thanks a bunch

I think this is doing the trick. Let’s hope I don’t zero everything now =)

EDIT: It’s working now. Thanks for the help!!

1 Like