Author |
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: December 29 2012 at 00:02 | IP Logged
|
|
|
Hey Guys,
does anyone know the Youless energymeter?
there is a wiki here
I bought one andwould like to get some data of the youless into PH...
The postman (who always rings twice... ) will deliver it monday 31st
found some php-code:
Code:
<?php
//////////////////////////////////////////////////////////// ////
// Script to read minute data of Youless te lezen
//
//
//
// Result showm as echo on screen
//////////////////////////////////////////////////////////// ////
$sserver='localhost';
$sdatabase_name='';
$susername='';
$spassword='';
//$con = mysql_connect($sserver, $susername, $spassword) or die('Could not connect: ' . mysql_error());
//$condatabase = mysql_select_db($sdatabase_name, $con) or die ('Could not select database' . mysql_error());
//////////////////////////////////////////////////////////// ////
//Bijhorende tabel
/*$sql="
CREATE TABLE IF NOT EXISTS `YL_data_minute` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`tijdstip` datetime DEFAULT NULL,
`meting` float DEFAULT NULL,
`kWh` float DEFAULT NULL,
`kWh_cumm` float DEFAULT NULL,
UNIQUE KEY `id` (`id`),
KEY `YL_minute` (`tijdstip`)
)";
$result = mysql_query($sql) or die("Query failed. create table ".mysql_error());
*/
//////////////////////////////////////////////////////////// ////
// todo: IP nummer aanpassen aan eigen IP nummer
//
// Youless (options):
// h=2 => minuut data van 60-30 minuten geleden (30 records)
// w=1, w=2 of w=3 => 10 minutes data van 0-8 hrs, 8-16 hrs en 16-24 hrs ago (48 records)
// d=0, d=1, d=2, d=.. => hour data van 0, 1, 2, .. days ago (24 records)
/////////// //////////////////////////////////////////////////////////// ////
// Read data per minute of the last half hour (30 records)
$url="http://192.168.0.14/V?h=1&f=j";
$handle=fopen($url, "r");
$json=fgets($handle, 1000000);
fclose($handle);
echo "$json <HR>";
$richd=json_decode($json);
$tm=$richd->tm; // tijdstip
$dt=$richd->dt; // interval
$value=$richd->val; // waarden (array)
$base_tijdstip=strtotime($tm); // Eerste tijd
$last_tijdstip=strtotime((count($value)-2)*$dt." second", $base_tijdstip); //bepaal laatste tijd (ignore null record)
echo "Base: ".date('Y-m-d H:i:s', $base_tijdstip)."<BR>
Last: ".date('Y-m-d H:i:s', $last_tijdstip)."<HR>";
$max_tijdstip=0;
$cumm=0;
////////////////////////////////////////////////////////////////////////
//// SQL om te bepalen wat de laatste waarde is die in de database staat
//////// //////////////////////////////////////////////////////////// ////
//$sql= "select date_format(tijdstip, '%Y-%m-%d %H:%i:%s'), kWh_cumm from YL_data_minute order by tijdstip desc Limit 0,1";
//$result = mysql_query($sql) or die("Query failed. last YL data ".mysql_error());
//$row = mysql_fetch_array($result);
//$max_tijdstip=strtotime($row[0]);
//$cumm=$row[1];
//echo date('Y-m-d H:i:s', $max_tijdstip)." ($max_tijdstip) - $cumm<HR>";
for ($i=0;$i<count($value)-1;$i++){ // Lees de meetwaarden in de array
$tijdstip=strtotime($i*$dt." second", $base_tijdstip); //Tijdstip is base+i*delta
if ($tijdstip>$max_tijdstip) { // Alleen als record nog niet bestaat
$v=str_replace(",",".",$value [$i]); // Mijn database wil . ipv ,
if ($v=="*") $v=0; // Indien geen meetwaarde aanwezig, meetwaarde=0
$kWh=$v/1000*$dt/3600; // omrekenen naar kWh
$cumm+=$kWh; // Cummulatief bijwerken
$sql= "insert into YL_data_minute values(null, str_to_date('$tm', '%Y-%m-%dT%H:%i:%s') + interval $i*$dt second, $v, $kWh, $cumm);";
// &nbs p;$result = mysql_query($sql) or die("Query failed. YL insert ".mysql_error());
echo "loaded: $i, $tm, $dt -> ".date('Y-m-d H:i', $tijdstip).", $value[$i]<br>";
}
else echo "rejected: $i, $tm, $dt -> ".date('Y-m-d H:i', $tijdstip).", $value[$i]<br>";
}
?>
|
|
|
was found here: http://gathering.tweakers.net/forum/list_messages/1494214 (dutch language)
and this short piece to get the total readout of the meter:
Code:
<?php
///Get Youless data
$url="http://192.168.0.14/a?f=j";
$handle=fopen($url, "r");
$json="";
$json=fgets($handle, 1000000);
$Youless= json_decode($json);
$YL_meterstand=1000*str_replace(",",".",$Youless->cnt);
echo "Youless meterstand: ".$YL_meterstand;
?>
|
|
|
Edited by krommetje - December 29 2012 at 00:17
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: December 31 2012 at 09:23 | IP Logged
|
|
|
Hey guys,
need your help for starters I would like to import the
current powerconsumption using http://192.168.1.14/a or with http://192.168.1.14/a?f=j
for some reason my code returns only 0
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: January 04 2013 at 22:14 | IP Logged
|
|
|
Peter,
Would need to see your code to know why you're getting a
0. Since it looks like this meter has a web interface,
you can probably scrape the data using the URLScraper
plugin.
To get going though, I would run this function:
ph_geturl("http://192.168.1.14/a?f=j")
and post the results.
Dave.
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: January 05 2013 at 03:40 | IP Logged
|
|
|
The result is:
Code:
HTTP/1.0 200 OK
Content-Type: application/json
{"cnt":"134,828","pwr":983,"lvl":21,"dev":"(±8%)","de t":"","con":"OK","sts":"(08)","raw":88}
|
|
|
The CNT is the metercount since startup
The pwr is the current powerconsumption
The LVL is the lightlevel which is reflected by the meter
The DEV is the lightlevel which is responsisble for detecting a puls
the rest is of no importance, been trying to get the powerconsumption by putting the ph_url function to [LOCAL1]
and then with ph_setglobal_s("YOULESSTEMP1",ph_regexdiff1("kWh,Watt",",","[LOCAL1]",1,2,0,0,0)) but the string always is emtpy...
Peter
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: January 05 2013 at 11:16 | IP Logged
|
|
|
Peter,
Your ph_regexdiff1 function needs some work.
Particularly since the LOCAL1 var will contain double
quotes, you'll want to use ph_getvar_s(1,1) instead of "
[LOCAL1]" (variable substitution). You'll also want to
delimit your strings within the function with single
quotes instead of double quotes to work around this
issue.
I was going to bang it out real quick for you but the
biggest problem is your search strings. According to
your function, you're trying to return data between the
string "kWh" and "Watt" (I think) but neither of those
strings appear in the returned JSON data.
Dave.
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: January 05 2013 at 11:21 | IP Logged
|
|
|
Peter,
Reread your post and since you state you're interested in
returning the powerconsumption value (the value
identified by "pwr"), I went ahead and did the function
below that will do that for you:
Code:
ph_regexdiff1('"pwr":','"',ph_gevar_s(1,1),1,1,0,0,
0) |
|
|
Of course, this assumes that LOCAL1 contains the returned
data from the ph_geturl function.
Hope this helps,
Dave.
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: January 06 2013 at 03:42 | IP Logged
|
|
|
I am getting a synthax error:
here is the macro
Code:
insert into macroheader values ('HVACPWRCONS','HVACPWRCONS',0,0,1);
insert into macrodetail values ('HVACPWRCONS',1,15,'[LOCAL1]',NULL,'ph_geturl("http://192.168.1.14/a?f=j") ',0,'','');
insert into macrodetail values ('HVACPWRCONS',2,10,'YOULESSTEMP1',N ULL,'ph_regexdiff1(''"pw r":'',''"'',ph_gevar_s(1,1),1,1,0,0,0)',0,'','');
insert into macrodetail values ('HVACPWRCONS',3,36,'',NULL,'{YOULESSTEMP1}',0,'','');
|
|
|
I went on and changed the macro to this:
Code:
insert into macroheader values ('HVACPWRCONS','HVACPWRCONS',0,0,1);
insert into macrodetail values ('HVACPWRCONS',1,38,'',0,'ph_setvar_s(1,1,ph_geturl("http:// 192.168.1.14/a?f=j"))',0,'','');
insert into macrodetail values ('HVACPWRCONS',2,36,'',NULL,'ph_getvar_s(1,1)',0,'','');
insert into macrodetail values ('HVACPWRCONS',3,10,'YOULESSTEMP1',NULL,'ph_regexdiff1(''"pw r":'',''"'',ph_gevar_s(1,1),1,1,0,0,0)',0,'','');
insert into macrodetail values ('HVACPWRCONS',4,36,'',NULL,'{YOULESSTEMP1}',0,'','');
|
|
|
and got the same result:
Edited by krommetje - January 06 2013 at 04:00
|
Back to Top |
|
|
dhoward Admin Group
Joined: June 29 2001 Location: United States
Online Status: Offline Posts: 4447
|
Posted: January 06 2013 at 16:15 | IP Logged
|
|
|
Peter,
Didnt look at your second macro and just went with
troubleshooting the first. The problem is a typo (that I
originally made).
Within the ph_regexdiff1 function, the get variable
function should read ph_getvar_s instead of ph_gevar_s
(the "t" is missing).
Also, you'll (sometimes) have problems with the last line
of your macro unless you enclose {YOULESSTEMP1} within
single or double quotes (if YOULESSTEMP1 only contains a
number, you'll be ok. If it ever contains a string, it
will fail. Putting quotes around it fixes it for all
datatypes).
Dave.
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: January 07 2013 at 01:22 | IP Logged
|
|
|
Yep, it is working now, crazy that I did not find the typo Now I can go on and extend the macro with more functions like history etc. The usermessage was just used for checking what was in LOCAL1, it can be removed now.
The first thing I did was make this:
Now it would be nice to get the totalcount in here too but that is for later
Edited by krommetje - January 07 2013 at 03:06
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: January 07 2013 at 23:19 | IP Logged
|
|
|
Here is the same meter with the totalcount:
code used was:
ph_regexdiff1('"cnt":"','"',ph_getvar_s(1,1),1,1,0,0,0)
Edited by krommetje - January 07 2013 at 23:19
|
Back to Top |
|
|
krommetje Super User
Joined: December 29 2004 Location: Netherlands
Online Status: Offline Posts: 695
|
Posted: February 01 2013 at 07:53 | IP Logged
|
|
|
Here is some more work on the youless... what I've done is get a basic Chart-script and convert this for PH and added a calendar which says what the total used electricity is on each month and the used kWh so far for the running month.
to get this data into PH is pretty basic. On the last day of each month I put the metercount into a GV and subscract the last month from the totalcount.
here is a screenshot:
Edited by krommetje - February 01 2013 at 07:55
|
Back to Top |
|
|
|
|