// Copyright (C) 2019-2025 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = amiplus
    meter_type     = ElectricityMeter
    default_fields = name,id,total_energy_consumption_kwh,current_power_consumption_kw,total_energy_production_kwh,current_power_production_kw,voltage_at_phase_1_v,voltage_at_phase_2_v,voltage_at_phase_3_v,total_energy_consumption_tariff_1_kwh,total_energy_consumption_tariff_2_kwh,total_energy_consumption_tariff_3_kwh,total_energy_production_tariff_1_kwh,total_energy_production_tariff_2_kwh,total_energy_production_tariff_3_kwh,timestamp
    detect {
        mvt = APA,02,02
        mvt = DEV,02,37
        mvt = DEV,00,02
        mvt = DEV,01,02
        mvt = NES,03,02
        // Apator Otus 1/3 — frames with APA decoded here, frames with APT unknown.
        mvt = APA,01,02
        mvt = APT,01,02
        // Apator Gama 350.
        mvt = EGM,01,02
    }
    fields {
        field {
            name           = total_energy_consumption
            quantity       = Energy
            info           = 'The total energy consumption recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
            }
        }
        field {
            name           = current_power_consumption
            quantity       = Power
            info           = 'Current power consumption.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = PowerW
            }
        }
        field {
            name           = total_energy_production
            quantity       = Energy
            info           = 'The total energy production recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                difvifkey = 0E833C
            }
        }
        field {
            name           = current_power_production
            quantity       = Power
            info           = 'Current power production.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                difvifkey = 0BAB3C
            }
        }
        field {
            name           = voltage_at_phase_1
            quantity       = Voltage
            info           = 'Voltage at phase L1.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Voltage
                add_combinable   = AtPhase1
            }
        }
        field {
            name           = voltage_at_phase_2
            quantity       = Voltage
            info           = 'Voltage at phase L2.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Voltage
                add_combinable   = AtPhase2
            }
        }
        field {
            name           = voltage_at_phase_3
            quantity       = Voltage
            info           = 'Voltage at phase L3.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Voltage
                add_combinable   = AtPhase3
            }
        }
        field {
            name         = device
            quantity     = PointInTime
            info         = 'Device date time.'
            display_unit = datetime
            match {
                measurement_type = Instantaneous
                vif_range        = DateTime
            }
        }
        field {
            name           = total_energy_consumption_tariff_1
            quantity       = Energy
            info           = 'The total energy consumption recorded by this meter on tariff 1.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                tariff_nr        = 1
            }
        }
        field {
            name           = total_energy_consumption_tariff_2
            quantity       = Energy
            info           = 'The total energy consumption recorded by this meter on tariff 2.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                tariff_nr        = 2
            }
        }
        field {
            name           = total_energy_consumption_tariff_3
            quantity       = Energy
            info           = 'The total energy consumption recorded by this meter on tariff 3.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                tariff_nr        = 3
            }
        }
        field {
            name           = total_energy_production_tariff_1
            quantity       = Energy
            info           = 'The total energy production recorded by this meter on tariff 1.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                difvifkey = 8E10833C
            }
        }
        field {
            name           = total_energy_production_tariff_2
            quantity       = Energy
            info           = 'The total energy production recorded by this meter on tariff 2.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                difvifkey = 8E20833C
            }
        }
        field {
            name           = total_energy_production_tariff_3
            quantity       = Energy
            info           = 'The total energy production recorded by this meter on tariff 3.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                difvifkey = 8E30833C
            }
        }
        field {
            name           = max_power_consumption
            quantity       = Power
            info           = 'The maximum demand indicator (maximum 15-min average power consumption recorded this month).'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Maximum
                vif_range        = AnyPowerVIF
            }
        }
        field {
            name           = total_reactive_energy_inductive
            quantity       = Reactive_Energy
            info           = 'The total inductive reactive energy recorded by this meter.'
            vif_scaling    = None
            dif_signedness = Signed
            display_unit   = kvarh
            force_scale    = 0.001
            match {
                difvifkey = 0EFB8273
            }
        }
        field {
            name           = total_reactive_energy_capacitive
            quantity       = Reactive_Energy
            info           = 'The total capacitive reactive energy recorded by this meter.'
            vif_scaling    = None
            dif_signedness = Signed
            display_unit   = kvarh
            force_scale    = 0.001
            match {
                difvifkey = 0EFB82F33C
            }
        }
        field {
            name           = reactive_power_inductive
            quantity       = Reactive_Power
            info           = 'Current inductive reactive power.'
            vif_scaling    = None
            dif_signedness = Signed
            display_unit   = kvar
            force_scale    = 0.001
            match {
                difvifkey = 0BFB14
            }
        }
        field {
            name           = reactive_power_capacitive
            quantity       = Reactive_Power
            info           = 'Current capacitive reactive power.'
            vif_scaling    = None
            dif_signedness = Signed
            display_unit   = kvar
            force_scale    = 0.001
            match {
                difvifkey = 0BFB943C
            }
        }
    }
    tests {
        test {
            args     = 'MyElectricity1 amiplus 10101010 NOKEY'
            telegram = 4E4401061010101002027A00004005_2F2F0E035040691500000B2B300300066D00790C7423400C78371204860BABC8FC100000000E833C8074000000000BAB3C0000000AFDC9FC0136022F2F2F2F2F
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity1","id":"10101010","total_energy_consumption_kwh":15694.05,"current_power_consumption_kw":0.33,"total_energy_production_kwh":7.48,"current_power_production_kw":0,"voltage_at_phase_1_v":236,"device_datetime":"2019-03-20 12:57","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity1;10101010;15694.05;0.33;7.48;0;236;null;null;null;null;null;null;null;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity2 amiplus 00254358 NOKEY'
            comment  = 'amiplus/apator electricity meter with three phase voltages'
            telegram = 5E44B6105843250000027A2A005005_2F2F0C7835221400066D404708AC2A400E032022650900000E833C0000000000001B2B9647000B2B5510000BAB3C0000000AFDC9FC0135020AFDC9FC0245020AFDC9FC0339020BABC8FC100000002F2F
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity2","id":"00254358","total_energy_consumption_kwh":9652.22,"current_power_consumption_kw":1.055,"total_energy_production_kwh":0,"current_power_production_kw":0,"voltage_at_phase_1_v":235,"voltage_at_phase_2_v":245,"voltage_at_phase_3_v":239,"max_power_consumption_kw":4.796,"device_datetime":"2021-10-12 08:07","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity2;00254358;9652.22;1.055;0;0;235;245;239;null;null;null;null;null;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity3 amiplus 86064864 NOKEY'
            comment  = 'amiplus/apator electricity meter with three phase voltages and 2 tariffs.'
            telegram = 804401066448068602027A000070052F2F_066D1E5C11DA21400C78644806868E10036110012500008E20038106531800008E10833C9949000000008E20833C8606000000001B2B5228020B2B3217000BAB3C0000000AFDC9FC0131020AFDC9FC0225020AFDC9FC0331020BABC8FC100000002F2F2F2F2F2F2F2F2F2F2F2F2FDE47
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity3","id":"86064864","current_power_consumption_kw":1.732,"current_power_production_kw":0,"voltage_at_phase_1_v":231,"voltage_at_phase_2_v":225,"voltage_at_phase_3_v":231,"device_datetime":"2022-01-26 17:28","total_energy_consumption_tariff_1_kwh":25011.061,"total_energy_consumption_tariff_2_kwh":18530.681,"total_energy_production_tariff_1_kwh":4.999,"total_energy_production_tariff_2_kwh":0.686,"max_power_consumption_kw":22.852,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity3;86064864;null;1.732;null;0;231;225;231;25011.061;18530.681;null;4.999;0.686;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity4 amiplus 55090884 NOKEY'
            comment  = 'amiplus/apator electricity meter with single phase voltage - Otus 1'
            telegram = 7E4401068408095501027A7C1070052F2F_066DDE5E150D39800C78840809550AFDC9FC0139028E30833C0000000000008E20833C0000000000008E10833C4301000000000BABC8FC100000008E10035336420200008E20030000000000008E30030000000000000B2B9502000BAB3C0000002F2F2F2F2F2F2F2F2F2F2F2F2F
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity4","id":"55090884","current_power_consumption_kw":0.295,"current_power_production_kw":0,"total_energy_consumption_tariff_1_kwh":2423.653,"total_energy_consumption_tariff_2_kwh":0,"total_energy_consumption_tariff_3_kwh":0,"total_energy_production_tariff_1_kwh":0.143,"total_energy_production_tariff_2_kwh":0,"total_energy_production_tariff_3_kwh":0,"voltage_at_phase_1_v":239,"device_datetime":"2024-09-13 21:30","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity4;55090884;null;0.295;null;0;239;null;null;2423.653;0;0;0.143;0;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity4 amiplus 00320787 NOKEY'
            telegram = 3e44b6108707320001027a380030052f2f0C7830253390066D6872141239400E031891690000000E833C9265010000000B2B2602000BAB3C0000002F2F2F2F
            json     = '{"_":"telegram","current_power_consumption_kw": 0.226,"current_power_production_kw": 0,"device_datetime": "2024-09-18 20:50","id": "00320787","media": "electricity","driver": "amiplus","name": "MyElectricity4","timestamp": "1111-11-11T11:11:11Z","total_energy_consumption_kwh": 699.118,"total_energy_production_kwh": 16.592}'
            fields   = 'MyElectricity4;00320787;699.118;0.226;16.592;0;null;null;null;null;null;null;null;null;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity5 amiplus 56914504 NOKEY'
            telegram = 9e4401060445915601027a3d0390052f2f066dc076091935800c78044591560e032088300000008e10032088300000008e20030000000000008e30030000000000008e8010030000000000000e833c2702000000008e10833c2702000000008e20833c0000000000008e30833c0000000000008e8010833c0000000000000afdc8fc0136240afdc8fc0262240afdc8fc0389222f2f2f2f2f2f2f2f2f2f2f2f
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity5","id":"56914504","total_energy_consumption_kwh":308.82,"total_energy_consumption_tariff_1_kwh":308.82,"total_energy_consumption_tariff_2_kwh":0,"total_energy_consumption_tariff_3_kwh":0,"total_energy_production_kwh":0.227,"total_energy_production_tariff_1_kwh":0.227,"total_energy_production_tariff_2_kwh":0,"total_energy_production_tariff_3_kwh":0,"voltage_at_phase_1_v":243.6,"voltage_at_phase_2_v":246.2,"voltage_at_phase_3_v":228.9,"device_datetime":"2024-05-25 09:54","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity5;56914504;308.82;null;0.227;null;243.6;246.2;228.9;308.82;0;0;0.227;0;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity6 amiplus 00086426 NOKEY'
            telegram = 8F44B3382664080003027A090080052F2F_066D37090E2232050C78266408000AFDC9FC0142020AFDC9FC0240020AFDC9FC0338028E30833C0000000000008E20833C0000000000008E10833C0000000000000BABC8FC100000008E10030750030000008E20035379060000008E30030000000000000B2B1307000BAB3C0000002F2F2F2F2F2F2F2F2F2F2F2F2F2F2F2F
            json     = '{"_": "telegram","current_power_consumption_kw": 0.713,"current_power_production_kw": 0,"device_datetime": "2025-02-02 14:09","id": "00086426","media": "electricity","driver": "amiplus","name": "MyElectricity6","timestamp": "1111-11-11T11:11:11Z","total_energy_consumption_tariff_1_kwh": 35.007,"total_energy_consumption_tariff_2_kwh": 67.953,"total_energy_consumption_tariff_3_kwh": 0,"total_energy_production_tariff_1_kwh": 0,"total_energy_production_tariff_2_kwh": 0,"total_energy_production_tariff_3_kwh": 0,"voltage_at_phase_1_v": 242,"voltage_at_phase_2_v": 240,"voltage_at_phase_3_v": 238}'
            fields   = 'MyElectricity6;00086426;null;0.713;null;0;242;240;238;35.007;67.953;0;0;0;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity7 amiplus 56859196 NOKEY'
            comment  = 'Apator Otus 3 reporting reactive energy and reactive power (registers under mfct specific FB VIFEs).'
            telegram = CE4401069691855601027AC600C005_2F2F066D4073124234800C78969185560E032050260600008E10032050260600000E833C9299930700008E10833C9299930700000EFB82736461630100008E10FB82736461630100000EFB82F33C9248050500008E10FB82F33C9248050500000B2B6800000BAB3C0300000BFB142303000BFB943C1803000AFDC8FC0197220AFDC8FC0231230AFDC8FC035123146DADAA423414ED3C80AB42340BFDDAFC011400000BFDDAFC023401000BFDDAFC030302002F2F2F2F2F2F2F2F2F2F2F2F2F2F
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity7","id":"56859196","current_power_consumption_kw":0.068,"current_power_production_kw":0.003,"reactive_power_capacitive_kvar":0.318,"reactive_power_inductive_kvar":0.323,"device_datetime":"2026-04-02 18:51","total_energy_consumption_kwh":6265.02,"total_energy_consumption_tariff_1_kwh":6265.02,"total_energy_production_kwh":7939.992,"total_energy_production_tariff_1_kwh":7939.992,"total_reactive_energy_capacitive_kvarh":5054.892,"total_reactive_energy_inductive_kvarh":1636.164,"voltage_at_phase_1_v":229.7,"voltage_at_phase_2_v":233.1,"voltage_at_phase_3_v":235.1,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity7;56859196;6265.02;0.068;7939.992;0.003;229.7;233.1;235.1;6265.02;null;null;7939.992;null;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'MyElectricity8 amiplus 30584551 fba95687613b73604c320a16aa8892ea'
            comment  = 'Apator Gama 350 (issue 2036) reporting reactive power. This meter does not send the reactive energy registers.'
            telegram = be44ed145145583001027ae700b005_a3c60e5d52ab6b2c2cfdd8368f408830f50f6d698041d61a76fd4e01988dde5d601f0e425682c43566156c932fc9d5661d01832707d90236c6ea2fddd6579a1533fe1b637082c2458939e96cc2cbd46410e6880a7990f7219a4b2c546b21eb2cddfda202aaf448ba01ab92e07ac68dd339d860b483fecdbd25851d938c002a3fe654875a0e6d8fa6692de3a66afc2dc3cff669d6a581ae7903f299e037ea81a7ae842fd4fe4ba4c9f5f01b60277a6861
            json     = '{"_":"telegram","media":"electricity","driver":"amiplus","name":"MyElectricity8","id":"30584551","current_power_consumption_kw":0.445,"current_power_production_kw":0,"reactive_power_capacitive_kvar":0.434,"reactive_power_inductive_kvar":0,"device_datetime":"2026-07-28 00:40","total_energy_consumption_kwh":15948.218,"total_energy_consumption_tariff_1_kwh":15948.218,"total_energy_consumption_tariff_2_kwh":0,"total_energy_consumption_tariff_3_kwh":0,"total_energy_production_kwh":0.023,"total_energy_production_tariff_1_kwh":0.023,"total_energy_production_tariff_2_kwh":0,"total_energy_production_tariff_3_kwh":0,"voltage_at_phase_1_v":236,"voltage_at_phase_2_v":235,"voltage_at_phase_3_v":238,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'MyElectricity8;30584551;15948.218;0.445;0.023;0;236;235;238;15948.218;0;0;0.023;0;0;1111-11-11 11:11.11'
        }
    }
}