i need some help with this rule, see below.
The rule triggers on pushing the button on the soft remote to control the volume of the Echo.
If I increase the volume by using
Echo_Living_Room_Volume.sendCommand(‘INCREASE’)
It raises the volume by 1, makes you hit the button about 4 or 5 times to noticeably adjust the volume.
My idea was to increase the value in steps of 5, just like In the setpoint in the sitemap (it works there).
But I get an error in the log running the rule below. Where is my mistake?
rule "Remote (2.0)"
when
Item nodon_remote_scene received update 2.0
then
logInfo("Remote", "Remote TASTE + ; VolumeState: " + Echo_Living_Room_Volume.state)
var vol = Echo_Living_Room_Volume.state
logInfo("Remote", "var Vol: " + vol)
vol = vol + 5
Echo_Living_Room_Volume.sendCommand(vol)
Log: 2020-07-19 09:25:41.305 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Remote (2.0)': An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null
Yep, that is, because a state is a state and not a string. Furthermore vol has to be of type Number, but you are using it as a string.
rule "Remote (2.0)"
when
Item nodon_remote_scene received update 2.0
then
logInfo("Remote", "Remote TASTE + ; VolumeState: " + Echo_Living_Room_Volume.state.toString)
var Number vol = Echo_Living_Room_Volume.state as Number
logInfo("Remote", "var Vol: " + vol.toString)
vol = vol + 5
Echo_Living_Room_Volume.sendCommand(vol)
end
For me it is difficult to understand that Echo_Living_Room_Volume.state, which shows in the logInfo as for example “25”, seems not to be a “Number” nor a “String”.
Echo_Living_Room_Volume is an Item. .state gives the state of the Item. This can be interpreted as a string or a number, but it depends on the command you are using. logInfo(string,string) needs explicitly strings as arguments. logInfo() is not able to guess the correct way to transform the parameter to string, so you need to transform yourself.
Luckily, .toString is part of every object in openHAB. (please be aware that primitives aren‘t objects, so
var int iInteger = 5
logInfo(”message”,”iInteger =” + iInteger.toString)
will FAIL, because there is no such function as .toString. But
var Integer iInteger = 5
logInfo(”message”,”iInteger =” + iInteger.toString)
will not, because iInteger now is an object.
Of course, you can use
logInfo(”message”,”iInteger = {}”, iInteger)
instead, this will work for both int and Integer, as now logInfo() uses parameterized strings (don’t know the correct word in English…)