[SOLVED] Rule that joins 2 items in a new one, is there a TRANSFORM method?

Hi all
I’m using an energy meter that shows on separate modbus registers sign and value of the active power (sign is 0 or 1). Currently I’m using a rule to join the register and create a signed item:

Number nPariniActivePowerM   	"Active Power [%f]"   		{modbus="GR1:10"}
Number nPariniActiveSignM     "Segno Potenza Attiva [%f]"   {modbus="GR2:0"}
Number nPariniActivePower   	"Active Power [%f]"   	(gInfluxEnergy)

Rule:

var String idModbusRule = "modbusrule"

rule "Modbus Values Conversion"
when
	Item nPariniActivePowerM received update
then
	logInfo(idModbusRule, "Start rule")
	postUpdate(nPariniActivePower, (nPariniActivePowerM.state as DecimalType / 100.0) * (1-(2 * nPariniActiveSignM.state  as DecimalType)))
	logInfo(idModbusRule, "end rule")
end

Is it possibile to do the same join using a transformation rules ?

If possibile I’d prefer JS because I’m already using it to apply scale factor to other registers, eg:

Number nPariniVoltage       	"Parini Volt [%f]"    			(gInfluxEnergy)	{modbus="<[GR1:0:transformation=JS(getDecValue_div1000.js)]"}

getDecValue_div1000.js:

parseFloat(input, 10)/1000;

Thanks

  • Platform information:
    Overall: openHAB 2.2.0-1 (Release Build) - OpenHabIan

  • Hardware: Raspberry Pi 3 Model B Rev 1.2

  • OS: Raspbian GNU/Linux 9 (stretch) - Kernel Linux 4.9.59-v7+

  • Java Runtime Environment:
    openjdk version “1.8.0_152”
    OpenJDK Runtime Environment (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 1.8.0_152-b76)
    OpenJDK Client VM (Zulu Embedded 8.25.0.76-linux-aarch32hf) (build 25.152-b76, mixed mode, Evaluation)

  • openHAB version: openHAB 2.2.0-1 (Release Build) - OpenHabIan

  • modbus binding 1.1.0

You can apply transformation in rules.

The problem would be that you can only supply one input to a transform. Conceivably, that could be a string containing two pieces of information that the transform could unpack to process.
It’s the same problem that prevents you using a transform for your purpose in the binding, only one input.

There seems little point in doing all that, when you have a perfectly workable rule doing the job.


Off topic, there is a slight risk of a “race” condition in your rule.
The two registers are I guess fetched from your device in one poll, one Modbus transfer.

But your rule triggers from just one Item updating, and the Modbus binding will not update all the Items from one poll at the same instant. It is just possible for the rule to process one Item from the recent poll and one “old” one from the poll before. Using two bits of data that don’t belong together.

The easiest way to avoid that is to put a little delay in the beginning of your rule, to allow the second Item time to be updated.
It’s a guess, but I would expect 50mS to be plenty of time.
Thread::sleep(50)

Thanks for the feedback and the hint.

The 2 registers comes from 2 different modbus groups (reads) so maybe that it’s better the use a slightly bigger delay.

I’m not a black belt of openhab so i’ve asked the community in order to verify if there is some trick. The starting need is to minimize the number and the complexity of the rules in order have a responsive OH system.

I’ll set the question as “solved” and next time I’ll ask for a “code review” :slight_smile:

In that case, you can never be sure the target device hasn’t changed them between the reads, which cannot be fixed by openHAB. Don’t bother with any delay, it won’t help there.

Unless you can set up a Modbus read poller to read both in one go.