Windows: Rules and exec

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? :confused:
Thank you in advance

Something like that:

rule “Volume control”
when
Item VOLUME received command
then
// int LASTVOLUME = 0
// int NEWVOLUME = receivedCommand * 655
if(receivedCommand==0){
executeCommandLine(“f:/openhab/nircmd.exe mutesysvolume 1”)
}
else{
executeCommandLine(“f:/openhab/nircmd.exe mutesysvolume 0”)
// execute nircmd.exe with difference between NEWVOLUME-LASTVOLUME
//
// for example:
// NEWVOLUME 12800, LASTVOLUME 25600
// value to send: nircmd.exe changesysvolume -12800

}
end

Something like:

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

Thanks for the quick response!
But it is not working …
https://yadi.sk/i/9CJi5I9pmknno :dizzy_face:

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
1 Like

Thx!
But:

Maybe need a variable to hold the result newVolume - lastVolume?
And save value like lastVolume = newVolume ?
I know, I dummies…

Many thanks!

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.

Thank you so much! You really helped!

final version:

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
1 Like