I use a function inside my rule. For this function I provide an object variable (called SV) that can be a Numer or a String. This is used to control my rollershutters. So this var can be “UP”, “DOWN”, 10, 20,…etc.
Generally, this works fine.
But wow to find out inside the function what kind of variable was transfered. Sounds very easy, but I didn´t got it.
rule "SS"
when
System started or
Item rs_AZ changed
then
logInfo("_",rs_AZ.state.toString)
if (rs_AZ.state instanceof PercentType) {logInfo("_","PercentType")}
else if(rs_AZ.state instanceof UpDownType) {logInfo("_","UpDownType")}
end
It’s easyer to see the whole code.
In youreExample it’s always a object.
What is the reason for the instanceof-test?
Is it only for the output, then ever use toString.
Else you have to solve it in a different way.
val Functions.Function1<Object,Boolean> f_test = [ SV |
// logInfo("test","Test Start")
logInfo("test",SV.toString)
if(SV instanceof Object) logInfo("test","Object:")
if(SV instanceof DecimalType) logInfo("test","Number:")
if(SV instanceof StringType) logInfo("test","String:")
var x = SV
if(x instanceof Object) logInfo("test","Object:")
if(x instanceof DecimalType) logInfo("test","Number:")
if(x instanceof StringType) logInfo("test","String:")
// logInfo("test","Instance:"+test) ???
// logInfo("test",SV.toString)
return true]
rule "test"
when
System started
then
f_test.apply(10)
f_test.apply("Test")
end
The problem you had with instanceof is that DecimalType and StringType are OH State Types. You are passing generic Java Types. So what you would want is
But, as Harry indicates, just use String and send “UP”, “DOWN”, “10”, “20”, etc. Unless you have to do math there is no reason to pass anything but a String to your lambda.
Do you need to do math if the value is a number or are you just passing it on?
My script is a little bit more than shown here. I want to distinguish between presets and variable inputs for my rollershutters.
So I need a characteristic to switch between theese both possibilities.
Tried to use that in OH3.2, any idea what import I am missing ?
2022-01-15 09:20:50.304 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testit-1' failed: cannot invoke method public final native java.lang.Class java.lang.Object.getClass() on null in testit
I am looking to use this to determine if a variable contents matches a type definition.
I am searching after a nasty problem with rules DSL and UoM where an item is of Number:Power. Item’s value was set with UoM but it’s losing its UoM somewhere during the persistence process so when I access with typecast like item.state as QuantityType<Power> I’m getting the error below.
Script execution of rule with UID 'Energiemanagement-38' failed: Could not cast 0.0 to org.openhab.core.library.types.QuantityType; line 2481, column 72, length 58 in Energiemanagement javax.script.ScriptException: Could not cast 0.0 to org.openhab.core.library.types.QuantityType; line 2481, column 72, length 58 in Energiemanagement
Yes rules DSL. To fix the exception, the interesting question I’m after is:
would instanceof return true if the variable has UoM and false when it has not ?
ATM I’m struggling in testing to recreate the situation with the item having no UoM, as I said I don’t know exactly when and how that happens.
Or any other idea for a proper test for that condition.
metadata, really ? So if I do not enter any metadata for an item it does not know the default UoM ?
Or do you mean the item type (which strictly speaking isn’t meta) ?
pattern metadata is just used for displaying
No I’ve encountered that multiple times, with multiple items, across restarts even.