Get value from PHP (EXEC)

Hi, i’m trying to get a value from a php with exec.

This is my item : Number BatDom “” {exec="<[ php /Users/admin/OpenHab/PHP/BattDom.php]"}

This is my Sitemap : Text item=BatDom label=“BatDom”

Know that my exec binding is working since i’m using it for other thing

What I want to do is to get this value and make a chart with it (it’s my iPhone battery % )

p.s this is my php …

<?php include ("class.findmyiphone.php");

$fmi = new FindMyiPhone(“thib5"AT"me.com”, “XXX”);

echo $fmi->getBatteryLevel(“WiexmlYdIa2zbUHjMvXlWOt6lmq2GCu7gEafOzP/amxDUGh6dtTMueHYVNSUzmWV”);

?>

Maybe you have to use the full path for php, e.g. /usr/bin/php or whereever php resists.

Nah … I make it work but now i’m stock with a string value and i’m not able to use it in a chart … what could i do !?

Hi,

maybe try to convert like this in a rule when item is to be stored via cron:

var int Temp = (yourvariable.state as DecimalType).intValue()
postUpdate(xxxx, Temp)

where xxx is the variable you want to store and visualize in your graph.

Cheers

Björn

So my rules look like that :

rule "stringtoint"
when Item stringtoint received command
then
var int Temp = (stringtoint.state as DecimalType).intValue()
postUpdate(BatDom, Temp)
end

and i receive this error :

2016-02-25 14:43:41.350 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule ‘stringtoint’: Cannot cast org.openhab.core.library.types.StringType to org.openhab.core.library.types.DecimalType

Please add some logging to the rule:

rule "stringtoint"
    when Item stringtoint received command
 then
    logInfo("stringtoint rule","String is {}",stringtoint.state.toString)
    var int Temp = (stringtoint.state as DecimalType).intValue()
    postUpdate(BatDom, Temp)
end 

Then there should be an additional Line in openhab.log just before the error.

2016-02-25 22:09:44.766 [INFO ] [.model.script.stringtoint rule] - String is 20

That’s strange… In fact, the casting should work

Maybe stringtoint is a reserved name, so try a more natural itemname, e.g. MyPHPvalue or so…

Ah. .intValue() could be wrong, I use .intValue without braces in my rules.

nah … still the same error … :pensive: could it be because of my item :

ITEMS:

Number BatDom ""
String MyPHPvalue “”

or maybe my php … since the exec thing didn’t work I rewrite my php :

Php

<?php include ("class.findmyiphone.php"); $fmi = new FindMyiPhone("thib5@me.com", "Bond 0819"); $BattDom = $fmi->getBatteryLevel("WiexmlYdIa2zbUHjMvXlWOt6lmq2GCu7gEafOzP/amxDUGh6dtTMueHYVNSUzmWV");

function postUpdate($item, $data) {
$url = “http://localhost:8880/rest/items/” . $item;
$options = array( ‘http’ => array(
‘header’ => “Content-type: text/plain\r\n”,
‘method’ => ‘POST’,
‘content’ => $data));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
$formattedNum = $BattDom."";
$percent = $formattedNum * 100;
postUpdate(“MyPHPvalue”,$percent);
?>

Hi Dominic,

Maybe I am wrong, but have you just tried to convert your output in you php to a integer value and then do an output via echo (e.g. multiplication with 1)?

Cheers
Björn

i try converting it directly from my php … the echo work but openhab said :" BatDom receive command "
with nothing after … and my value doesn’t update so i guest that it doesn’t receive anything

Hi,
Maybe try a php file only with command echo “100”.

then in the items file:
Number Check “Check [%.0f]” { exec="<[php /Users/admin/OpenHab/PHP/BattDom.ph:600000:REGEX((.*?))]" }
sitemap ony
Text item=Check.

Cheers
Björn

PS: I missed the first time to make my php file executable with chmod.

I already try that and it’s working … (well i can get my value in my php) I din’t test it in a chart cause the issue was when I put my real function ( the on to get my iPhone battery ) the echo was returning all the process to connect to iCloud server and after the value so my BatDom Value was like :

  • Trying 17.167.144.28…
  • Connected to fmipmobile.icloud.com (17.167.144.28) port 443 (#0)
  • TLS 1.2 connection using TLS_RSA_WITH_RC4_128_MD5
  • Server certificate: *.icloud.com
  • Server certificate: Apple IST CA 2 - G1
  • Server certificate: GeoTrust Global CA
  • Server auth using Basic with user ‘thib5@me.com’

POST /fmipservice/device/thib5@me.com/initClient HTTP/1.1
Host: fmipmobile.icloud.com

etc…