Hi, have a problem
I want to control the volume of my computer (windows 10)
To do this (supposed) to use simple command-line utility NirCmd http://www.nirsoft.net/utils/nircmd.html
If use the switch for mute - its simple:
{ exec=">[1:f:/openhab/nircmd mutesysvolume 1] >[2:f:/openhab/nircmd mutesysvolume 0]" }
But I need to send the value of the dimmer in the command line.
Values from the dimmer 0-100
Values for NirCmd 0-65535
Unfortunately, I do not know much Java … Does anyone have an idea?
Thank you in advance
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