Data from Register Variable to variables

I have a P1 connection with my smart meter.

The Register Variable is trowing data properly (see example) and I am able to read it. How to adapt the data so I am able to put the data in the created variables?

<?

// wenn das Skript von einer RegisterVariable-Instanz aus aufgerufen worden ist
if ($_IPS['SENDER'] == "RegisterVariable") {

// bereits im Puffer der Instanz vorhandene Daten in $data kopieren
$data  = RegVar_GetBuffer($_IPS['INSTANCE']);

// neu empfangene Daten an $data anhängen
$data .= $_IPS['VALUE'];



 //   $data2 .= $_IPS['VALUE'];


// QUESTION IS THIS A STRING OR A ARRAY ( I THINK STRING)
// echo     "test123";
// echo $_IPS['SENDER'];


// $lines = explode(")", $data);


$liness = preg_split('/\r\n|\r|\n/', $data);


if (in_array("1-0:1.8.2", $liness))
  {
  echo "Match found";
  }


// echo $lines[0]; // piece1
// echo $lines[1]; // piece2

// echo $lines; // piece1
// print_r($liness);


// var_dump(substr( $data, 2));        
// var_dump(substr( $data, 2));        


// var_dump($liness["1-0:1.7.0"]);

/// tester
// SetValue(11690 /*[Energie\Momentanverbrauch]*/ , $data);

////


// $data = explode(",", $_IPS['VALUE']);
// SetValue(12345, $data2[0]);
// SetValue(11690, $data[3]);

//     if str_starts_with($data, "0-1") {
// echo "BINGO"    }



		if (substr($data, 0, 10) == "0-1:24.2.1")   // Zählerstand gas
		{
			$ar = explode(")(",$data);
			SetValueFloat(55015 /*[Energie\Momentanverbrauch]*/ ,  floatval($ar[1]));
		}


Give the Variables one time manually an Ident like „1-0:72.7.0“

IPS_SetIdent($variableID, "1-0:72.7.0");

In the next step you could go through the $liness with foreach($liness as $k => $v) and set the Variables by

$id = IPS_GetObjectIDByIdent($parentCategoryID, $k);
SetValue($id, $v);

Maybe the format from $liness is wrong. So please sent us an output from print_r($liness);

thanks! The print_r output is:

14/08/2022, 12:43:16 | Register Variable    | Array
(
    [0] => 
    [1] => 1-0:31.7.0(004*A)
    [2] => 1-0:51.7.0(001*A)
    [3] => 1-0:71.7.0(003*A)
    [4] => 1-0:21.7.0(00.000*kW)
    [5] => 1-0:41.7.0(00.000*kW)
    [6] => 1-0:61.7.0(00.000*kW)
    [7] => 1-0:22.7.0(01.003*kW)
    [8] => 1-0:42.7.0(00.181*kW)
    [9] => 1-0:62.7.0(00.864*kW)
    [10] => 0-1:24.1.0(003)
    [11] => 0-1:96.1.0(4730303738353635353939383135313230)
    [12] => 0-1:24.2.1(220814124005S)(00010.454*m3)
    [13] => !B2E8
    [14] => 
)

I now updated the script to

<?

// if the script was called from a RegisterVariable instance
if ($_IPS['SENDER'] == "RegisterVariable") {

// Copy data already in the instances buffer to data
$data  = RegVar_GetBuffer($_IPS['INSTANCE']);

// Append newly received data to data
$data .= $_IPS['VALUE'];

// split Lines from RegisterVariable
$liness = preg_split('/\r\n|\r|\n/', $data);
// print_r($liness);


// Set identifier to the specific ObjectIDs
IPS_SetIdent(44599, "1-0:72.7.0");
IPS_SetIdent(59046, "0-0:96.14.0");
//later: continue IPS_SetIdent for every variable

foreach($liness as $k => $v);

$id = IPS_GetObjectIDByIdent($parentCategoryID, $k);
SetValue($id, $v);

}

Use IPS_SetIdent only ONE time. (Maybe in a seperate script).
You can check the correct Ident by changing the Table-Columns in the Object-Tree.

You have to seperate the $liness maybe something like this:

foreach($liness as $k){
  $v = explode("(", $k);
  $id = IPS_GetObjectIDByIdent($parentCategoryID /*set at script start */, $v[0]);
  $value = (str_replace(array(")", "*kW", "*m3"), "", $v[1]));
  SetValue($id, $value);
}

Looking at the debug of the RegisterVariable and documentation. I updated the script (to wrap data correctly and prevent broken lines etc. ) Data is now grouping and buffering the way I want.

// if the script was called from a RegisterVariable instance
if ($_IPS['SENDER'] == "RegisterVariable") {

// Copy data already in the instances buffer to data
$buf = RegVar_GetBuffer($_IPS['INSTANCE']);

// Append newly received data to $data
$buf .= $_IPS['VALUE']; 
 
// Write back remaining buffer > solution for broken datalines received. // ONLY OLD DATA IS SHOWN WHEN THIS IS WRONG

RegVar_SetBuffer($_IPS['INSTANCE'], $buf); 

// split to array
$lines = preg_split('/\r\n|\r|\n/', $buf);


Every batch of data starts with /ISK5\2M550T-1013 - followed by all information line by line.

1-0:1.8.1(000044.676kWh)
1-0:1.8.2(000046.373
kWh)
1-0:2.8.1(000123.223kWh)
1-0:2.8.2(000319.614
kWh)
0-0:96.14.0(0001)
etc etc.

I now am to trying to use the an easy filter to obtain the data.


    $meterstandelectra = array_filter($lines, function($key) {
       return strpos($key, '1-0:1.8.1') === 0;   // strpos = first positions in the key are en is strictly true ( === 0)
    }, ARRAY_FILTER_USE_BOTH);
// $latest = end($meterstandelectra);
echo $meterstandelectra;

or filter bu string position

if (strpos($buf, ‚0-0:1.0.0‘) === 0)
{
// $data in durch = separierte Datensätze zerlegen
$datasets = explode(‚)‘, $lines);
// $data auf den Inhalt des letzten (unvollständigen) Datensatzes setzen
$buf = $datasets[count($datasets) - 1];
echo „JAJASJ“;
// print_r( $datasets);
}

challenge is still to last is to clear the buffer (I get error messages because of the buffer size)