Page 1 of 1

How to import data from php script for a counter

Posted: Sunday 08 November 2015 14:35
by btw8
Hello Community,
i'm new to domoticz and not sure if this is the right place for my question.

Domoticz is running here on a Synology DS-214. Everything is perfect so far but now i wonder how to get data from variables in php script that is running on the same machine into domoticz as a counter. I know Javascript and a little bit PHP. So i made a script to read out data from my solar inverter (Kostal Piko 4.2), see below.

In domoticz i set up a dummy device with a virtual sensor. And now? Is it possible to import the output from my script?

Code: Select all

<?php

    /* ########## DATA KOSTAL ########## */
        $url = "192.168.11.99";
        $username = "user";
        $password = "password";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        $pos1 = strpos($output,"aktuell</td>");
        $pos2 = strpos($output,"</td>",$pos1+20);
        $data = substr($output,($pos1+65),$pos2-$pos1-65);
    /* # WERT ERTRAG # */
        $ertrag = (float) $data;

        $pos1 = strpos($output,"Gesamtenergie</td>");
        $pos2 = strpos($output,"</td>",$pos1+30);
    /* # WERT GESAMTERTRAG # */
        $gesamtertrag = substr($output,($pos1+70),$pos2-$pos1-70);


        $pos1 = strpos($output,"Tagesenergie</td>");
        $pos2 = strpos($output,"</td>",$pos1+30);
    /* # WERT TAGESERTRAG # */
        $tagesertrag = substr($output,($pos1+70),$pos2-$pos1-70);

echo($ertrag);
echo($tagesertrag);
echo($gesamtertrag);

?>
regards, Oliver

Re: How to import data from php script for a counter

Posted: Monday 09 November 2015 20:56
by stlaha2007
Hi Oliver,

Havent tried out php to push data into domoticz. However the subforums contain several examples...

I would recommend to look at the domoticz.com/wiki/json api....
There are some examples how to create and update virtual devices.

For example iḿ using RFXmeter with gas and water to import the meter readings from file.

According to your story your better use rfxmeter with electricity or the (P1) power and counter.

I created the virtualsensors with bash commandline tools directly with the json-api.
Also using this to push the data into domoticz.

Eg. Push 120Watt And Counter 123456 into an Electricity Meter VirtualDevice

Code: Select all

curl -s "http://domoticz.ipadd.ressImageortnumber/json.htm?type=command&param=udevice&idx=VirtualDeviceIDX&svalue=120;123456"
Grtz,
Stephan

Sent from my K00C using Tapatalk

Re: How to import data from php script for a counter

Posted: Wednesday 11 November 2015 11:16
by btw8
Thank you Stephan for pointing me in the right direction.
The following script works for pushing the value of actual power to a virtual device.

Greetings, Oliver.

Code: Select all

<?php

    /* ########## DATA KOSTAL ########## */
        $url = "192.168.11.99";
        $username = "user";
        $password = "password";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        $pos1 = strpos($output,"aktuell</td>");
        $pos2 = strpos($output,"</td>",$pos1+20);
        $data = substr($output,($pos1+65),$pos2-$pos1-65);
    /* # WERT ERTRAG # */
        $actualpower = (float) $data;

        $pos1 = strpos($output,"Gesamtenergie</td>");
        $pos2 = strpos($output,"</td>",$pos1+30);

/* ########## PUSH DATA TO DOMOTICZ ########## */

$url = '192.168.11.90:8084/json.htm?';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url.http_build_query(
		array(
			'type' => 'command',
			'param' => 'udevice',
			'idx' => '24',
			'svalue' => $actualpower
		)
	)
);

curl_exec ($ch);
curl_close ($ch);

?>