Page 1 of 2

Import data from Fronius Solar api

Posted: Saturday 23 July 2016 1:01
by videodrome
Is there a script that allows to import in domoticz the data of production via solar API?

Re: Import data from Fronius Solar api

Posted: Saturday 23 July 2016 4:37
by trixwood
did you try the search? Like search for "Fronius"? You know the upper right corner?

http://domoticz.com/forum/viewtopic.php ... it=Fronius

Re: Import data from Fronius Solar api

Posted: Saturday 23 July 2016 13:38
by videodrome
trixwood wrote:did you try the search? Like search for "Fronius"? You know the upper right corner?

http://domoticz.com/forum/viewtopic.php ... it=Fronius
I read it, but......
1) I don't want to install emoncms, feed data to this and then pass them to domoticz. I'd like to transfer them in real time directly from the inverter to domotic via solar API call. I am actually downloading data from the inverter (using the Fronius push service) to PVoutput which feeds domoticz. The problem is that the push service sends data every five minutes, so I can't have the production data in real time into domoticz.
2) if I launch a http request like this"http://192.168.x.xxx./solar_api/v1/GetI ... verterData" I get a json file with all the readings in real time. II think it is possible to feed a virtual sensor with this data, but how?

Re: Import data from Fronius Solar api

Posted: Saturday 23 July 2016 16:13
by Egregius
Your inverter has a json api interface?
Run a script that pulls that json, decode it and push it to domoticz to a virtual solar device.
Is you post a json example I can create a php script for you.
I do something similar but my data comes from a csv that's updated every minute. Once the value is in Domoticz I use it to decide if lights need to switch on and stuff like that.

Re: Import data from Fronius Solar api

Posted: Saturday 23 July 2016 23:19
by videodrome
Egregius wrote:Your inverter has a json api interface?
Run a script that pulls that json, decode it and push it to domoticz to a virtual solar device.
Is you post a json example I can create a php script for you.
I do something similar but my data comes from a csv that's updated every minute. Once the value is in Domoticz I use it to decide if lights need to switch on and stuff like that.
Yes, my fronius inverter supports json Api interface.
Tomorrow, when the inverter is active, i'll post the json reply from the http request.
Thanx

Re: Import data from Fronius Solar api

Posted: Sunday 24 July 2016 18:33
by videodrome
Egregius wrote:Your inverter has a json api interface?
Run a script that pulls that json, decode it and push it to domoticz to a virtual solar device.
Is you post a json example I can create a php script for you.
I do something similar but my data comes from a csv that's updated every minute. Once the value is in Domoticz I use it to decide if lights need to switch on and stuff like that.
Below the reply from the api call:

http://192.168.X.XXX/solar_api/v1/GetIn ... verterData

PAC is the value of AC power generated in real time

Code: Select all

{
	"Head" : {
		"RequestArguments" : {
			"DataCollection" : "CommonInverterData",
			"DeviceClass" : "Inverter",
			"DeviceId" : "1",
			"Scope" : "Device"
		},
		"Status" : {
			"Code" : 0,
			"Reason" : "",
			"UserMessage" : ""
		},
		"Timestamp" : "2016-07-24T18:22:14+02:00"
	},
	"Body" : {
		"Data" : {
			"DAY_ENERGY" : {
				"Value" : 17710,
				"Unit" : "Wh"
			},
			"FAC" : {
				"Value" : 50,
				"Unit" : "Hz"
			},
			"IAC" : {
				"Value" : 2.56,
				"Unit" : "A"
			},
			"IDC" : {
				"Value" : 1.8,
				"Unit" : "A"
			},
			"PAC" : {
				"Value" : 570,
				"Unit" : "W"
			},
			"TOTAL_ENERGY" : {
				"Value" : 507147.03,
				"Unit" : "Wh"
			},
			"UAC" : {
				"Value" : 231.5,
				"Unit" : "V"
			},
			"UDC" : {
				"Value" : 345.9,
				"Unit" : "V"
			},
			"YEAR_ENERGY" : {
				"Value" : 507147.72,
				"Unit" : "Wh"
			},
			"DeviceStatus" : {
				"StatusCode" : 7,
				"MgmtTimerRemainingTime" : -1,
				"ErrorCode" : 0,
				"LEDColor" : 2,
				"LEDState" : 0,
				"StateToReset" : false
			}
		}
	}
}

Re: Import data from Fronius Solar api

Posted: Sunday 24 July 2016 19:51
by Westcott
There are many examples on this forum showing how to read JSON data directly with Lua, and update virtual devices.

Re: Import data from Fronius Solar api

Posted: Monday 25 July 2016 8:17
by Egregius
create a file /home/pi/solar.php

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.x.xxx/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    file_get_contents('http://127.0.0.1:8084/json.htm?type=command&param=udevice&idx=412&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}
make it executable:
chmod +x /home/pi/solar.php

add the script to cron (crontab -e)
* 7-23 * * * /home/pi/solar.php
That's every minute between from 7 till 23.

Re: Import data from Fronius Solar api

Posted: Tuesday 26 July 2016 23:13
by videodrome
Egregius wrote:create a file /home/pi/solar.php

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.x.xxx/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    file_get_contents('http://127.0.0.1:8084/json.htm?type=command&param=udevice&idx=412&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}
 
make it executable:
chmod +x /home/pi/solar.php

add the script to cron (crontab -e)
* 7-23 * * * /home/pi/solar.php
That's every minute between from 7 till 23.
i can't execute the script. I create the php file and changed the idx number of my sensor, the ip number and port of domoticz, added the scripts to crontab and maked executable with chmod, but nothing

The attributes of php file are correct?

Code: Select all

-rwxr-xr-x 1 pi pi      395 Jul 26 19:14 solar.php
my script below . If i try to launch php solar.php i have no reply
.

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}

Re: Import data from Fronius Solar api

Posted: Tuesday 26 July 2016 23:21
by Egregius
Add a line like this to see if php is ok:
echo 'test';
And also add echo in front of the file_get_contents of the udevice command to see the reply of domoticz.
Otherwise there's no output.

Re: Import data from Fronius Solar api

Posted: Tuesday 26 July 2016 23:59
by videodrome
the first echo test is ok but the second echo on the line of "file_get_contents" is negative... :| :|

Re: Import data from Fronius Solar api

Posted: Wednesday 27 July 2016 7:09
by Egregius
Can you post the output of this:

Code: Select all

#!/usr/bin/php
<?php
echo 'test 1';
$data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
print_r($data);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    echo 'test 2';
    echo 'PAC='.$data['Body']['Data']['PAC']['Value'];
    file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}

Re: Import data from Fronius Solar api

Posted: Wednesday 27 July 2016 19:42
by videodrome
yess, we are, now the sensor read the output. :)
1 read only, when i launched the script in putty. I added the php file in crontab, but it doesn't seems to work :roll:
Is it possible to add the DAY ENERGY and the TOTAL ENERGY in the script? Thanx in advance


Code: Select all

test 1Array
(
    [Head] => Array
        (
            [RequestArguments] => Array
                (
                    [DataCollection] => CommonInverterData
                    [DeviceClass] => Inverter
                    [DeviceId] => 1
                    [Scope] => Device
                )

            [Status] => Array
                (
                    [Code] => 0
                    [Reason] =>
                    [UserMessage] =>
                )

            [Timestamp] => 2016-07-27T19:16:46+02:00
        )

    [Body] => Array
        (
            [Data] => Array
                (
                    [DAY_ENERGY] => Array
                        (
                            [Value] => 18862
                            [Unit] => Wh
                        )

                    [FAC] => Array
                        (
                            [Value] => 49.98
                            [Unit] => Hz
                        )

                    [IAC] => Array
                        (
                            [Value] => 1.03
                            [Unit] => A
                        )

                    [IDC] => Array
                        (
                            [Value] => 0.67
                            [Unit] => A
                        )

                    [PAC] => Array
                        (
                            [Value] => 196
                            [Unit] => W
                        )

                    [TOTAL_ENERGY] => Array
                        (
                            [Value] => 572775
                            [Unit] => Wh
                        )

                    [UAC] => Array
                        (
                            [Value] => 219.1
                            [Unit] => V
                        )

                    [UDC] => Array
                        (
                            [Value] => 351.4
                            [Unit] => V
                        )

                    [YEAR_ENERGY] => Array
                        (
                            [Value] => 572775.44
                            [Unit] => Wh
                        )

                    [DeviceStatus] => Array
                        (
                            [StatusCode] => 7
                            [MgmtTimerRemainingTime] => -1
                            [ErrorCode] => 0
                            [LEDColor] => 2
                            [LEDState] => 0
                            [StateToReset] =>
                        )

                )

        )

)

Re: Import data from Fronius Solar api

Posted: Wednesday 27 July 2016 22:56
by Egregius
It isn't very hard to add more values, just walk thru the array:

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
print_r($data);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=59&nvalue=0&svalue='.$data['Body']['Data']['DAY_ENERGY']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=60&nvalue=0&svalue='.$data['Body']['Data']['TOTAL_ENERGY']['Value']);
    logwrite($result);
}

function logwrite($msg) {
    $time    = microtime(true);
    $dFormat = "Y-m-d H:i:s";
    $mSecs   =  $time - floor($time);
    $mSecs   =  substr(number_format($mSecs,3),1);
    $fp = fopen(/var/log/fronius.log,"a+");
    fwrite($fp, sprintf("%s%s %s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp);
}
I always put a microsleep of 1/4 sec between commands, just to not stress the system to much.
I also added a logfile (make sure /var/log is writable for the user that runs the script) to check if it runs in cron.

Re: Import data from Fronius Solar api

Posted: Thursday 28 July 2016 1:13
by videodrome
I always put a microsleep of 1/4 sec between commands, just to not stress the system to much.
I also added a logfile (make sure /var/log is writable for the user that runs the script) to check if it runs in cron.[/quote]

I changed the owner (pi) to the log folder and add the permission to be writable (700) at the owner. Is it correct?
but when i launch the php file it says
HP Parse error: syntax error, unexpected '/' in /home/pi/solar.php on line 21

Re: Import data from Fronius Solar api

Posted: Thursday 28 July 2016 1:15
by videodrome
I always put a microsleep of 1/4 sec between commands, just to not stress the system to much.
I also added a logfile (make sure /var/log is writable for the user that runs the script) to check if it runs in cron.[/quote]

I changed the owner (pi) to the log folder and add the permission to be writable (700) at the owner. Is it correct?

Code: Select all

drwx------  10 pi   pi         4096 Jul 28 00:56 log
but when i launch the php file it says
HP Parse error: syntax error, unexpected '/' in /home/pi/solar.php on line 21

Re: Import data from Fronius Solar api

Posted: Thursday 28 July 2016 7:58
by Egregius
Oh, forgot to quote the logfile.

$fp = fopen(/var/log/fronius.log,"a+");
Must be
$fp = fopen('/var/log/fronius.log',"a+");

Re: Import data from Fronius Solar api

Posted: Friday 29 July 2016 18:52
by videodrome
Egregius wrote:Oh, forgot to quote the logfile.

$fp = fopen(/var/log/fronius.log,"a+");
Must be
$fp = fopen('/var/log/fronius.log',"a+");
the script works on all 3 sensor.
I have this warnirng the the end of the script

Code: Select all

PHP Warning:  sprintf(): Too few arguments in /home/pi/solar.php on line 22
PHP Warning:  sprintf(): Too few arguments in /home/pi/solar.php on line 22
PHP Warning:  sprintf(): Too few arguments in /home/pi/solar.php on line 22
Otherwise i can't execute the script with crontab, the cronjob must be run as root or as pi?

the crontab seems correct....

Code: Select all

# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
* 7-24 * * * /home/pi/solar.php

Re: Import data from Fronius Solar api

Posted: Friday 29 July 2016 22:59
by Egregius
Didn't test the code, copied from my script and stripped the unneeded stuff, sorry for the errors. There's a %s to much in the sprintf line.

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
print_r($data);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=59&nvalue=0&svalue='.$data['Body']['Data']['DAY_ENERGY']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=60&nvalue=0&svalue='.$data['Body']['Data']['TOTAL_ENERGY']['Value']);
    logwrite($result);
}

function logwrite($msg) {
    $time    = microtime(true);
    $dFormat = "Y-m-d H:i:s";
    $mSecs   =  $time - floor($time);
    $mSecs   =  substr(number_format($mSecs,3),1);
    $fp = fopen(/var/log/fronius.log,"a+");
    fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp);
}
 
I always run scripts as root (sudo crontab -e)
Never used a user crontab, don't even know if that's executed without being logged in.
Also, maybe test with chmod 755 solar.php

Was there a logfile after running manually?

Re: Import data from Fronius Solar api

Posted: Sunday 31 July 2016 23:13
by videodrome
Now the script works well, thanx. :D

I ask you for an advice on another php script already running.
The script below, reads from a meter (eastron sdm 220 c) under rs485 modbus.
All the readings works well.
I'd like to import the reading of the energy imported (-i).

I entered in line n. 5 -i for reading, and at the bottom of the file the reference to the idx n.44 for the reading of imported energy. When I restart the script I find inverted the two readings ( exported on 44 and imported on 43 ) Why ?


Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$x = exec("/usr/local/bin/sdm120c -v -c -p -g -f -e -i -t -q -w3 -z5 /dev/ttyUSB0");
$data = preg_split('/[[:space:]]+/', $x);
if ($data[8] = 'OK') {

  $tensione = $data[0];
  $corrente = $data[1];
  $potenza = $data[2];
  $cosfi = $data[3];
  //$frequenza_2 = $data[4];
  $immessa = $data[5];
  $prelevata = $data[6];
  $energia = $data[7];
}

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=1&nvalue=0&svalue=$tensione");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=2&nvalue=0&svalue=$corrente");
      $oem = curl_exec($ch);
curl_close($ch);


$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=3&nvalue=0&svalue=$potenza;$energia");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=$cosfi");
      $oem = curl_exec($ch);
curl_close($ch);


$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=43&nvalue=0&svalue=$immessa");
      $oem = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue=$prelevata");
       $oem = curl_exec($ch);
 curl_close($ch);

//sleep(2);
goto inizio;

?>