How is it possible to perform calculations with the obtained values?
thing-channel MODBUS thermometer gets a value of 227 and I need to divide it by 10 (value / 10)
thing-channel MQTT voltage gets a value of 132 and I need to subtract 20 and divide by 3 (value - 20 / 3)
where and how it can best be done in openhub.
Thank you in advance
Well, it depends
Either use a rule or a transformation script.
rule "make a calculation"
Item myInputItem changed
if(!(myInputItem.state instanceof Number))
val input = myInputItem.state as Number // get the value
val output = Math.sqrt((myInput / 10 + 4) * 5.3 + 75.2345) // some crazy calculation
You can use similar options for transformation, either inline or via text file. You can use every installed Scripting language for both options.
Some calculations can be done via unit system, i.e. maybe you get a temperature as °F, but multiplied with 10. So the “correct” unit would be d°F (this is no common unit. however…)
If getting this temperature via mqtt or http, use a number channel and set
If you want the temperature in °C with one decimal fraction, set the
stateDescription pattern for the linked Number:Temperature Item to
%.1f °C and boom, you will get the correct Temperature.
If you want the temperature in Kelvin, use
%.1f K instead. And of course you could use
°F as well…
A third option is to use a Profile. The Modbus add-on comes with an offset profile which is designed to do exactly this.
Thank you all for your kind advice.
So I ended up using the following:
To divide by 10, I used the Gain-Offset Correction profile where I set the Gain to 0.1
To get the decimal place, I used the Metadata State description specifically suggested pattern %.1f °C
So now I have the three-digit number “999” obtained from the register of the modbus thermometer in the form of 99.9 °C as I needed.
I would also like to know if there is a description of the language in which you sent me the “rule” proposal. As a programmer, it’s quite clear to me, but I don’t know the declaration of variables (val ?), keywords (postUpdate ?) construction (as Number ?). In short, at least some kind of reference manual. Maybe it could be avoided by watching others, but I don’t have that much free time.
thanks in advance for any tip Jan
The rule is the good ole openHAB Rules DSL. This was initially created with XTend (if I remember correctly) and as XTend itself is built with Java, there are some similarities.
val is a constant definition,
var would be a variable
.state will provide the current state of an Item.
as Number is Type casting to ensure that the value is interpreted as a Number
.postUpdate() is a method which is available for all Items to set the state of the Item.
For a first reference, take a look at
Yes, that’s pretty good, thanks.
thanks for tip
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.