Help with rule, Volume percent to key-presses

Hi,

Would love to have some input regarding a rule for converting percentage to key-presses.
An example would be voice command control of soundsystem connected over global cache IR. Where the soundsystem have 10 key-presses from minimum volume to maximum volume. Then volume 50% would be 5 keypresses from minimum. How would this be coded in a rule?

Best regards
Christian

Perhaps there are more then one Solutions … but you have to give more Informations about the Soundsystem.

Is it possible to get the actual State of the Volume via a Post request ?

If this ist possible, I can give you a easy Solution…

If this not Possible … the Solutions is not so good … to get an correct State in Openhab… send 10 Keypresses Down and than then 5 Keypresses Up to get 50%… if you do not 10 Keypresses Down … you have no State to calculate the 50%… I hope you understand my bad English :wink:

To give you an Idea here an Example: (Theres no Feedback from the Device)

rule "Percent to Number"
when
    Item dimmer_item changed
then
    if(dimmer_item.state >= 0 && dimmer_item.state <= 10) keypresses.sendCommand(1)
    if(dimmer_item.state >= 11 && dimmer_item.state <= 20) keypresses.sendCommand(2)
    if(dimmer_item.state >= 21 && dimmer_item.state <= 30) keypresses.sendCommand(3)
    if(dimmer_item.state >= 31 && dimmer_item.state <= 40) keypresses.sendCommand(4)
    if(dimmer_item.state >= 41 && dimmer_item.state <= 50) keypresses.sendCommand(5)
    if(dimmer_item.state >= 51 && dimmer_item.state <= 60) keypresses.sendCommand(6)
    if(dimmer_item.state >= 61 && dimmer_item.state <= 70) keypresses.sendCommand(7)
    if(dimmer_item.state >= 71 && dimmer_item.state <= 80) keypresses.sendCommand(8)
    if(dimmer_item.state >= 81 && dimmer_item.state <= 90) keypresses.sendCommand(9)
    if(dimmer_item.state >= 91 && dimmer_item.state <= 100) keypresses.sendCommand(10)
end

rule "Send the IR"
when
    Item keypresses changed
then
  if (keypresses.state > 0) {
    //command for 10 VolumeDown Presses    
    var i = 0
    while((i=i+1) <= keypresses.state) {
        //command for the IR Keypress
    }
  keypresses.sendCommand(0)
  }    
end

I hope I understand your Question and this helps you :slight_smile:

Thank you very much for your contribution! :slight_smile: I really appreciate your help

Just some information regarding my setup:
I use Global Cache itach unit to convert ip commands to one-way IR output signals, to control my ir devices (TV, arvani soundbar, and set-top box) I use a globalcache.map file to store all the IR codes for my devices.
globalcache.items:

/* ADB Decoder */
String ADB	"ADB"	(FF_Living, Multimedia) { channel="globalcache:itachIR:000C1E026137:ir-m1#c1" }

/* Soundbar */
String Arvani	"Arvani"	(FF_Living, Multimedia)	{ channel="globalcache:itachIR:000C1E026137:ir-m1#c3" }

/* TV */
String Thomson	"TV"	(FF_Living, Multimedia) { channel="globalcache:itachIR:000C1E026137:ir-m1#c3" }

globalcahce.map:

Arvani_VolUp			= 38109,1,1,341,171,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,21,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,21,22,21,22,21,22,21,22,21,22,63,22,21,22,21,22,63,22,63,22,63,22,63,22,63,22,21,22,63,22,1518,341,85,22,3810
Arvani_VolDown			= 38109,1,1,341,170,22,20,22,20,22,21,22,20,22,21,22,20,22,20,22,20,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,63,22,21,22,63,22,21,22,20,22,20,22,63,22,20,22,21,22,63,22,20,22,63,22,63,22,63,22,21,22,63,22,1520,341,85,22,3810

So when I’m sending commands to global cache devices, using a rule, it will look something like this:

sendCommand(Arvani, "Arvani_VolUp")

I can’t see that this can be done using your above rule? :S
I’ll have to make a rule that sends exactly this “sendCommand(Arvani, “Arvani_VolUp”)” command X times, based on the dimmer value.

Best regards
Chris

Yes thats no Problem … you can do this with litte changes in my Solution … but the Problem is you have no State from the Device … if you cange the Volume by Device Remote, Openhab doesn’t know that. And your Rule do wrong things next time.

That’s correct. I’ve given that some thoughts before I decided to try making this rule. I will eventually add a IR-receiver that pics up my remote control signals, and update the states in OH2. But untill then It would be great to try the rule as is :slight_smile:

That would be a rule to always have the correct status, even if the device was changed. The disadvantage is that the volume always goes to 0 and then adjusts to the desired volume.

rule "Percent to Number"
when
    Item dimmer_item changed
then
    if(dimmer_item.state >= 0 && dimmer_item.state <= 10) keypresses.sendCommand(1)
    if(dimmer_item.state >= 11 && dimmer_item.state <= 20) keypresses.sendCommand(2)
    if(dimmer_item.state >= 21 && dimmer_item.state <= 30) keypresses.sendCommand(3)
    if(dimmer_item.state >= 31 && dimmer_item.state <= 40) keypresses.sendCommand(4)
    if(dimmer_item.state >= 41 && dimmer_item.state <= 50) keypresses.sendCommand(5)
    if(dimmer_item.state >= 51 && dimmer_item.state <= 60) keypresses.sendCommand(6)
    if(dimmer_item.state >= 61 && dimmer_item.state <= 70) keypresses.sendCommand(7)
    if(dimmer_item.state >= 71 && dimmer_item.state <= 80) keypresses.sendCommand(8)
    if(dimmer_item.state >= 81 && dimmer_item.state <= 90) keypresses.sendCommand(9)
    if(dimmer_item.state >= 91 && dimmer_item.state <= 100) keypresses.sendCommand(10)
end

rule "Send the IR"
when
    Item keypresses changed
then
  if (keypresses.state > 0) {
    var down = 10
    while((down=down-1) >= 0) {
        sendCommand(Arvani, "Arvani_VolDown")
    }  
    var up = 0
    while((up=up+1) <= keypresses.state) {
        sendCommand(Arvani, "Arvani_VolUp")
    }
  keypresses.sendCommand(0)
  }    
end

WOW, this is great!! Thank you so much!!

When I compose this rule with Smarthome designer, I get the following error:

Type mismatch: Cannot convert from State to byte 
while((up=up+1) <= keypresses.state) {
2018-01-06 16:14:38.546 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '4' to a command type which item 'keypresses' accepts: [OnOffType, RefreshType].

Figured it out, Just added “as Number” behind keypresses.state :wink: