Problem with php-scripts for energy & power values

P1 interface and others smart meters
Post Reply
JKSU
Posts: 6
Joined: Sun Aug 30, 2020 8:38 pm

Problem with php-scripts for energy & power values

Post by JKSU » Sun Aug 30, 2020 9:23 pm

Hello,

I created php-scripts (first scripts ever ;) ) to create the json string as meterN input [ID(VALUE*UNIT)], extractng the corresponding value from an http port. The smart meter values are sent to the http port by vzlogger.

In my opinion the php-script output is correct, but meterN says it's invalid using the test command button.

As example the script for the actual power:

Code: Select all

*********************************************************************************************************************
<?php

// This script will output a meterN compatible format for the house actual power (1-0:16.7.0) main command

// create curl resource
	$ch = curl_init();

// set url
    curl_setopt($ch, CURLOPT_URL, "http://localhost:8081/0dd0410c-7aaf-11ea-8e60-03ff29a5b0c2");

//return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//	set HTTP-Request-Method to GET
	curl_setopt($ch,CURLOPT_HTTPGET, 1);
	
// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
        curl_close($ch);      

// convert $output in utf8 coded for 'json_decode' 
$output = utf8_encode($output);

// decode json string into array
$results = json_decode($output,true); 

//extract actual power value
$power_act = $results['data'][0]['tuples'][0][1];
$power_act = round($power_act, 1);

// create string for meterN
$str = utf8_decode("1-0:16.7.0($power_act*W)\n");
echo "$str";

// outputs for tests
# echo $results['data'][0]['tuples'][0][1];
# echo $output;

?>
*********************************************************************************************************************
respose using the meterN test button:
Command : php /var/www/html/metern/power_actual.php

1-0:16.7.0(579*W) is not valid, the correct format is 1-0:16.7.0(1234.5*W)

For the input/output energy it's the same but without the hint "...the correct format is".

Example of json string from http port 8081:

{ "version": "0.8.0", "generator": "vzlogger", "data": [ { "uuid": "0dd0410c-7aaf-11ea-8e60-03ff29a5b0c2", "last": 1598818757665, "interval": -1, "protocol": "sml", "tuples": [ [ 1598818757665, 579 ] ] } ] }

Who can give me a hint to solve my problem.

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: Problem with php-scripts for energy & power values

Post by jeanmarc » Mon Aug 31, 2020 4:29 am

Hi,
Great job :thumbup: just change :

Code: Select all

$str = utf8_decode("1-0:16.7.0($power_act*W)\n");
echo "$str";
for
echo "1-0:16.7.0($power_act*W)\n";

JKSU
Posts: 6
Joined: Sun Aug 30, 2020 8:38 pm

Re: Problem with php-scripts for energy & power values

Post by JKSU » Mon Aug 31, 2020 9:32 am

Thanks for the help jeanmarc,

but using the test button in meterN, the response is unfortunately the same as before.

My actual power value has no postion afte the decimal point.

But even if I replace " $power_act" with "e.g. "901.5" in

echo "1-0:16.7.0(901.5*W)\n";

the response using the test button is the same:

Command : php /var/www/html/metern/power_actual.php
1-0:16.7.0(901.5*W) is not valid, the correct format is 1-0:16.7.0(1234.5*W)

For me it seems to ber a valid json string.

Do you have further proposals for me?

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: Problem with php-scripts for energy & power values

Post by jeanmarc » Mon Aug 31, 2020 10:36 am

Did you use latest version ? You choose Json as Dataset format or IEC62056 ?

JKSU
Posts: 6
Joined: Sun Aug 30, 2020 8:38 pm

Re: Problem with php-scripts for energy & power values

Post by JKSU » Mon Aug 31, 2020 11:24 am

I use vesion meterN 1.2.3 (should be the newest version) an in main config I selected:
-Dataset format: JSON
- Debug: Yes
- Decimal mark: Dot

How can I add screenshots to the discussion in this forum?

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: Problem with php-scripts for energy & power values

Post by jeanmarc » Mon Aug 31, 2020 11:49 am

Ok 1-0:16.7.0(901.5*W) is a IEC62056 format.
For JSON, it should be {"1-0:16.7.0": 579}

So in php :

Code: Select all

 echo "{\"1-0:16.7.0\":$power_act}\n";
You can't put image on the forum, sorry but i'am limited with the hosting and i can't even cover the cost of running the it with ads.
You should use a link to another hosting service.

PS: Please don't put your com app in /var/www/html/metern/ folder but into /var/www/html/comapps/ or something.
PS2: Yes there is a glitch while testing live command using JSON dataset

JKSU
Posts: 6
Joined: Sun Aug 30, 2020 8:38 pm

Re: Problem with php-scripts for energy & power values

Post by JKSU » Mon Aug 31, 2020 1:17 pm

ok, thanks.

Now it's working correctly, fine, great!!!

For me (as beginner of this topic) the explanation in "How-to" for the rquired string format (ID(VALUE*UNIT)) was a little bit confusing. I thought that is the reuqired jason format.

I hope it helps other people too.

jeanmarc
Posts: 2134
Joined: Thu Aug 29, 2013 7:16 am

Re: Problem with php-scripts for energy & power values

Post by jeanmarc » Mon Aug 31, 2020 1:32 pm

Sorry for the misunderstanding, i've correct the output for JSON.

PS for your script :

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted'); // Avoid request for anyone using HTTP
}
and do
ln -s /srv/http/comapps/yourscript.php /usr/bin/yourscript and chmod +x yourscript.php :thumbup:

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest