[SOLVED] Exec- releasing input variable - android keyevent

Hello Hive Mind,

I am trying to use the exec binding with android adb key events. I am able to send the initial key event, but all subsequent commands do not function. An example:

I send the key 66 for enter - android tv pauses
I send the key 66 again - no response

I send another key, then send 66 - android tv responds to 66 after responding to other random key.

Basically, I would like to be able to add a button to habpanel to play/pause the android tv. I know the commands work without delay and without sending a random key because I can send multiple key commands (key 66 for example) without issue bu directly typing them into the command line of the openhab2 host. The main issue seems to be openhab keeping the original variable and not re-sending it (i.e. not changing state.) I have attached a photo of the logs from openhab as well as the thing file. If you look at the bottom half you can see what i am talking about. It will grab “66”, go from off to on, then will just keep saying “predicted to be” until another key is sent (key “20” in the log.)

If this makes no sense let me know and i will provide any information i can to help. Thanks in advance.

Can you post the thing, item and log. Very difficult to see on a screen shot

Log:
02:45:30.712 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:33.173 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:36.210 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:38.018 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 20
02:45:40.440 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:40.749 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:41.035 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:41.310 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:41.575 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:41.581 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command
02:45:41.834 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:42.107 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:42.369 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:43.742 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:44.460 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:45.020 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:45.441 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:45.705 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:45.987 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:46.236 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:46.469 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:46.722 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:47.006 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:47.243 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:47.515 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:47.772 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:48.044 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:48.327 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:48.611 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66
02:45:50.672 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 20
02:45:52.761 [ome.event.ItemCommandEvent] - Item ‘ADB_KEY_Args’ received command 66

If you have a look at the log, command “66” is sent to the Android TV correctly. Any subsequent “66” command is not sent. The blank received command is part of the rule I was attempting to reset the variable (i will include below) but it does not work. Reading down log you will see command “20”. This is basically sending another key press and works correctly. After sending the “20” command, “66” will function correctly once, then the log just repeats. It is not just the “66” command btw. You can swap the two, or use different commands and the same result happens. It will not resend the command until a different command is sent.

Thing:

//Android ADB KeyEvent
//Sends android ADB a keyevent number

Thing exec:command:ADB_KEY [ command="/home/openhab/platform-tools/adb shell input keyevent %2$s", interval=0, autorun=true, timeout=01, autoUpdate=false ]

This is the Thing I have been using. The timeout and autoUpdate were added as an attempt to get things working correctly. Removing/adding them has no impact.

Items:

Switch ADBKey
Number ADBKeyNumber

//is running or finished
Switch ADB_KEY {channel="exec:command:ADB_KEY:run"[profile="rawbutton-toggle-switch"]}
//Arguments to be passed to '%2$s' in command
String ADB_KEY_Args {channel="exec:command:ADB_KEY:input", autoupdate="false"}

This is the item. There was an exit string but it did nothing to help or hurt with or without it. The autoUpdate=false was added to stop the “predicted to become” log. It has no impact with or without. The rawbutton-toggle-switch was added to troubleshoot as well. If you take the default exec item binding and use that the end result is the same.

Rule:

 var Number i = 0
rule "ADBKeyEventRule"
  when
     Item ADB_KEY_Args received command
  then
    while ((i=i+1) == 10)
    {
      ADB_KEY_Args.sendCommand("")
    }
end

This is the closest I have been able to get with a rule to achieve the desired function. It results in the log above. If you were to use the example rule for the exec binding, the log and effect are the same, except the log will show an item state change. Interesting tidbit, if you were to remove the “var Number i = 0” and remove the “while ((i=i+1) ==10)” bits, the end result is correct. The commands are issued as soon as they are given. However, the log runs continuously, as in, non-stop with many updates a second. It will show the “received command 66” line then run away with “received command (space)” until another command is sent. I hope this information is a little more helpful, but if not just let me know what I can do to help. Thanks again.

Please use code fences. It is very difficult to read. I actually stopped at the first paragraph

My mistake. I did not realize that was an option. Edited to include code blocks for clarity.

OK thanks.

 var Number i = 0
rule "ADBKeyEventRule"
  when
     Item ADB_KEY_Args received command
  then
    while ((i=i+1) == 10)
    {
      ADB_KEY_Args.sendCommand("")
    }
end

I honestly dont understand what you are trying to do with this rule. You have received command rule that sends command to the same item. You have an iterator i, that gets only initialized to zero once (when the script is loaded) and never gets zeroed again.

Try adding i=0 right before the end

      ...
      ADB_KEY_Args.sendCommand("")
    }
    i=0
end

Thanks for the input. I just gave it a go and i get the same result. Basically what I am trying to do is control an AndroidTV from the dash board without writing the 200 something items (one for each button.) So, ideally, I would able to do something like below:

  1. Watching Netflix or some media on the AndroidTV with lights off for a movie.
  2. I need to get up to do something.
  3. I press a button on the dashboard and the following happens:
    A. The AndroidTV receives the “select” command (adb input keyevent 66)
    B. The media is paused and the lights come to a value, say 25%
  4. I come back from doing a thing and press the same button on the dashboard and the following should happen.
    A. The lights dim back to 0%
    B. The AndroidTV the receives the select command again (adb input keyevent 66) and media resumes

The main sticking point is the keyevent value is not sent the second time. I need to issue a different adb keyevent command, then reissue the keyevent i want to use for it to function correctly. This does not work because sometime reissuing the same command multipul times in succession is nessary. Think selecting the Netflix (keyevent 66), selecting the current show (keyevent 66), and then selecting resume in the app (keyevent 66). So in this case the same keyevent needs to be sent 3 times in a row. Play/pause would be issuing the same command twice in a row. The only way it will currently function is by doing something like select (66), down (20), select (66), down (20), select (66). This does not work for day to day use or for selecting something with a pop up menu when the down key is pressed on the AndroidTV remote.

If i am going about this the wrong please let me know. I have been at it for days now and nothing i do, except for letting the log run away, will allow it to function as intended. As far as the rule above goes, it is just the closest I have been to it functioning correctly.

As a side note this is not something to do with the AndroidTV. I can issue the same keyevent from the adb command as many times as i need without issue.

Second time from where… from a rule? From HabPanel? The confusing part is your rule. You are sending a command when you receive a command, it’s goes into a loop. Try a proxy item, a number.

...
when
  Item proxyItem received command
then
  ADB_KEY_Args.sendCommand(...)
  

For habPanel, I don’t know of any issue/problem issuing same sendCmd values.

The dpad style widget is what i am using. When i say send it twice i mean clicking the center select button twice in a row, or any of the key more than once. Am i using this wrong, do i even need the rule? I am using this because the alternative is a bunch of square buttons to issue one key command each, which doesn’t look from a UI design, and is not all that good from a functional perspective.

Remove your rule for now. Hit the button twice. You should see received command in the log twice.

Rule removed and openhab restarted. I do see the “ADB_KEY_Args received command 66” for each time i click the select button, but it is the same result. It will only issue the command once, until a different button is clicked. I am starting to think this is a failed endeavor and am going to just have to use a different layout with square buttons :confused:

Correct. Now set autorun as false. Auto run true will only run the exec binding IF the value has changed. Set it not to autorun.


Thing exec:command:ADB_KEY [ command="/home/openhab/platform-tools/adb shell input keyevent %2$s", interval=0, autorun=false, timeout=01, autoUpdate=false ]

Now on your rule, do this

when
  Item ADB_KEY_ARGS received command
then
   ADB_KEY.sendCommand(ON)
end

Now your exec thing has to be autorun false. The above rule will run the exec regardless of whether or not the input (ADB_KEY_ARGS) have changed. But we will manually run it when ARGS received any command.

PS: This has nothing to do with habPanel

Holy crap it works! :+1:

I had to remove a few extra parameters from the item and thing file i had added, but after that it works like a charm. I guess that is what i get for blindly following an example without understanding it. The only thing i have left is to map common words to the keyevent numbers so I do not have to keep up with a list of numbers, but that will be another post if needed. Thanks a million and i owe you a huge thanks!

You can tick the post as the answer :slight_smile:

For mapping just use a .map file transformation.

Hi,
Can you share your working code?
i’m trying to do something similar with firetv.
Thank you