rule "Volume control"
when
Item VOLUME received command
then
if (receivedCommand == 0) {
executeCommandLine("f:/openhab/nircmd.exe mutesysvolume 1")
}
else {
val int lastVolume = if (VOLUME.state instanceof DecimalType) then (VOLUME.state as DecimalType).intValue * 655 else 65535 // assume last volume was on full loud if we don't know it
val int newVolume = (receivedCommand as DecimalType).intValue * 655
executeCommandLine(String::format("f:/openhab/nircmd.exe changesysvolume %d", newVolume - lastVolume))
// unmute *after* setting new volume?
executeCommandLine("f:/openhab/nircmd.exe mutesysvolume 0")
}
end
That is one very long error message! The word “then” shouldn’t have been there. Here is a clearer version:
rule "Volume control"
when
Item VOLUME received command
then
if (receivedCommand == 0) {
executeCommandLine("f:/openhab/nircmd.exe mutesysvolume 1")
}
else {
var int lastVolume = 65535 // assume last volume was on full loud if we don't know it
if (VOLUME.state instanceof DecimalType) {
lastVolume = (VOLUME.state as DecimalType).intValue * 655
}
val int newVolume = (receivedCommand as DecimalType).intValue * 655
executeCommandLine(String::format("f:/openhab/nircmd.exe changesysvolume %d", newVolume - lastVolume))
// unmute *after* setting new volume?
executeCommandLine("f:/openhab/nircmd.exe mutesysvolume 0")
}
end
You’re no dummy! This is difficult terrain to cross. I suspect the problem is that the state of the VOLUME item has already been auto-updated by the time the rule runs, so VOLUME.state == receivedCommand. One possible fix is to add , autoupdate="false" to the Item VOLUME ... binding section, so the auto-update doesn’t happen. This means that your VOLUME item will continue to reflect the old volume after you change it in the UI, until some other mechanism updates the item’s state.
var int lastVolume = 0
rule "Volume control"
when
Item VOLUME received command
then
val int newVolume = (receivedCommand as DecimalType).intValue * 655
executeCommandLine(String::format("f:/openhab/nircmd.exe changesysvolume %d", newVolume - lastVolume ))
lastVolume = newVolume
end