I have been setting up a DSL rule as such:
rule "Curtain_down"
when
Item weather_illumination received update
then
logInfo("valueInfo", "" + weather_illumination.state)
logInfo("typeInfo", "" + weather_illumination.state.getClass().getTypeName()) //Quantity type
logInfo("typeInfo", "" + weather_illumination.getClass().getTypeName()) //Number type
end
16:46:28.066 [INFO ] [rg.openhab.core.model.script.typeInfo] - org.openhab.core.library.types.QuantityType
16:46:28.068 [INFO ] [rg.openhab.core.model.script.typeInfo] - org.openhab.core.library.items.NumberItem
17:04:14.447 [INFO ] [g.openhab.core.model.script.valueInfo] - 7846 lx //QuantityType
How can I extract the 7846 value as a primitive (int) type?
i have already read OH javadoc but no way. Thanks
Platform information:
Dockerized openhab 4.2.1 with FineOffset binding
First things first would be better to mark code as code (the </> button). This way one can simply copy the code (the forum software will create extra buttons for that purpose).
Another thing is, there are changes in the text (e.g. “”
instead of ""
, which will prevent the code from being executed at all)
Given the fact, that the item weather_illumination
is of type Number:Illuminance you can use something like this:
rule "Curtain down"
when
Item weather_illumination changed
then
logInfo("curtainDown", "State is {} and as Integer it's {}", newState, (newState as Number).intValue)
end
You should avoid primitives if not really needed, as primitives are way more time consuming than objects (afaik factor >> 100).
I followed your suggestions I reworked the rule as:
rule "Curtain_down"
when
Item weather_illumination changed
then
var weather_illumination_value = (weather_illumination.state as Number).intValue
logInfo("value {}",weather_illumination_value)
end
I got this log message:" Script execution of rule with UID ‘curtain_down-1’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in curtain_down"
Any tip? Thanks
Please be aware that logInfo()
needs two strings as parameters, where the first string is the name of the logger and the second string is the message to log.
logInfo("loggername","my message")
The log commands support substitution, so every {} is substituted by the next value of the comma separated list:
logInfo("loggername","my message value 1: {} value 2: {}",myVal1,myVal2)
There are four log commands, logDebug()
, logInfo()
, logWarn()
and logError()
, and you can set the log level at runtime from Karaf console:
log:set DEBUG org.openhab.model.script.myLogger
will set the log level to DEBUG, so all log commands with the name myLogger
will be executed.
log:set ERROR org.openhab.model.script.myLogger
will only execute the logError("myLogger","...")
commands but omit all other log commands for myLogger
.
At last I found a solution as below:
rule "Curtainsdown"
when
Item weather_rain_event changed or
Item weather_speed_wind changed or
Item weather_illumination changed
then
var weather_rain_event_value = (weather_rain_event.state as Number).doubleValue
var weather_speed_wind_value = (weather_speed_wind.state as Number).doubleValue
var weather_illumination_value = (weather_illumination.state as Number).intValue
if (weather_rain_event_value == 0.0 || weather_speed_wind_value < 1.5 || weather_illumination_value > 70000) {
iTH_shutter.sendCommand(100)
iTG_shutter.sendCommand(100)
}
end
Everything is going smootly.
Thank you so much for your help!
Hi I made a mistake in the condition, obviously the correct one is:
rule "Curtainsdown"
when
Item weather_rain_event changed or
Item weather_speed_wind changed or
Item weather_illumination changed
then
var weather_rain_event_value = (weather_rain_event.state as Number).doubleValue
var weather_speed_wind_value = (weather_speed_wind.state as Number).doubleValue
var weather_illumination_value = (weather_illumination.state as Number).intValue
if (weather_rain_event_value == 0.0 && weather_speed_wind_value < 1.5 && weather_illumination_value > 70000) {
iTH_shutter.sendCommand(100)
iTG_shutter.sendCommand(100)
}
end
and
rule "Curtainsup"
when
Item weather_rain_event changed or
Item weather_speed_wind changed or
Item weather_illumination changed
then
var weather_rain_event_value = (weather_rain_event.state as Number).doubleValue
var weather_speed_wind_value = (weather_speed_wind.state as Number).doubleValue
var weather_illumination_value = (weather_illumination.state as Number).intValue
if (weather_rain_event_value > 0.0 || weather_speed_wind_value > 3.0 || weather_illumination_value < 6000) {
iTH_shutter.sendCommand(0)
iTG_shutter.sendCommand(0)
}
end
As both rules are triggered by the same Items, one rule will suffice:
rule "automatic Curtains"
when
Item weather_rain_event changed or
Item weather_speed_wind changed or
Item weather_illumination changed
then
val dRain = (weather_rain_event.state as Number).doubleValue
val dSpeed = (weather_speed_wind.state as Number).doubleValue
val iIllumination = (weather_illumination.state as Number).intValue
var iTarget = -1
if (dRain == 0 && dSpeed < 1.5 && iIllumination > 70000) iTarget = 100
if (dRain > 0 || dSpeed > 3.0 || iIllumination < 6000) iTarget = 0
if(iTarget > -1) {
if(iTH_shutter.state != iTarget)
iTH_shutter.sendCommand(iTarget)
if(iTG_shutter.state != iTarget)
iTG_shutter.sendCommand(iTarget)
}
end
The rule will also omit unnecessary sendCommands.
Well done!
I will test the rule and give you a feedback! You know, today it’s cloudy in north-east Italy so the illumination level is too low for curtains to open and verify!
Thanks
I verified the rule and it works as expected!
Thanks again for your help, so long!
1 Like