SMA Energy Meter

Vielleicht hilft euch meine Umsetzung weiter:


<?PHP
declare(strict_types=1);

// OBIS Parameter
$list_sum   = []; //Summen
$list_sum['00010400'] = ['OBIS' => '0140', 'divisor' => 10, 'name' => 'Real Power +'];
$list_sum['00010800'] = ['OBIS' => '0180', 'divisor' => 3600000, 'name' => 'Counter Real Power +'];
$list_sum['00020400'] = ['OBIS' => '0240', 'divisor' => 10, 'name' => 'Real Power -'];
$list_sum['00020800'] = ['OBIS' => '0280', 'divisor' => 3600000, 'name' => 'Counter Real Power -'];
$list_sum['00030400'] = ['OBIS' => '0340', 'divisor' => 10, 'name' => 'Reactive Power +'];
$list_sum['00030800'] = ['OBIS' => '0380', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power +'];
$list_sum['00040400'] = ['OBIS' => '0440', 'divisor' => 10, 'name' => 'Reactive Power -'];
$list_sum['00040800'] = ['OBIS' => '0480', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power -'];
$list_sum['00090400'] = ['OBIS' => '0940', 'divisor' => 10, 'name' => 'Apparent Power +'];
$list_sum['00090800'] = ['OBIS' => '0980', 'divisor' => 3600000, 'name' => 'Counter Apparent Power +'];
$list_sum['000a0400'] = ['OBIS' => '1040', 'divisor' => 10, 'name' => 'Apparent Power -'];
$list_sum['000a0800'] = ['OBIS' => '1080', 'divisor' => 3600000, 'name' => 'Counter Apparent Power -'];
$list_sum['000d0400'] = ['OBIS' => '1340', 'divisor' => 1000, 'name' => 'Power Faktor'];
$list_sum['000e0400'] = ['OBIS' => '1440', 'divisor' => 1000, 'name' => 'Network Frequency'];

$list_l1   = []; //Phase 1
$list_l1['00150400'] = ['OBIS' => '2140', 'divisor' => 10, 'name' => 'Real Power +'];
$list_l1['00150800'] = ['OBIS' => '2180', 'divisor' => 3600000, 'name' => 'Counter Real Power +'];
$list_l1['00160400'] = ['OBIS' => '2240', 'divisor' => 10, 'name' => 'Real Power -'];
$list_l1['00160800'] = ['OBIS' => '2280', 'divisor' => 3600000, 'name' => 'Counter Real Power -'];
$list_l1['00170400'] = ['OBIS' => '2340', 'divisor' => 10, 'name' => 'Reactive Power +'];
$list_l1['00170800'] = ['OBIS' => '2380', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power +'];
$list_l1['00180400'] = ['OBIS' => '2440', 'divisor' => 10, 'name' => 'Reactive Power -'];
$list_l1['00180800'] = ['OBIS' => '2480', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power -'];
$list_l1['001d0400'] = ['OBIS' => '2940', 'divisor' => 10, 'name' => 'Apparent Power +'];
$list_l1['001d0800'] = ['OBIS' => '2980', 'divisor' => 3600000, 'name' => 'Counter Apparent Power +'];
$list_l1['001e0400'] = ['OBIS' => '3040', 'divisor' => 10, 'name' => 'Apparent Power -'];
$list_l1['001e0800'] = ['OBIS' => '3080', 'divisor' => 3600000, 'name' => 'Counter Apparent Power -'];
$list_l1['001f0400'] = ['OBIS' => '3140', 'divisor' => 1, 'name' => 'Power'];
$list_l1['00200400'] = ['OBIS' => '3240', 'divisor' => 1000, 'name' => 'Voltage'];
$list_l1['00210400'] = ['OBIS' => '3340', 'divisor' => 1000, 'name' => 'Network Frequency'];

$list_l2   = []; //Phase 2
$list_l2['00290400'] = ['OBIS' => '4140', 'divisor' => 10, 'name' => 'Real Power +'];
$list_l2['00290800'] = ['OBIS' => '4180', 'divisor' => 3600000, 'name' => 'Counter Real Power +'];
$list_l2['002a0400'] = ['OBIS' => '4240', 'divisor' => 10, 'name' => 'Real Power -'];
$list_l2['002a0800'] = ['OBIS' => '4280', 'divisor' => 3600000, 'name' => 'Counter Real Power -'];
$list_l2['002b0400'] = ['OBIS' => '4340', 'divisor' => 10, 'name' => 'Reactive Power +'];
$list_l2['002b0800'] = ['OBIS' => '4380', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power +'];
$list_l2['002c0400'] = ['OBIS' => '4440', 'divisor' => 10, 'name' => 'Reactive Power -'];
$list_l2['002c0800'] = ['OBIS' => '4480', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power -'];
$list_l2['00310400'] = ['OBIS' => '4940', 'divisor' => 10, 'name' => 'Apparent Power +'];
$list_l2['00310800'] = ['OBIS' => '4980', 'divisor' => 3600000, 'name' => 'Counter Apparent Power +'];
$list_l2['00320400'] = ['OBIS' => '5040', 'divisor' => 10, 'name' => 'Apparent Power -'];
$list_l2['00320800'] = ['OBIS' => '5080', 'divisor' => 3600000, 'name' => 'Counter Apparent Power -'];
$list_l2['00330400'] = ['OBIS' => '5140', 'divisor' => 1, 'name' => 'Power'];
$list_l2['00340400'] = ['OBIS' => '5240', 'divisor' => 1000, 'name' => 'Voltage'];
$list_l2['00350400'] = ['OBIS' => '5340', 'divisor' => 1000, 'name' => 'Network Frequency'];

$list_l3   = []; //Phase 3
$list_l3['003d0400'] = ['OBIS' => '4140', 'divisor' => 10, 'name' => 'Real Power +'];
$list_l3['003d0800'] = ['OBIS' => '4180', 'divisor' => 3600000, 'name' => 'Counter Real Power +'];
$list_l3['003e0400'] = ['OBIS' => '4240', 'divisor' => 10, 'name' => 'Real Power -'];
$list_l3['003e0800'] = ['OBIS' => '4280', 'divisor' => 3600000, 'name' => 'Counter Real Power -'];
$list_l3['003f0400'] = ['OBIS' => '4340', 'divisor' => 10, 'name' => 'Reactive Power +'];
$list_l3['003f0800'] = ['OBIS' => '4380', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power +'];
$list_l3['00400400'] = ['OBIS' => '4440', 'divisor' => 10, 'name' => 'Reactive Power -'];
$list_l3['00400800'] = ['OBIS' => '4480', 'divisor' => 3600000, 'name' => 'Counter ReReactive Power -'];
$list_l3['00450400'] = ['OBIS' => '4940', 'divisor' => 10, 'name' => 'Apparent Power +'];
$list_l3['00450800'] = ['OBIS' => '4980', 'divisor' => 3600000, 'name' => 'Counter Apparent Power +'];
$list_l3['00460400'] = ['OBIS' => '5040', 'divisor' => 10, 'name' => 'Apparent Power -'];
$list_l3['00460800'] = ['OBIS' => '5080', 'divisor' => 3600000, 'name' => 'Counter Apparent Power -'];
$list_l3['00470400'] = ['OBIS' => '5140', 'divisor' => 1, 'name' => 'Power'];
$list_l3['00480400'] = ['OBIS' => '5240', 'divisor' => 1000, 'name' => 'Voltage'];
$list_l3['00490400'] = ['OBIS' => '5340', 'divisor' => 1000, 'name' => 'Network Frequency'];

if ($_IPS['SENDER'] === 'Execute'){
    //zum Testen
    $hraw = '534d4100000402a000000001024c001060690174b33a68d524a6a271000104000000000000010800000000005628897000020400000075be00020800000000037a39805000030400000000000003080000000000657b04c0000404000000093d0004080000000000436dbad00009040000000000000908000000000065e92c48000a04000000761b000a08000000000388d3e3f8000d0400000003e5000e04000000c359001504000000000000150800000000000deb6e9800160400000029d8001608000000000145ab1fd00017040000000000001708000000000020aa986000180400000003c700180800000000001e3635d0001d040000000000001d08000000000017db2560001e040000002a04001e0800000000014b33af58001f0400000012710020040000037ecc00210400000003e4002904000000000000290800000000003c4ae980002a040000002524002a0800000000010e795978002b040000000000002b0800000000002cedd9c0002c04000000023f002c08000000000012d341d800310400000000000031080000000000407691a80032040000002535003208000000000115096da0003304000000109a0034040000037fbc00350400000003e6003d040000000000003d0800000000001747a358003e0400000026c3003e080000000001316a7908003f040000000000003f0800000000002142e340004004000000033700400800000000001bc49530004504000000000000450800000000001fa4e6a000460400000026e5004608000000000136d71f6800470400000011340048040000037b7200490400000003e5900000000203055200000000';
} else {
    // Prepare data
    $hraw = bin2hex(RegVar_GetBuffer($_IPS['INSTANCE']) . $_IPS['VALUE']); // im Puffer der Instanz vorhandene Daten holen und um die letzten Werte ergänzen
}

//Zählerkennung
$offset = 0;
$len = 4;
//echo hexToStr(substr($hraw, $offset * 2, $len * 2)) . PHP_EOL;

//ProtokollID
$offset = 16;
$len = 2;
//echo (substr($hraw, $offset * 2, $len * 2)) . PHP_EOL;

//gruppe
$offset = 8;
$len = 2;
//echo (substr($hraw, $offset * 2, $len * 2)) . PHP_EOL;

//zaehlerkennung
$offset = 18;
$len = 6;
//echo (substr($hraw, $offset * 2, $len * 2)) . PHP_EOL;

//Messzeitpunkt
$offset = 24;
$len = 4;
//echo (substr($hraw, $offset * 2, $len * 2)) . PHP_EOL;

$offset += $len;
$finished = false;

while (!$finished){

    //obis Id
    $len = 4;
    $id = substr($hraw, $offset * 2, $len * 2);
    if ($id === '00000000'){
        $finished = true;
        continue;
    }
    $offset += $len;


    //obis Messwert
    $len = (int) substr($id, 2 * 2, 2); //die Länge entspricht der Messart (Byte 2)

    if (isset($list_sum[$id])){
        $value = substr($hraw, $offset * 2, $len * 2);
        $res_sum[$list_sum[$id]['name']] = base_convert(substr($hraw, $offset * 2, $len * 2), 16, 10) / $list_sum[$id]['divisor'];
    } elseif (isset($list_l1[$id])) {
        $value = substr($hraw, $offset * 2, $len * 2);
        $res_l1[$list_l1[$id]['name']] = base_convert(substr($hraw, $offset * 2, $len * 2), 16, 10) / $list_l1[$id]['divisor'];
    } elseif (isset($list_l2[$id])) {
        $value = substr($hraw, $offset * 2, $len * 2);
        $res_l2[$list_l2[$id]['name']] = base_convert(substr($hraw, $offset * 2, $len * 2), 16, 10) / $list_l2[$id]['divisor'];
    } elseif (isset($list_l3[$id])) {
        $value = substr($hraw, $offset * 2, $len * 2);
        $res_l3[$list_l3[$id]['name']] = base_convert(substr($hraw, $offset * 2, $len * 2), 16, 10) / $list_l3[$id]['divisor'];
    } elseif($id = '90000000') {
        $len = 4;
        $value = substr($hraw, $offset * 2, $len * 2);
        //echo sprintf('%s Softwareversion: %s', $id, $value) . PHP_EOL;
    } else {
        trigger_error ("$id unbekannt");
        $finished = true;
    }
    $offset += $len;

}

// zum Testen
if ($_IPS['SENDER'] === 'Execute'){

    print_r($res_sum);
    print_r($res_l1);
    print_r($res_l2);
    print_r($res_l3);
    return;
}

SetChangedValueFloat(20807, $res_l1['Real Power +']); //Bezug L1
SetChangedValueFloat(35978, $res_l2['Real Power +']); //Bezug L2
SetChangedValueFloat(49204, $res_l1['Real Power +']); //Bezug L3
SetChangedValueFloat(24942, $res_sum['Real Power +']); //Bezug gesamt
SetChangedValueFloat(43989, $res_sum['Counter Real Power +']); //Bezug Zähler

SetChangedValueFloat(44961, $res_l1['Real Power -']); //Einspeisung L1
SetChangedValueFloat(58571, $res_l2['Real Power -']); //Einspeisung L2
SetChangedValueFloat(31960, $res_l3['Real Power -']); //Einspeisung L3
SetChangedValueFloat(35568, $res_sum['Real Power -']); //Einspeisung gesamt
SetChangedValueFloat(47491, $res_sum['Counter Real Power -']); //Einspeisung Zähler

SetChangedValueFloat(32048, max(GetValueFloat(21200) - $res_sum['Real Power -'], 0)); //Eigenversorgung
SetChangedValueFloat(34179, max(GetValueFloat(21200) + $res_sum['Real Power +'] - $res_sum['Real Power -'], 0)); //Verbrauch

function SetChangedValueFloat(int $id, float $value){
    if (GetValueFloat($id) !== $value){
        SetValueFloat($id, $value);
    }
}


function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

Burkhard