Exec binding with php script return data

Hello
I’m new to Openhab but i’ve found it intriguing how configurable it is and defiently like it :slight_smile:
I’ve although ran into a problem i hope you guys can assist me with in respect to “Exec-bindings”

A bit about my project:

  • I’m running OH2 (ver 2.2.0) on my Synology 218+
  • I’ve installed OH2 through the via https://docs.openhab.org/installation/synology.html
    – As this is a manual install via synology i do not get a “service” running, at least what i can track… but OH2 functions without problems.
    – When i have to restart the OH2 it’s a bit of pain as i need to restart the entire NAS…
  • I’ve already got various widgets to work in HABPanel so it’s up and running…

My problem:

  • I want to take data from my own MySQL database (No MySQL persistent bindings is not an option, i want it from my external DB), in this case the simply the latest temperature
  • I’ve found out the best way to do this must be via exec bindings… i hope…
  • I’ve created a php script that works with no problems if I use it through the web browser of the Synology or via putty ssh. My return string from the php script is JSON and an example would be: [{“temperature”:“22.3”}]
    – I do need to write php56 test.php (in putty) before it works though… using php test.php only, it will fail on mysqli… (which is kind of funny as if i write php -v i get the 5.6 version…)
  • Next step is that i’ve used exec binding via Things in the following way (My test.things):
Thing exec:command:TempOutsideScript [command="php ./conf/scripts/TempOutside1Latest.php", transform="Temperature [JSONPATH($.temperature):%.1f °C]", interval=15, timeout=5]

– I’ve also tried:

Thing exec:command:TempOutsideScript [command="php ./conf/scripts/TempOutside1Latest.php", transform="REGEX((.*?))", interval=15, timeout=5]

– I’m pretty sure this is where my issue is, but i simply dont get how to test if this “thing call” works?
– It doesnt show up in PAPER UI -> Configuration -> Things (but perhaps it shouldn’t?)
– How do i determine if the path is correct?
– How do i determine if the “transform part” is correct?
– How do i determine if any value is coming from the command?

  • Afterwards the next step is to put in into items (test.items):
Number OutsideTemp1        "Temperature [%.2f °C]"      {channel="exec:command:TempOutsideScript:output"}		
  • Finally i added a template widget to the HABPanel which just shows the value for now, which returns “NULL”:
{{itemValue('OutsideTemp1')}} 	

The event.log only shows that:

  • Link ‘OutsideTemp1-exec:command:TempOutsideScript:output’ has been added.

The openhab.log shows (which means it is runnung…):

  • 2018-02-18 17:43:29.570 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘test.things’
  • 2018-02-18 17:54:16.961 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model ‘test.items’

I hope you guys can help me, or tell me if i’m doing some basic stuff wrong as i’m new too this.
If you need further information please let me know.

@Baldorian
maybe some tutorial for exec helps.

  1. try to execute the command as user openhab. And determine if user permissions and path is correct.
sudo -u openhab php ./conf/scripts/TempOutside1Latest.php
  1. First don’t use the transmformation and set up a rule to log the return value. See the tutorials.
  2. The item can not be a Number it needs to be a String.
  3. Add the transfomation. Try if it woks regex, json.
1 Like

Thanks Josar for replying :slight_smile:

Regarding 1) If i browse to my openhab installation (volume1/@appstore/openHAB) the path is correct… although:

  • sudo -u openhab php ./conf/scripts/TempOutside1Latest.php returns error in php file… (related to topic i wrote earlier)
  • sudo -u openhab php56 ./conf/scripts/TempOutside1Latest.php returns the [{“temperature”:“22.3”}]

Would you assume this to be a problem, my guess is that openhab only uses “php”?

If i remove the transformation, i should also be able to see the string value printed without any rule (when i change the item to a string) isn’t that correct? (I tried this, but doesnt work :))

@Baldorian

You should see what the exection returns when you use the item as text in your sitemap. But i have made the experience that setting up a rule and trigger that with a swicht, as shown in the examples, is the best way to debug the execution of script etc. As the Text Item is also limited in how many character it displays.

If you need to execute it with php56 then just change the exec line.

command="php56 ./conf/scripts/TempOutside1Latest.php"

I can not see where you described what error was encountered, you only stated it fails. But then expect it to work with openhab user which won’t.

Well i tried to make it really simple, simply having the php script returning as JSON “22.3” string with no data handling in the script at all…Which also runs fine from the terminal.

Regardless of the string returning exactly “22.3” or not i should still get something through the item and onto the sitemap/habpanel… but nothing…

Perhaps i’m failing on some other basic stuff, something i’m missing?

  • Do you need to do anything but have:
    – X.things
    – X.items
    – X.sitemap

Do i need somehow to indicate Openhab to run those “X.things” files? (as far as i can read myself through they should be run automatically)…

I tried using your example with the CPU temperature from the RPI system temperature link you provided… here you don’t use a switch either to get the CPU temperature out… i’m pretty sure i’ve done more or less the same (except for exluding the persistant part)

@Baldorian then just post the content all of your files.

Content of my files:

TempOutside1Latest.php:

<?php
  echo json_encode("28.2");
?>

running from /volume1/@appstore/openHAB/: sudo -u openhab php ./conf/scripts/TempOutside1Latest.php yields:

  • “28.2”

Overview.things:

Thing exec:command:TempOutsideScript [command="php ./conf/scripts/TempOutside1Latest.php", interval=15, timeout=5]

Overview.items:

String OutsideTemp1        "Temperature [%s degC]"      {channel="exec:command:TempOutsideScript:output"}

sitemap Overview label=“Overview”
{
Frame {
Text item=OutsideTemp1
}
}

Sitemap prints: Temperature - degC

and no errors in the logs.

what is in the output channel if you just set something up like this.

Thing exec:command:TempOutsideScript [command="php -v", interval=30, timeout=5]
Switch php_run    { channel="exec:command:TempOutsideScript:run"    }
String php_Out  { channel="exec:command:TempOutsideScript:output" }
rule "Test"
    when
        Item php_Out  received update
    then
        logInfo("Php", "Result:" + php_Out.state )
        php_Out.sendUpdate("")
end

use the full path to your file and try the f option.

php -f  <findFullPath>/openHAB/conf/scripts/TempOutside1Latest.php 

Hello Josar, appreciate the help :slight_smile:

I’ve tried to add the thing to overview.things, and added the Switch/String to overview.items, and the rule to overview.rules.
I’m presented with nothing (compared to “-” before) in the overview.sitemap, see code:

sitemap Overview label="Overview"
{
        Frame {
            Text item=OutsideTemp1    
        }
	Frame {
  	   Text item=OutsideTemp1clean    
	}

	Frame {
  	   Text item=php_run
	}

	Frame {
  	   Text item=php_Out
	}

	Switch item=php_run label="PHP RUN" icon="light"
}

OpenhabSitemapTest

Additionally if i go all the way to the root in putty LoginName@UnitName:/ and write:
php56 -f volume1/@appstore/openHAB/conf/scripts/TempOutside1Latest.php

i still get the “28.2” value…

Could there be something with the LoginName@UnitName ?? (i mean i’m not using “Admin” but my created login to the Synology to connect to putty?)
It seems to me like the “Thing” stuff isn’t run, since there’s no info in any log file what so ever… (unless there are other important log files than in userdata/logs
I do get the following in event.log:

  • 2018-02-19 20:16:54.430 [ome.event.ItemCommandEvent] - Item ‘php_run’ received command ON
  • 2018-02-19 20:16:54.431 [vent.ItemStateChangedEvent] - php_run changed from OFF to ON
  • 2018-02-19 20:16:54.962 [ome.event.ItemCommandEvent] - Item ‘php_run’ received command OFF
  • 2018-02-19 20:16:54.963 [vent.ItemStateChangedEvent] - php_run changed from ON to OFF

please post all your files contents.

Please make sure in PaperUI thath the Exec binding is installed.

Please try the simple php56 -v command i asked for, so we know that executing php from openhab works.

when i am on my rpi and try to find the root folder then i type

pi@raspberrypi:~/SourceCode/433Utils/RPi_utils $ cd
pi@raspberrypi:~ $ 

then i try to go the folders up

pi@raspberrypi:~ $ cd ..
pi@raspberrypi:/home $ cd ..
pi@raspberrypi:/ $  cd ..

till i reach the end. So i was in the folder pi as reference where it starts.

Then this is my fullpath /home/pi/SourceCode/raspberry-remote/send try to find your equivalent. Don’t forget the first slash.

Josar!! Thanks man!!! it works!

It was the exec binding that was messed up… I re-installed it and it worked! (from Paper UI)

Look:

@Baldorian just the transormation left to accomplish. :wink:

Hey Josar
Yep, but it appears i’m a total rookie in the Xbase / Xtend language…

You told me to not use transformation on “Things”, which means i must do it on rules.

I was thinking something like:

rule "OutsideTemperatureConversion"
when
	Item OutsideTemp1 received update 
// My item that is updated from Things returning [{"temperature":"22.3"}] 

then
	OutsideTemp1Numb.postUpdate(12133) // Simply a test to update into a number, which works fine
	OutsideTemp3.postUpdate(transform("REGEX","\btemperature":"\K\S+[[:digit:]]",OutsideTemp1.state) 
// my idea, but doesnt work... it should return 22.3.. and tried to print it both as a number and string item, neither works..
// I tested the command via https://regex101.com/ on the string [{"temperature":"22.3"}]

// I tried with jsonpath conversion also, didnt seem to do the trick either..
end

I’m obviously not understanding the language, do you have any insight? :slight_smile:

No i told , don’t do it yet. As there could also be problems. When you get your string you can do it. As all other errors are solved.

Your posted a string like this.

[{“temperature”:“22.3”}]

But it has to look like this
{"temperature":"22.3"} or this [{"temperature":"22.3"}]

In your rule it is right

[{"temperature":"22.3"}] 

Validate your json and your jsonpath here http://jsonpath.com/

At least one error "\btemperature":"\K\S+[[:digit:]]". The inner " has to be escaped as you embedd them in a string. "\btemperature\":\"\K\S+[[:digit:]]".

if you want to do it in the rule. Explanation for the regex see below.

rule "OutsideTemperatureConversion"
when
	Item OutsideTemp1 received update 
then
	OutsideTemp3.postUpdate(transform("JSONPATH","$..temperature",OutsideTemp1.state) 
        OutsideTemp3.postUpdate(transform("REGEX","s/.+:.(.{4}).+/$1/g",OutsideTemp1.state) 
end
String OutsideTemp1 "Temperature [JSONPATH($.temperature):%s °C]" 

or with regex and the unchanged string, but should also work with the changed string.
.+:.(.{4}).+ This is the regex : " All character, one :, one charater , a group of 4 character which will replace $1, all character left.
or this one
.*:.(.*).}] which you can decipher on https://regex101.com

String OutsideTemp1 "Temperature [REGEX(s/.+:.(.{4}).+/$1/g):%s °C]" 

Have a look here for some detailed regex