Closed dakipro closed 6 years ago
Are you sure msg.payload_in is a number and not a string ?
Hm.. it looks that it is a string actually. I am not that level of javascript expert, do you have a theory of why would comparing a string vs string not give correct results? Or better yet, why would converting second string to a int give correct results in this case? Thanks!
String comparison is alphabetical, not numerical.
https://www.w3schools.com/js/js_comparisons.asp states 👍
When comparing a string with a number, JavaScript will convert the string to a number when doing the comparison. An empty string converts to 0. A non-numeric string converts to NaN which is always false.
Hi, I was writing a simple rule that should dim the lights if they are brighter then X. I have a node that sets payload to f.eks. 40, then I call openhab2-get to get current value, then I do a simple function that does
if(msg.payload_in < msg.payload.state){ retMsg = {}; retMsg.payload = msg.payload_in; return retMsg; }
However, it works fine but not if msg.payload.state is full brightness (value of 100). So if a value is 99, then it works and compares it fine with f.eks. 40, but if it is a 100 then I must cast it to int and compare it. I usually do this simply by putting + in front of the value, so that it becomes
if(msg.payload_in < +msg.payload.state){
Now I might be wrong about the casting, I wasn't really debugging it, and I might be doing something totally wrong, but it got me by a surprise a few times when the rule didn't fired, and it was very difficult to figure out why.
It would be great if unpredictable behavior could be minimized if possible. Thanks!