I have been setting up the Netatmo anemometer lately (OH 2.3), with some problems setting rules… (all else works fine).
The following instruction simply is not executed and I do not see errors in the logs:
if (Netatmo_Wind_Angle.state < 8) {
Netatmo_Winds_Dir.postUpdate(0) //THIS IS NOT DONE
logInfo("netatmo", "0") //THIS COMMENT NEVER SHOWS IN LOG
}
The “if” is never true.
I understood there is something to do with the item type, which is declared as per documentation:
Number:Angle Netatmo_Wind_Angle "Direzione del vento [%d\u00B0]" <mywind> { channel...
In fact, the following does work perfectly:
var int Netatmo_Prev_WindDir //USING A VARIABLE SINCE HAVING MANY IFs
(...)
Netatmo_Prev_WindDir = (Netatmo_Wind_Angle.state as Number).intValue
if (Netatmo_Prev_WindDir < 8) {
Netatmo_Winds_Dir.postUpdate(0)
logInfo("netatmo", "0")
}
Same thing seems to happen with wind strength, but not with temperatures.
Is that normal or am I doing something wrong?
By the way, I have to say that the new item types for numbers with units is quite confusing… In my opinion the user/programmer shall know what the number represents without telling the system.
P.S.: just for info, the above code is part of a rule to discretize 360deg for having dynamic wind arrow icon shown: the documented methods for dynamic icons (e.g. “icon-0”, “icon-15”, “icon-30”, etc) is not working with wind angle unless pecisly set (e.g. if the angle is 188 deg I must have a “icon-188”, not “icon-187” nor “icon-190”) . May this have to do with item type too…
Thanks, I will try that.
Still I believe this is a silly complication: introducing special characters (for units) in the code looks weird to me and might be dangerous. What if that would be done the same way with currencies for example? ("|$", “|£”, etc.) …mmm minefield.
Why temperatures are different? I do measure in ° as well…
Yes Correct. So when we would need to do calculations involving more than one physical unit we will need to do conversion for all of them…
Not sure this is a good path… -> math limitations and errors.
I guess I will need to accept that math, physics an IT are mixing in the same cookie jar.
When I use these in a rule with several comparisons and checks I use a variable:
var Number Netatmo_Prev_WindDir //USING A VARIABLE SINCE HAVING MANY IFs
(...)
Netatmo_Prev_WindDir = Netatmoo_Wind_Angle.state as Number
if (Netatmo_Prev_WindDir < 8) {
Netatmo_Winds_Dir.postUpdate(0)
logInfo("netatmo", "0")
}
I can’t find proper documentation on these new “special types” and how to deal with them. Do you know where it might be?
Yesterday I spent half a day on this problem, it would be better to avoid others do the same.
I have read your posts again but I am not convinced. Sorry for being boring.
I tried:
var Number Netatmo_Prev_WindDir //USING A VARIABLE SINCE HAVING MANY IFs
(..)
Netatmo_Prev_WindDir = Netatmoo_Wind_Angle.state as Number
instead of
var int Netatmo_Prev_WindDir //USING A VARIABLE SINCE HAVING MANY IFs
(...)
Netatmo_Prev_WindDir = (Netatmo_Wind_Angle.state as Number).intValue
but actually, for some reason, this is working bad: wind angle is 270° but the condition that triggers is (<8)… Lowering that number to 3 it triggers the second condition (<15).
I am not investigating over, there is for sure a good reason, but have no time to clear it out. I am wondering if it really is (or has to be) so complicated.
I guess I’ll stick with my method, that woks well: I am not a PRO but my teacher used to say “always declare variables for what they are!”
Many years have passed thou and things got more comfortable since then, luckily (->auto cast). But still we get problems if new types are introduced. Moreover, these do not give errors, just but wrong results
By the way, why do I have the temperature declared like this
I would like to convert the angle into a direction-string, but I can’t access a predictable way in the rule.
I tried:
logInfo("Angle: ", Netatmo_Wind_WindAngle.state.toString)
1. var int angleAsInt = (Netatmo_Wind_WindAngle.state as Number).intValue
2. var int angleAsInt = (Integer.parseInt(Netatmo_Wind_WindAngle.state) as Number).intValue
3. var int angleAsInt = Integer::parseInt(Netatmo_Wind_WindAngle.state)
4. var int angleAsInt = Integer::parseInt(Netatmo_Wind_WindAngle.state.toString)
5. var int angleAsInt = new java.math.BigDecimal(Integer::parseInt(Netatmo_Wind_WindAngle.state))
logInfo("AngleAsInt: ", angleAsInt)
The 2nd log output always gives the following error message:
Error during the execution of rule ‘windDirection’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object) on instance: null
rule "Winddirection"
when
Item Netatmo_Wind_WindAngle changed
then
// In case of error, direction is undef
var direction = "UNDEF";
var int angle = (Netatmo_Wind_WindAngle.state as Number).intValue
var directions = 8;
var degree = 360 / directions;
angle = angle + degree/2;
if (angle >= 0 * degree && angle < 1 * degree) direction="N";
if (angle >= 1 * degree && angle < 2 * degree) direction="NE";
if (angle >= 2 * degree && angle < 3 * degree) direction="E";
if (angle >= 3 * degree && angle < 4 * degree) direction="SE";
if (angle >= 4 * degree && angle < 5 * degree) direction="S";
if (angle >= 5 * degree && angle < 6 * degree) direction="SW";
if (angle >= 6 * degree && angle < 7 * degree) direction="W";
if (angle >= 7 * degree && angle < 8 * degree) direction="NW";
Netatmo_Wind_WindDirection.postUpdate(direction)
end
If I try to convert the state into an integer, angle is null every time.
If I use angle=state.toString, the “angle = angle + degree/2” will be a string concadination.