update virtual counter -help

Moderator: leecollings

Post Reply
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

update virtual counter -help

Post 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?
User avatar
bobkersten
Posts: 34
Joined: Tuesday 06 October 2015 9:17
Target OS: Linux
Domoticz version: beta
Location: Venray
Contact:

Re: update virtual counter -help

Post 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&param=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.
Mac mini w/ ESXi running Ubuntu w/ Domoticz custombuild, SolarEdge 3500Wp, S0 Meter, P1 Smart Meter, RFXtrx433e, Aeotec Z-Stick Gen5
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: update virtual counter -help

Post 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&param=udevice&idx=20&nvalue=0&svalue={$actualPower};{$dailyPower}");
fopen ("http://13.3.89.3:8080/json.htm?type=command&param=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.. :twisted:
User avatar
bobkersten
Posts: 34
Joined: Tuesday 06 October 2015 9:17
Target OS: Linux
Domoticz version: beta
Location: Venray
Contact:

Re: update virtual counter -help

Post 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&param=udevice&idx=20&nvalue=0&svalue={$actualPower};{$totalPower}" );
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command&param=udevice&idx=21&svalue={$totalPower}" );
?>
Mac mini w/ ESXi running Ubuntu w/ Domoticz custombuild, SolarEdge 3500Wp, S0 Meter, P1 Smart Meter, RFXtrx433e, Aeotec Z-Stick Gen5
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: update virtual counter -help

Post by Mazzokun »

Ops, sorry I discovered that i'm not on basic but DIGEST auth :oops:
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&param=udevice&idx=20&nvalue=0&svalue={$actualKPower};{$totalPower}" );
file_get_contents( "http://13.3.89.3:8080/json.htm?type=command&param=udevice&idx=21&svalue={$totalPower}" );

?>

the only issue is that cronjob doesn't work :twisted:
Last edited by Mazzokun on Friday 20 May 2016 9:11, edited 2 times in total.
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: update virtual counter -help

Post by Mazzokun »

ok now all works!! thank you very very much!! ;) :mrgreen:
Mazzokun
Posts: 89
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: update virtual counter -help

Post 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? :shock: :?
wouterbr
Posts: 3
Joined: Monday 24 September 2018 22:50
Target OS: OS X
Domoticz version:
Contact:

Re: update virtual counter -help

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest