I am using OH2.2-1 on Raspberrypi2B.
I have a thermostat that give me (finally ) two values from a json-object. I put these in two items called WK_room_temp_1_lsb and WK_room_temp_1_msb. When I show these on my sitemap, they are integers and they do show up.
To calculate the temperature, I made a Number item T_WK_SP_real which I want to update with a rule. For the calculation I made this file:
/transform/bit2celcius.js
(function(i){
var inputarray = i.split(" ");
var lbs = inputArray[0];
var mbs = inputArray[1];
var celcius = (lbs + mbs*256)/100
result = celcius
})(input)
and this is my rule, which I roughly copied from examples here on the forum, but I do not fully understand, so hopefully you can help me bit. I do understand it should paste the two values, separated by a space which later is used to split them up again.
rule "Update Livingroom Thermostat real temperature"
when
Item WK_room_temp_1_lsb changed or
Item WK_room_temp_1_msb changed
then
T_WK_is.postUpdate(transform("JS","bit2celcius.js",(WK_room_temp_1_lsb.state as DecimalType).concat(" ").concat(WK_room_temp_1_msb.state as DecimalType)))
logInfo("Update Thermostat", "temperature calculated")
end
When I save the rules-file I get an error in my log: concat is not part of OH2.
So how should I bring two values to the transformation?
Almost forgot: I have put an extra trigger in place to trigger the rule. I never get ’ temperature calculated’ in the log. I also tried the following for simplicity:
So if you are looking for simplicity, why not just implement it in the rule?
rule "Update Livingroom Thermostat real temperature"
when
Item WK_room_temp_1_lsb changed or
Item WK_room_temp_1_msb changed
then
val lbs = WK_room_temp_1_lsb.state as Number
val mbs = WK_room_temp_1_msb.state as Number
val celcius = (lbs + mbs * 256) / 100
T_WK_is.postUpdate(celcius)
logInfo("Update Thermostat", "temperature calculated")
end
Pfff, I tried that yesterday but not exactly apparently. So I searched ’ math openhab’ and only found plus or minus and everything more complicated than that was with transforms. So that is how I got there.
But THANKS again Rich, you’ re the best. Now I can sleep well and tomorrow go on to the next hurdle of openhab, which wil be sending out an url with data in the other direction.
The Rules DSL supports all the math operations including modulo and boolean operations. I think it may even supports binary shift operations, but don’t know for sure. And this is pretty much true of ALL programming languages.
+ addition - subtraction * multiplication / division % modulo (i.e. remainder) > greater than < less than == equal === the same object = assignment <= less than equal >= greater than equal != not equal && AND || OR
All of these operators work as written more or less in ALL programming and scripting languages.
Searching for “math openhab” is unlikely to turn on useful results because postings with examples are unlikely to say “here is some math” and even the official docs call them “operators”. Sadly this is just one of those cases where the nomenclature doesn’t line up well for those who don’t already know a little about programming.
I’v tried to read that, but it doesn’t really help me. I need an english and a programmers dictionairy. Specially this part is all just blala to my ears:
Type Casts
A type cast behaves exactly like casts in Java, but has a slightly more readable syntax. Type casts bind stronger than any other operator but weaker than feature calls.
The conformance rules for casts are defined in the Java Language Specification. Here are some examples:
something as MyClass
42 as Integer
Instead of a plain type cast it’s also possible to use a switch with a type guard which performs both the casting and the instance-of check. Dispatch methods are another alternative to casts that offers the potential to enhance the number of expected and handled types in subclasses.
To explain those statements would take 20-30 pages. If you really want to understand you should take one of the many online courses on programming. The MIT intro to programming course that uses Python would be a good choice.
The only part of that section you need to understand to code Rules is:
every object has a Type
to cast (i.e. identify an object as a specific type) you use objectName as ClassName, e.g. MySwitch as SwitchItem
Don’t feel like you have to read the Xtend docs too closely. Use it as a reference for when you want to know the syntax for how to do something like a switch statement or if statement. They were not written for a general audience.