Exec binding %2$s format exception

I am working on setting up openhab2 on Ubuntu server 16.04 with the Zulu JVM. I am attempting to use the exec addon binding to run a python script. However I am having trouble getting the state of my Color item type into the command line arguments. Viewing the openhab.log file I find the following error,

2017-08-29 23:50:27.540 [ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while formatting the command line with the current time and input values : 'Format specifier '%2$s''

My things file is,

Thing exec:command:sunrise "Sunrise" @ "Bedroom" [command="/usr/bin/python /home/cameron/test.py %2$s", interval=1, autorun=true]

My Items file is,

Color sunrise "Sunrise" <light> (bedroom) {channel="exec:command:sunrise:input"}
String output "Output" (bedroom) {channel="exec:command:sunrise:output"}

Finally if it is of interest my Python file is,

import sys
print sys.argv

Any help troubleshooting this would be much appreciated.

Cameron

Which version of OH are you on? 2.1 or 2.2 SNAPSHOT. If the SNAPSHOT, which build number?

Someone else reported this problem and I couldn’t help him get it to work. I think an issue needs to be filed.

In the mean time you can use a Rule with a cron trigger and executeCommandLine.

At the bottom of my openhab start page it says, openHAB 2.1.0 - release build -.

Looking at the Exec binding code, namely this file,

There is a line which exactly matches the error received. It is from the following block of code,

try {
   if (lastInput != null) {
      commandLine = String.format(commandLine, Calendar.getInstance().getTime(), lastInput);
   } else {
      commandLine = String.format(commandLine, Calendar.getInstance().getTime());
   }
} catch (IllegalFormatException e) {
   logger.error(
      "An exception occurred while formatting the command line with the current time and input values : '{}'",
      e.getMessage());
   updateState(RUN, OnOffType.OFF);
   return;
}

From this I am wondering lastInput starts out as null so the if condition is not met. Therefore commandLine = String.format(commandLine, Calendar.getInstance().getTime()); is run, and %2$s clearly does not exist, as only one argument is given to format(). I wonder if this would work better if the if condition was not used and allow null to be passed to the external program.

I was having the same issue with that binding as well. I gave up and did a half baked rule to trigger my python script.

Are you certain that the InputItem (i.e. Sunrise) has a state and isn’t NULL?

If so does it start to work when you give Sunrise a real state or do you continue to get the error?

I wonder if the binding was written with the use case in mind where one would have both a refresh interval and using %2$s.

This is a problem and an issue should definitely be filed on the openhab2-addons repo I think.

I believe it should have a HSBType state per, http://docs.openhab.org/concepts/items.html#hsbtype.

How would I go about giving Sunrise a real state?

I’ll ask back how did plan on using Sunrise as the argument to your script in the first place? That is what the input channel is for.

You can put it on your sitemap with a Color element and choose a Color.

Create a rule and postUpdate an HSBType to Sunrise.

But given the whole purpose of input channel is to pass arguments to your script I assumed you already figured out how you want to populate the Item linked to the input channel.

I currently have it set up in a .items file shown in my original post. This appears in the PaperUI. It is set up with autorun=true so whenever the Color item is changed the script is run. I can tell that this should be working because when I change the color I get the same error in the openhab.log file again.

You have autorun=true and interval=1 in your Thing which means that the script is executed every second in addition to being run every time the Color Item is updated.

If you are changing the Color Item then that Item isn’t NULL.

That is strange because I am not getting a log entry once a second that the command is run.

I have found and fixed the issue and filed a pull request located here, https://github.com/openhab/openhab2-addons/pull/2671

1 Like