Successfully Integrating Modbus Data Transformation in OH4.0.2

Hey everyone, I wanted to share my journey of figuring out how to transform Modbus data in OH4.0.2. I know many of you might be struggling with similar issues, so I hope this helps!

Initially, I was really frustrated because I couldn’t get my Modbus data to transform correctly. I had installed the JavaScript and Modbus bindings, created the TCP bridge, poller, and data points, but the values weren’t making sense. For example, I was getting a raw value of 2141 for my battery voltage, which should be 214.1 V. I tried everything from inline JavaScript code to creating a separate transform file, but nothing worked.

After hours of trial and error, I finally figured out the solution! The key was to ensure that the JavaScript transform file was correctly placed in the right directory and that the syntax was perfect. Here’s what I did:

  1. Created the Transform File: I made a new file called divideby10.js in the transforms folder. The code was simple:
    javascript
    // Wrap everything in a function to avoid global variable pollution
    (function(inputData) {
    var DIVIDE_BY = 10;
    return parseFloat(inputData) / DIVIDE_BY;
    })(input)

  2. Assigned the Transform: I linked this script to my Modbus data point under the Read Transform field.

The result? Magic! My battery voltage now correctly shows as 214.1 V instead of 2141. It was such a relief!

I also want to mention that the community here is amazing. If I hadn’t seen similar posts and solutions, I might have given up. Kudos to everyone who shares their knowledge!

If anyone else is struggling with Modbus transformations, feel free to reach out. I’d be happy to help! :rocket: