Exec binding and echo, netcat, a pipe, carriage return and quotation marks

I’m trying to control my Sharp TV over IP. From the command line of the RPi that is also running OpenHAB, I’m able to turn it on sending:
echo -e "POWR1 \x0d" | nc ipaddressofTV portofTV
(and turn it off replacing the 1 in POWR1 with a 0), but a large number of different attempts at inserting this into an exec item binding haven’t worked (for example:

Switch AV_TV_IP_Power "TV Power" (gMR_MC_IP_TV) { exec=">[ON:/bin/echo -e \"POWR1   \r\" | /bin/nc ipaddressofTV portofTV] >[OFF:/bin/echo -e \"POWR0   \r\" | /bin/nc ipaddressofTV portofTV]" }

is not recognized). In my research to fix I’ve since discovered the tcp binding, which I imagine might be more appropriate, but before I figure that out I’m genuinely curious about a number of questions that have arisen from this not working.

Extra info:

  • I’ve gotten several exec items to work, all focused on sending IR commands through LIRC (irsend) over IR Emitters wired to a GPIO pin.

  • the command line works with single or double quotes around the echo, and I’ve escaped both out (’ and ") in the exec item configuration without success.

  • I put the item into the designer and, though the \x0d gave an invalid character signal, I’ve also tried \x0d (escaping out the ), \r and \u000d and none of those seem to work either (and didn’t give an error in Designer).

So my questions are:

  1. I’ve seen the links here and the wiki, that say more complicated commands require @@ instead of spaces, but clearly there’s more characters than just spaces that need delimiters (for instance it looks like -h is Header?), in any event, even if this isn’t the answer to my question, this seems like an important concept to grasp. Can anyone point me to more info about this? When exactly is it needed? Is there a table that shows which characters should be replaced with what?

  2. Why ISN’T this particular command/item being recognized by OpenHAB? is my running theory about it probably needing the offending characters escaped out correct? If so, what SHOULD I replace the quotes and carriage return’s with? Are there any other offending characters?

  3. What would be the best way for me to debug this, and future errors with item configuration? I find a line in the openhab.log that it “can’t retrieve the item”, and the Events.log shows me pressing the Switch in the WebUI, but it’d be really helpful to see what error OpenHAB is encountering when it tried to “retrieve” this item from the .items file. This is obviously a more open-ended question (to avoid bugging the experts here as much as possible moving forward), I added a specific Insteonplm log through logback.xml according to another helpful user’s post, and I’m sure the same is warranted here…

  4. This is maybe a slight aside, but I know there’s a debug start script, but I run openhab as a different user, that I defined in my systemd config. And running that debug start script worked but seemed to really screw up my permissions and I wasn’t able to restart the regular openhab instance, is there somewhere inside OpenHAB native configs that I can set what user it runs as?

edit: changed some forward slash’s to back slash’s to accurately reflect what I’ve tried.

1 Like

I can’t answer all your questions but here are a few things to note.

Don’t use the Exec binding while trying to figure out executing things on the command line. Instead create a “virtual switch” and a rule that executes when you flip that switch. In the rule use:

val results = executeCommandLine("echo -e \"POWR1 \\x0d\" | nc ipaddressofTV portofTV", 5000)
logInfo("Exec", results)

Then watch the logs for what your command returns. If you have a permission error or some other error that is the only way you will be able to see it.

What user OH runs as is defined outside of OH. However, I know that at least with an apt-get install, everything is configured to run as the openhab user and there is a script that executes at startup which resets the permissions on pretty much everything openhab owns or needs. I don’t know if the debug script does that too but ultimately my advice is if you are running on Linux to use the apt-get installation method unless you are really really good at debuging these sorts of problems.

haha yeah i was all over the map with that one. But I have so many questions I can’t find answers to!!! Thanks for sifting through it:)

YES!! I saw you post that in another thread but I didn’t quite grasp the concept, but I think I get it now. I have yet to create a rule, so let me double check that I got the process?

  • Create Virtual Switch in *.items (e.g. “Switch exectest”)

  • Create rule (replace Exec with exectest in the example above)?

  • put the switch in .sitemap (e.g. “Switch exectest”)

  • monitor openhab.log with tail -f while I flip with switch in the gui?

Do I add anything else to the items or sitemap configurations?

Yeah I actually tried both ways to start and ultimately decided on the manual install. But I am running into a lot of these issues and am starting to wish I’d stuck with the apt-get install and keeping the openhab user. I’m strongly considering switching it back again, i think i have a handle on what files I’d need to backup, think it would be worth it going forward?

I think you have the right steps. And yes, i tgink it is totslly worth going back to apt-get.

Thanks for the quick reply, but I seem to still be missing something. Doing the above test and tailing/grepping the log for exectest results in the same thing as the bunk IP command, just that cannot retrieve the item and that it isn’t in the registry. Is there a different log I should be looking at? This is in the run.log in the home folder and ~/logs/openhab.log (which seem like pretty much the same thing to me).

I guess I’m not clear on what links the Rule with the Sitemap Item? What tells the Switch to fire that “executeCommandLine(…” when it’s toggled? is naming the Switch in *.sitemap the same as the name of the .rules file adequate? Is there anything more required other than the 2 lines you pasted for the rule? I added rule "exectest" for safe measure, but still not working…

edit: after reading the Rules wiki entry I added:
val results = executeCommandLine(“echo -e “POWR1 \x0d” | nc ipaddressoftv portoftv”, 5000)
rule "exectest"
when
Item exectest received update
then
logInfo(“exectest”, results)
end

Still doesn’t show anything but that the item isn’t registered in the log…

#Items:

Switch ExecCommand "label" <icon>

Where “label” is some reasonable text to put on your sitemap and <icon> is a reasonable icon name. Either or both can be omitted.

#Rule:

rule "exectest"
when
    Item ExecCommand received command
then
    val results = executeCommandLine("echo -e \"POWR1 \x0d\" | nc ipaddressoftv portoftv", 5000)
    logInfo("ExecTest", results)
end

#Sitemap

Switch item=ExecCommand

How did you install OH? If you are on Raspbian it is far far better to us apt-get to install OH then to do a manual install. See the wiki for how to install via apt-get. With an apt-get install the logs are in /var/log/openhab and the log you want to follow is openhab.log.

To trigger the command to execute you need to toggle the switch on your sitemap which will cause your rule to execute.

So as we discussed prior, I ended up with a manual install. THAT you convinced me to swap out for the apt-get install (that I just haven’t done yet, but thank you, I will follow your advice). However, I have a decent grasp on the location of files, currently I have a “run.log” in /opt/openhab/ and a openhab.log in /opt/openhab/logs/. These two logs appear to be the same. There is also an Events.log in /opt/openhab/logs, and an insteon log I defined in logback.xml. I’ve been checking the openhab.log and that’s where I’m only seeing that same “item not registered” errors.

I thought i had already set the logs to the “debug” level, but I guess I should double check this…you’re saying that I should see errors for why the rule didn’t work as I Toggle the switch (when currently it just keeps telling me the item is not defined in the registry (or something along those lines)).

I think i have everything the way you detail in your explanation (thanks for taking the time), however I have one question:

Is it the “label” that I put in the sitemap? or the word after Switch (in this case ExecCommand), which I believe is the ItemName? I’m pretty sure its the latter and that appears to be what you used in your Sitemap example, but thought I’d check because it seems everything else is the same.

One clarification, what does the 5000 parameter refer to in the executeCommandLine(…, 5000)? googling executeCommandLine didn’t yield much pertinent info:)

That means you have an error in your Items file.

When your Item file is wrong nothing else will work.

Switch - the Item Type
ExecCommand - the Item name
"label" - The string of text that the sitemap will display
<icon> - name of the icon file that is used on the sitemap

So to put it on your sitemap:

Switch item=ExecCommand

The number of milliseconds to wait for the command to complete. If it takes longer there will be no result. If it takes less time it will return immediately.

In the log you will see the result from the script printed using the code I posted above.

Sorry for the delay in my response, but you’ve been very generous with your suggestions and I wanted to triple check my configuration before I reported back that it simply wasn’t working for me. And I did.

My .items file has:
Switch ExecTest
My .sitemaps file has:
Switch item=ExecTest "Exec Test"
I have a, exectest.rules file in /opt/openhab/configurations/rules/ that is:

import org.openhab.core.library.types.*

rule "exectest"
when
    Item ExecTest received command
then
    val results = executeCommandLine("echo -e \"POWR1 \x0d\" | nc ipaddressoftv portoftv", 5000)
    logInfo("ExecTest", results)
end

And I’ve tried with putting the “label” in just the .items file and in both the .items and .sitemap files as well and the .rules file with and without the import at the beginning. I still only see the same “cannot retrieve item from registry” errors in all the logs. I’m curious for any other suggestions to get this method to give me more detailed texture on why reading of item file is failing. FWIW, I’ve also triple-checked that I’ve enabled DEBUG level logging in the logback.xml.

I, again, very much appreciate all the suggestions, but I’m stumped. I guess I’ll focus my energy on reinstalling openhab as apt-get and see if that does anything…

Hey @rlkoshak,

Sorry to bother, still haven’t figured this out, but I thought i’d switch over to the apt-get installation as you suggested to better troubleshoot. But I hit a bit of a snag. I backed up any config file I changed (included start.sh and start_debug.sh), deleted the /opt/openhab/ folder and the “service” file I created under /usr/lib/systemd/system/openhab.service to autostart openhab with systemd. I then rebooted, checked the service with systemctl status openhab and when I was convinced it was cleanly uninstalled I installed openhab with apt-get. Everything seems to have gone alright, but nothing is showing up when I point my browswer to http://ipaddressofrpi:8080/openhab.app?sitemap=nameofmysitemap I get what looks like a tomcat error (some kind of tunnelling/redirect error).

I suspect this is related to some collision on the 8080 port, I now remember one of the major reasons I went with the manual install in the first place was because I couldn’t figure out how to customize what ports access the Openhab webgui. In my manual install, I changed the start.sh and start_debug.sh ports to use 9307/9308 for http and https respectively but I don’t see a start script in the apt-get installation. I looked at openhab in /etc/init.d/openhab and there are clearly references to a $HTTP_PORT variable but that variable is not defined in that specific file. I saw the following in the “configuring the openHAB runtime” wiki:
${openhab_home}/etc/default/openhab USER_AND_GROUP=openhab:openhab HTTP_PORT=8080 HTTPS_PORT=8443 TELNET_PORT=5555 OPENHAB_JAVA=/usr/bin/java
which has the ${openhab_home}/etc/default/openhab as the title directly above with no further instructions.
I really don’t know what would be considered my openhab_home (my openhab user doesn’t have a folder created under /home/). I did find /usr/share/openhab/ which contained a bunch of symlinks and other folders, one of which was /usr/share/openhab/etc which was a symlink to /etc/openhab/jetty/etc/ and I made a directory called “default” there and then a file named openhab inside that newly created folder. But alas, after a reboot I still couldn’t access openhab at http://ipaddressofrpi:8080/openhab.app?sitemap=nameofmysitemap or http://ipaddressofrpi:9307/openhab.app?sitemap=nameofmysitemap

Extra info:
There is NOTHING in /var/logs/openhab/ which i discovered is where the new logs are supposed to be, as in not even an EMPTY log.
Running systemctl status openhab returns that /etc/init.d/openhab is running successfully.

What am I missing here? I wasted the whole weekend trying to figure this out, I’m sorry if its something simple and stupid that I shouldn’t be wasting anyone’s time with.

I can’t get to my machine remotely right now so I don’t have the exact right locations but the port is either changed from one of the scripts in /usr/share/openhab/bin or from /etc/init.d/openhab or from /etc/systemd/services/openhab.service (or something like that). It also might be changed from the jetty config. Once we figure out where it is to be changed the wiki clearly needs to be updated.

I wouldn’t expect there to be anything in /var/logs/openhab because if there is a port conflict OH dies before it gets a chance to spin up the logger.

I find it odd that systemctl says that it is active.

Well i think I’d found it (!!!) here:
/usr/share/openhab/bin/openhab.sh

I found that in a list of file locations listed [here] :
(https://github.com/openhab/openhab/wiki/Linux---OS-X#apt-get)
service configuration /etc/default/openhab site configuration /etc/openhab log files /var/log/openhab userdata like rrd4j databases /var/lib/openhab openHAB engine, addons and /webapps folder /usr/share/openhab
And though I didn’t have anything at /etc/default/openhab,I did have the rest. And there are lines inside /usr/share/openhab/bin/openhab.sh that define the HTTP, HTTPS and Telnet ports

Eventually my sitemap came up when I pointed it at http://ipaddressofpi:9307/openhab.app?sitemap=mysitemapname. None of my switches work but at least I see SOMETHING and have SOMETHING to troubleshoot. I’ll report back when I’m back to the same place I was when this whole thing started:)

For the record, this is/was the output of systemctl status openhab:
● openhab.service - LSB: openHAB server Loaded: loaded (/etc/init.d/openhab) Active: active (running) since Sun 2016-02-28 21:46:51 CST; 20h ago Process: 30968 ExecStop=/etc/init.d/openhab stop (code=exited, status=0/SUCCESS) Process: 30989 ExecStart=/etc/init.d/openhab start (code=exited, status=0/SUCCESS) CGroup: /system.slice/openhab.service └─31216 /usr/bin/java -Dlogback.configurationFile=/etc/openhab/logback.xml -Dosgi.clean=true -Declipse...

Spoke too soon. After a period of time, I started getting the same browser error’s when poiting it at http://ipaddressofpi:9307/openhab.app?sitemap=mysitemapname as if there’s nothing there. the sitemap came up at one point, though none of the items worked. At some point as I was attempting to transfer the setting I HAD in my backed up copy of openhab.cfg to the newly included openhab_default.cfg (renaming the old one to openhab.cfg.bak and the copying the new default to openhab.cfg and putting in the settings), the sitemap just stopped showing up. I replaced back the old openhab.cfg but it still doesn’t show up. I also chowned all the folders to openhab:openhab but it doensn’t seem to matter. Still nothing in /var/log/openhab either. I wonder if this is related to the logback.xml file I have in there. Should that be placed in /etc/openhab or one of the other locations? The paths it defines are relative, so i think i need to look it over to see if maybe something there needs to change.

Just FYI that the issue isn’t closed (and continuing to document for the next poor soul as unknowledgable as me :slight_smile: )

I suppose this is better for a new thread but: What are the practical differences between the Service Configuration, Site Configuration and things in the /usr/share/openhab directories (listed in the folders in the link in my above folder)? What type of customization are appropriate for which folders?

I am having difficulty following what you have and have not done. Rather than try to figure it out here is what you should have done.

Once your old install of openHAB was removed:

  1. Install openhab-runtime via apt-get
  2. Install all your needed bindings via apt-get
  3. Leave the existing logback.xml files unchanged. They are located in /etc/openhab/configurations (logback.xml and logback_debug.xml)
  4. Copy over your old openhab.cfg file or copy openhab_default.cfg to openhab.cfg and add the configurations for your bindings. This file is located in /etc/openhab/configurations
  5. Copy your old .items files to /etc/openhab/configurations/items
  6. Copy your old .rules files to /etc/openhab/configurations/rules
  7. Copy your old script files to /etc/openhab/configurations/scripts
  8. Copy your old sitemap files to /etc/openhab/configurations/sitemaps
  9. Copy your old persist files to /etc/openhab/configurations/persistence
    10.Copy your old transform files to /etc/openhab/configurations/transform
  10. If you are using a zwave dongle or some other binding that requires access to a device, add the openhab user to the appropriate group (do an ls -l /dev/tty[Your Device] and see what the group name is, often it is dialout).
  11. Redo any other tweaks you may have done to your environment that may need to be done.You probably don’t have anything.
  12. Restart openhab

The fact that it used to work and suddenly stopped implies that either OH crashed or you changed something to break it from the default install. This also could be the cause of the lack of log files.

To address some of your specific comments and questions:

Ignoring the sitemap issues for the moment, did you install your needed bindings? Did you copy over your Items and Rules files?

It is unlikely you could have changed anything in openhab.cfg that would cause the sitemap to stop showing up unless you introduced an error that causes openHAB itself to crash. Except for the file polling period, I can’t think of a single setting in that file that would impact the sitemap.

The start script for openHAB will chown and chmod all the files openHAB needs to own automatically.

Use the logback.xml that was installed with apt-get. Do not at this time copy over your own file or the logback files that came with the manual install of openHAB. The paths are all wrong in those files for an apt-get installed OH.

I don’t think I know what you mean by Service Configuration and Site Configuration but I’ll try to answer.

Almost all configuration changes go in /etc/openhab. This includes all your config files and home automation setup. This includes your sitemaps.

With an apt-get install the only two things you might need to do, as you saw, to the “Service” configuration would be to change the port that OH listens on in /usr/share/openhab/bin/openhab.sh or to change the user under which OH runs which would be done in /etc/init.d/openhab or /etc/system/[somewhere]/openhab.service.

I don’t know what you mean by Site Configuration but I’ll assume you mean the webapps stuff. All of these changes would take place in /usr/share/webapps and typical changes include renaming and/or adding icons to the webapps/images folder, adding habmin/habmin2, or creating a webview weather page as described by the WeatherBinding’s wiki page.

Finally, there might be some additional changes needed depending on your chosen bindings but they will be defined in the wiki page for that binding.

You should not be changing any other folders or files.

Once again, thanks so much for taking the time. I’ll respond inline:

  1. done
  2. done
  3. if there were already logback.xml and logback_debug.xml files in /etc/openhab/configurations on install, i think I inadvertently overwrote them with my old installation’s files (I thought I checked the folder first and would’ve backed up anything i was going to replace but maybe I missed them). This seems to most likely be my issue. I saw that the file had paths, but they looked relative to a variable that i was hoping was set correctly elsewhere. That was probably foolish.
  4. done, both with my old openhab.cfg and manipulating the new openhab.default.cfg file after cp’in it
  5. done (as well as a more_devices.xml file for my insteon devices)
  6. done (just the one we made to troubleshoot my initial issue)
  7. done (i had no scripts)
  8. done
  9. I actually didn’t do anything with the persistence files, but I also didn’t make any edits to them in the old installation. Should I have? I believe upon installation there were 4 files in that folder.
  10. done, added openhab user to dialout which is what my insteon PLM uses.
  11. Just changed the three ports under /usr/share/openhab/bin/openhab.sh
  12. done. (well i rebooted the RPi many times, maybe i’'ll try a sudo systemctl restart openhab)

I agree, maybe openHAB tried to write an Event log or something that it didn’t have permission to. I’ll try to roll back the logback files, and I’ve already switched back to the old openhab.cfg that you alluded to as unlikely to be culprit.

Yeah I saw that, but the /etc/openhab/jetty folder was owned by root:root, so after the webui stopped showing up, I chowned it to openhab just to see if it would work. it didn’t.

Sounds like this is a likely issue. If I delete the logback.xml’s that I moved in there, can i re-run sudo apt-get install openhab to download default logback files?

The only other thing i can think that I did was create a openhab folder in /home/ and symlink all the below folders into that /home/openhab folder cause I was getting tired of going all over the file system for them (well except for /etc/default/openhab which i didn’t have). Can’t imagine that would have a negative impact though…

This is what they are called in this [link] (https://github.com/openhab/openhab/wiki/Linux---OS-X#apt-get). I copied the pertinent section above, here it is again:
service configuration /etc/default/openhab site configuration /etc/openhab log files /var/log/openhab userdata like rrd4j databases /var/lib/openhab openHAB engine, addons and /webapps folder /usr/share/openhab
But thanks for the texture on that, very helpful.

apt-get install --reinstall openhab-runtime

You might want to delete ormove your existing logback.xml files out of config before you reinstall. I don’t think it overwrites them if the files already exist.

It shouldn’t. My /etc/openhab folder is actually a symbolic link to a folder in my home directory as well. And on top of that I have that folder source controlled in git so it is easy to roll back changes and keep up with my edits.

Whoops, didn’t see that you had replied. I decided to delete everything and start fresh after just using apt-get install didn’t work. Oh well, in that process now, will report back.

That’s genius. But does the symbolic link get overwritten when you update?

Just FYI, upon a fresh install, there are no logback.xml files in /etc/openhab/configurations/…

No, as far as the installer is concerned it sees the link as if the folder
already exists and leaves it alone.