- Platform information:
- Hardware: Intel Core i7/16GB/256GB
- OS: Windows 10 (1803)
- Java Runtime Environment: Oracle 8u144
- openHAB version:2.3
- Issue of the topic: The documentation on logging from rules appears to not work
This section
shows this:
logDebug("heating-control.rules", "Bedroom: Temperature: %1$.1f°C, Mode %2$s", Bedroom_Temp.state, Bedroom_Heater_Mode.state)
I tried it and it just printed exactly what I typed and not the actual values from the variable and/or items. I did a lot of searching to find alternative ways of doing and found a few.
Here is what I tried:
var humiditySetPoint = 55.0
rule "tooHumidInBasement"
when
Time cron "0 0/1 * 1/1 * ? *"
then
var Number humidityAverage = BS_DehumidIn_Humidity.averageSince(now.minusMinutes(60),"influxdb")
var Number humidityNow = BS_DehumidIn_Humidity.state
logInfo("basementDehumid.rules", "humidity set point: %1$.1f", humiditySetPoint)
logInfo("basementDehumid.rules", "humidity set point: [{}]", humiditySetPoint)
logInfo("basementDehumid.rules", "BS_DehumidIn_Humidity: Current: [{}]", BS_DehumidIn_Humidity.state)
logInfo("basementDehumid.rules", "BS_DehumidIn_Humidity: Avg: [{}]", humidityAverage)
logInfo("basementDehumid.rules", "BS_DehumidIn_Humidity Again: Avg: " + humidityAverage)
logInfo("basementDehumid.rules", String::format("Set point: %1$.2f%%", humiditySetPoint))
logInfo("basementDehumid.rules", String::format("Current: %1$.2f%%", humidityNow.floatValue()))
logInfo("basementDehumid.rules", String::format("Avg: %1$.2f%%", humidityAverage.floatValue()))
if(humidityAverage > humiditySetPoint) {
BS_DehumidPower_Power.send(ON)
}
else {
if(humidityAverage < (humiditySetPoint - 5)) {
BS_DehumidPower_Power.send(OFF)
}
}
end
and here are the results I got:
2018-09-09 16:24:46.246 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'basementDehumid.rules'
2018-09-09 16:25:00.188 [INFO ] [e.model.script.basementDehumid.rules] - humidity set point: %1$.1f
2018-09-09 16:25:00.189 [INFO ] [e.model.script.basementDehumid.rules] - humidity set point: [55.0]
2018-09-09 16:25:00.189 [INFO ] [e.model.script.basementDehumid.rules] - BS_DehumidIn_Humidity: Current: [55.9]
2018-09-09 16:25:00.190 [INFO ] [e.model.script.basementDehumid.rules] - BS_DehumidIn_Humidity: Avg: [50.56245053634353]
2018-09-09 16:25:00.191 [INFO ] [e.model.script.basementDehumid.rules] - BS_DehumidIn_Humidity Again: Avg: 50.56245053634353
2018-09-09 16:25:00.192 [INFO ] [e.model.script.basementDehumid.rules] - Set point: 55.00%
2018-09-09 16:25:00.193 [INFO ] [e.model.script.basementDehumid.rules] - Current: 55.90%
2018-09-09 16:25:00.193 [INFO ] [e.model.script.basementDehumid.rules] - Avg: 50.56%
As you can see the way documented in the rules section did not work at all. Two of the methods (string concatenation and squiggly braces worked but don’t allow setting precision). String::format allows setting the format, but is VERY picky about the type and the rules it uses make no sense to me (even after reading the documentation that I could find on them). I found that defining a local Number variable to hold the value allowed me to have a consistent method of getting the value I wanted.
So, does anybody use logging in rules? If so, how do you log the values of interesting things in rules. I have a way that works for me and is not horrible, so I am reasonably happy. Hopefully this provides the next person with things to try to get logging in rules to work.