Exec Binding, pipe is not being accepted as part of command

  • Platform information:

    • Hardware: ESXI VM
    • OS: Debian GNU/Linux 9 (stretch)
    • Java Runtime Environment: java version “1.8.0_201”
    • openHAB version: 2.5.0~S1502-1
  • Issue of the topic: Storing the output of an exec command in an item

Hello all.
I am struggling to get the output of an exec command into an item.
After successfully firing the below listed rule, the item has a state of UNDEF.

I have been recently learning how to adapt pmpkk’s Matrix Theme to my simple set up. I would like to display the installed version of openHAB on one of HabPanel system information pages, and am trying to get the version of OH installed into an item using the exec binding.
I’m not certain if there is an easier way to do this or if I’m doing this completely wrong but i am also using this as a learning experience for myself. If there is an easier or better way, please let me know!

exec Binding version installed:
225 │ Active │ 80 │ 2.5.0.201901142229 │ Exec Binding

exec.things

Thing exec:command:OH_version  [command="dpkg -s openhab2 | grep -i version", interval=0,autorun=false]

exec.items

String OH_version_out "[%s]" { channel="exec:command:OH_version:output" }

For testing, i am using a rule from part of my Zwave lights remote. ie when the lights are turned on… When working properly it will be put into a rule and executed when the system is started.

Mocked up rule where the remote button “ON” is pressed…

lights.rules

rule "Turn lights ON"
when
          Item on_button received command ON
then
          OH_version_out.sendCommand(ON)
          logInfo("EXEC BINDING", OH_version_out.state)
end

After successful running of the rule, the Item “OH_version_out” has an UNDEF state.
Running the command from the command line and the result:

openhab@OpenHAB-debian:$ dpkg -s openhab2 | grep -i version
Version: 2.5.0~S1502-1

Is anyone able to point me in the right direction or have I misunderstood how the exec binding works…

Thanks in advance,
Craig

Getting closer. I was not executing the correct item type. Should be:
exec.items

// state of the execution, is running or finished
Switch OH_version_run {channel="exec:command:OH_version:run"}
// Output of command line execution 
String OH_version_out "[%s]" { channel="exec:command:OH_version:output" }

lights.rules updated snippet

   OH_version_run.sendCommand("ON")
            logInfo("EXEC BINDING ON", OH_version_run.state)
            while(OH_version_run.state != OFF){
            Thread::sleep(500)
            }
    logInfo("EXEC BINDING", OH_version_out.state.toString) 

This results in “OH_version_out” being populated with data.
However the pipe in the exec command is being rejected, so the grep portion is not being processed. See the last line in the log below:

2019-01-27 20:02:04.076 [vent.ItemStateChangedEvent] - OH_version_run changed from OFF to ON

==> /var/log/openhab2/openhab.log <==

2019-01-27 20:02:04.089 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn’t transform response because transformationService of type ‘REGEX’ is unavailable
==> /var/log/openhab2/events.log <==

2019-01-27 20:02:04.090 [vent.ItemStateChangedEvent] - OH_version_run changed from ON to OFF

2019-01-27 20:02:04.090 [vent.ItemStateChangedEvent] - OH_version_out changed from Package: openhab2
Status: install ok installed
Priority: optional
Section: java
Installed-Size: 80274
Maintainer: https://community.openhab.org
Architecture: all
Source: openhab2
Version: 2.5.0~S1502-1
Depends: adduser
Suggests: zulu-8 | zulu-embedded-8 | oracle-java8-installer | openjdk-8-jdk-headless
Conflicts: openhab2-online, openhab2-offline
Conffiles:
/etc/default/openhab2 ce1de05b21befb8a4fc0f0e8e881de69
/etc/profile.d/openhab2.sh 6d8400926cb4a5fccfcd631818b5c539
/etc/openhab-cli/command-overrides.sh 8cd755c6f3b4ba095af80bc77c76e927
/etc/openhab2/rules/readme.txt f2780c3cb08c0cc211e70d1dd2b2af38
/etc/openhab2/icons/classic/readme.txt 011f31d40515c2fac27cced93642b105
/etc/openhab2/sounds/doorbell.mp3 70574f7e973f11bb455aede67937da85
/etc/openhab2/sounds/barking.mp3 cbec913d66083aef95c985138268d09f
/etc/openhab2/things/readme.txt b5f79f235cbd6b00988f2f55a64f1382
/etc/openhab2/transform/en.map f61a41a4e7ad91652408a0f97037f9d2
/etc/openhab2/transform/de.map cda3ddd36f115257078eba6591a11946
/etc/openhab2/transform/readme.txt 232ab644e3b46a7a635edd3ba62065e3
/etc/openhab2/items/readme.txt f9812990e58a5c7c6b6c20416e551c94
/etc/openhab2/persistence/readme.txt 8efee3cf25f59daf4ff1f13f5d9e5bcd
/etc/openhab2/services/addons.cfg 9b4cd4e740b09fcac98867ca3c21239e
/etc/openhab2/services/runtime.cfg eaf90a4db0c65f870dfb9e3a6be85a53
/etc/openhab2/services/readme.txt cb77ecf2c8c08d77dd3a4112b784c065
/etc/openhab2/sitemaps/readme.txt 1eb8ac0d62b23d6aa03738fa27ba8bf7
/etc/openhab2/html/index.html 4777d08e4312bb0c964fceadbe670625
/etc/openhab2/html/readme.txt 20c91c90ba105a0478219bfe026d2260
/etc/openhab2/scripts/readme.txt 69033952a19ce0d58630a2485dadffc4
/var/lib/openhab2/etc/users.properties 9f2f742f036c7302080fbed7d9723b41
/var/lib/openhab2/etc/org.ops4j.pax.logging.cfg 84cf9d71c50b76b7e1c87d0de44fb3dc
/var/lib/openhab2/etc/shell.init.script 3fff75e401bf196e6bfdd2e62c6e7266
/var/lib/openhab2/etc/java.util.logging.properties adb9f5b39047b64b12a6dc4834afc76e
/var/lib/openhab2/etc/scripts/shell.completion.script ac0e1a959777126397265ed304c8fe5c
/var/lib/openhab2/etc/scripts/shell.colors.script 00a54900d8a1fe0bdc83f8d719938ce
/var/lib/openhab2/etc/keystore 936040b89edd5ef52bbe60b4250ad05e
/var/lib/openhab2/etc/org.ops4j.pax.web.cfg 3afafbfbbd3abf44e3f1b3b0d21034bd
/var/lib/openhab2/etc/com.eclipsesource.jaxrs.swagger.config.cfg 6d4857767a953ea614289efd2936d88e
/var/lib/openhab2/tmp/README 4978d8f2a2800148bba63fd4ed226b83
/var/lib/openhab2/etc/equinox-debug.properties 383caa00ead275df972aaf251bea6b11
/var/lib/openhab2/etc/keys.properties 824c4436939f111cf0e5163dbadb419b
/var/lib/openhab2/etc/org.eclipse.smarthome.audio.cfg 767b9bf993fa6c74ac5a90758d92a026
/var/lib/openhab2/etc/org.eclipse.smarthome.voice.cfg a1ecde6c004db8ac15a47ea2600e3a6c
Description: openhab2
Linux installation package for openHAB 2.
Homepage: www.openhab.org

dpkg-query: error: --status needs a valid package name but ‘’|’’ is not: illegal package name in specifier ‘’|’’: must start with an alphanumeric character

Use --help for help about querying packages.

dpkg-query: error: --status needs a valid package name but ‘’|’’ is not: illegal package name in specifier ‘’|’’: must start with an alphanumeric character

How can i escape the pipe so it will not be rejected? I have tried ^| but it did not work.

Thanks,
Craig

I wonder if the binding implementation itself uses the pipe feature to return results.

A workaround would be to put the working parts into a little script?

Or you could try passing the pipe as an input channel. Not that it should be needed.

I also suggest getting a trace log to see what command is actually being executed.

An unrelated word of caution, cos it was seen in another thread. There’s a finite time between triggering run, and run.state going to ON. Depending on system performance you could act too early and get the result of a previous run. Check for run going ON before waiting, or allow a delay, or trigger a separate rule from run going OFF

Ideally set autoupdate false on the run Item as well, stop it interfering.

Thanks @rossko57 and @namraccr for your replies. My Windows computer decided to crash on me so it’s taken a few minutes to get back up and running…

I created a bash script and am getting the desired result:
updated thing:

Thing exec:command:OH_version [command="/bin/bash /etc/openhab2/scripts/version.sh", interval=0, autorun=false]

bash script:
version.sh

#!/bin/bash
dpkg -s openhab2 | grep -i version

Result for “OH_version_out”

Version: 2.5.0~S1502-1

I’m still getting some errors in the logging, so will have to look into them another time.

And when i redo my rules i will take into account your recommendations @rossko57, i need to learn best practice as well.

Craig