Page 1 of 1
update virtual counter -help
Posted: Tuesday 17 May 2016 15:11
by Mazzokun
Hi I have a Sunway solar inverter and I created a virtual electrical counter in Domoticz.
I created this script to read the txt file that is stored in the inverter web server.
Code: Select all
#!/usr/bin/php
<?php
########## DATA SUNWAYS ##########
$url = "http://13.3.89.50";
$username = "customer";
$password = "00000000";
$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);
curl_close($ch);
// Open the file
$fp = @fopen("http://13.3.89.50/data/sys.txt", 'r');
// Delete # & filter
$fp = explode('#', $input);
$actualPower = filter_var($fp[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower = filter_var($fp[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower = filter_var($fp[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
fclose($fp);
########## PUSH DATA TO DOMOTICZ ########## */
$url = '13.3.89.3:8080/json.htm?'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url.http_build_query(
array(
'type' => 'command',
'param' => 'udevice',
'idx' => '21',
'svalue' => $actualPower
)
)
);
curl_exec ($ch);
curl_close ($ch);
?>
I assign the script to "on action" ov a virtual switch.. Log: Error returned: 32256.
Where is the bug?
Re: update virtual counter -help
Posted: Wednesday 18 May 2016 9:12
by bobkersten
Assigning this script to the on-action of a virtual switch might not give you the results you want. I think this script needs to be run from a cronjob on your system, say, once a minute. Then you'd want to store both the actual power as well as totalpower. If you created a dummy Electricity (instant and counter) device the url should look like this:
/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=POWER;ENERGY
From the documentation here:
https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's
That means your php script should set the svalue param to "{$actualPower};{$totalPower}" and while I'm not sure it's absolutely necessary, also add a nvalue param with value 0.
Success.
Re: update virtual counter -help
Posted: Thursday 19 May 2016 10:51
by Mazzokun
Thank you very much

I rewrote the script:
Code: Select all
#!/usr/bin/php
<?php
########## DATA SUNWAYS ##########
$url = "http://13.3.89.50/data/sys.txt";
$username = "customer";
$password = "00000000";
$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);
curl_close($ch);
// Open the file
$fp = @fopen("http://13.3.89.50/data/sys.txt", 'r');
// Delete # & filter
$fp = explode('#', $input);
$actualPower = filter_var($fp[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower = filter_var($fp[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower = filter_var($fp[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
########## PUSH DATA TO DOMOTICZ ########## */
fopen ("http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=20&nvalue=0&svalue={$actualPower};{$dailyPower}");
fopen ("http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=21&svalue={$totalPower}");
fclose($fp);?>
and added a crontab-e
Code: Select all
*/1 * * * * sudo /home/pi/domoticz/scripts/pannelli.php
Now the counters are red.. seems that something happens... but no numeric update..

Re: update virtual counter -help
Posted: Friday 20 May 2016 8:28
by bobkersten
Calling fopen on an url is not enough, it merely opens a file descriptor for the url you've specified. As fas as I know it doesn't actually read from the descriptor and thus your command will not get executed. That would explain the counters turning red (which happens if the counters are not updated for a prolonged period of time). For these simple url calls I'd usually use file_get_contents. You have to verify the results to see if the command was successful.
You're also unnecessarily mixing up curl and fopen, they are both means to access a resource. Stick to one, or rewrite those to file_get_contents as well.
Finally, Domoticz will calculate daily values for you; just provide totals and the dailies are generated automatically.
Try something like this (not tested ofcourse):
Code: Select all
#!/usr/bin/php
<?php
########## DATA SUNWAYS ##########
$url = "http://13.3.89.50/data/sys.txt";
$username = "customer";
$password = "00000000";
$context = stream_context_create( array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode( "{$username}:{$password}" )
)
));
$input = file_get_contents( $url, false, $context );
$results = explode('#', $input);
$actualPower = filter_var($results[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower = filter_var($results[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower = filter_var($results[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
########## PUSH DATA TO DOMOTICZ ########## */
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=20&nvalue=0&svalue={$actualPower};{$totalPower}" );
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=21&svalue={$totalPower}" );
?>
Re: update virtual counter -help
Posted: Friday 20 May 2016 8:43
by Mazzokun
Ops, sorry I discovered that i'm not on basic but DIGEST auth
I REWROTE THIS AND WORKS
Code: Select all
<?php
$login = 'customer';
$password = '00000000';
$url = 'http://13.3.89.50/data/sys.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
$results = explode('#', $result);
$actualPower = filter_var($results[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower = filter_var($results[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower = filter_var($results[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$actualKPower= $actualPower * 1000;
########## PUSH DATA TO DOMOTICZ ########## */
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=20&nvalue=0&svalue={$actualKPower};{$totalPower}" );
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command¶m=udevice&idx=21&svalue={$totalPower}" );
?>
the only issue is that cronjob doesn't work

Re: update virtual counter -help
Posted: Friday 20 May 2016 12:19
by Mazzokun
ok now all works!! thank you very very much!!

Re: update virtual counter -help
Posted: Saturday 21 May 2016 9:41
by Mazzokun
mmmh, there's something strange.. today the inverter has already generated 10 Kwh.. the today field in domoticz is=0 but the total and instantaneous value in the domoticz counter are updated... what the hell?

Re: update virtual counter -help
Posted: Monday 24 September 2018 22:56
by wouterbr
I own a subways AT 3600 inverter. I placed my Sunways script in /domoticz/scripts. I installed php7.2 php7.2-cli
and I get an error <?php..."]:1: unexpected symbol near '<'
any tips?
Wouter