Import data from Fronius Solar api
Posted: Saturday 23 July 2016 1:01
Is there a script that allows to import in domoticz the data of production via solar API?
Open source Home Automation System
https://forum.domoticz.com/
I read it, but......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
Yes, my fronius inverter supports json Api interface.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: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.
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
}
}
}
}
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¶m=udevice&idx=412&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}
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 nothingEgregius wrote:create a file /home/pi/solar.phpmake it executable: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¶m=udevice&idx=412&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']); }
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.
Code: Select all
-rwxr-xr-x 1 pi pi 395 Jul 26 19:14 solar.php
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¶m=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}
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¶m=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
}
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] =>
)
)
)
)
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¶m=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¶m=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¶m=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);
}
Code: Select all
drwx------ 10 pi pi 4096 Jul 28 00:56 log
the script works on all 3 sensor.Egregius wrote:Oh, forgot to quote the logfile.
$fp = fopen(/var/log/fronius.log,"a+");
Must be
$fp = fopen('/var/log/fronius.log',"a+");
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
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
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¶m=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¶m=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¶m=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);
}
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¶m=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¶m=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¶m=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¶m=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¶m=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¶m=udevice&idx=44&nvalue=0&svalue=$prelevata");
$oem = curl_exec($ch);
curl_close($ch);
//sleep(2);
goto inizio;
?>