// Copyright (C) 2021 Vincent Privat (gpl-3.0-or-later)
// Copyright (C) 2022 Fredrik Öhrström (gpl-3.0-or-later)
// Diehl Sharky 774 heat meter.
driver {
    name           = sharky774
    meter_type     = HeatMeter
    default_fields = name,id,total_energy_consumption_kwh,energy_at_set_date_kwh,set_date,timestamp
    detect {
        mvt = DME,41,04
        mvt = DME,41,0d
        mvt = DME,41,0c
    }
    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           = total_cooling_consumption
            quantity       = Energy
            info           = 'The total cooling energy consumption recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                tariff_nr        = 1
            }
        }
        field {
            name           = total_volume
            quantity       = Volume
            info           = 'The total volume recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyVolumeVIF
            }
        }
        field {
            name           = total_cooling_volume
            quantity       = Volume
            info           = 'The total cooling volume recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyVolumeVIF
                tariff_nr        = 2
            }
        }
        field {
            name           = volume_flow
            quantity       = Flow
            info           = 'The current flow.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = VolumeFlow
            }
        }
        field {
            name           = power
            quantity       = Power
            info           = 'The power.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyPowerVIF
            }
        }
        field {
            name           = flow_temperature
            quantity       = Temperature
            info           = 'The flow temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = FlowTemperature
            }
        }
        field {
            name           = return_temperature
            quantity       = Temperature
            info           = 'The return temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = ReturnTemperature
            }
        }
        field {
            name           = operating_time
            quantity       = Time
            info           = 'How long the meter has been collecting data.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = OperatingTime
            }
        }
        field {
            name           = operating_time_in_error
            quantity       = Time
            info           = 'How long the meter has been in an error state and not collected data.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = OperatingTime
                add_combinable   = RecordErrorCodeMeterToController
            }
        }
        field {
            name           = energy_at_set_date
            quantity       = Energy
            info           = 'The total energy consumption recorded by this meter at the set date.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                storage_nr       = 1
            }
        }
        field {
            name           = cooling_at_set_date
            quantity       = Energy
            info           = 'The total cooling energy consumption recorded by this meter at the set date.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                storage_nr       = 1
                tariff_nr        = 1
            }
        }
        field {
            name           = set
            quantity       = PointInTime
            info           = 'The last billing set date.'
            vif_scaling    = Auto
            dif_signedness = Signed
            display_unit   = DateLT
            match {
                measurement_type = Instantaneous
                vif_range        = Date
                storage_nr       = 1
            }
        }
    }
    tests {
        test {
            args     = 'Heato sharky774 58496405 NOKEY'
            telegram = 3E44A5110564495841047A700030052F2F_0C06846800000C13195364000B3B0400000C2B110100000A5A17050A5E76020AA61800004C0647630000426CBF25
            json     = '{"_":"telegram","media":"heat","meter":"sharky774","name":"Heato","id":"58496405","total_energy_consumption_kwh":6884,"total_volume_m3":645.319,"volume_flow_m3h":0.004,"power_kw":0.111,"flow_temperature_c":51.7,"return_temperature_c":27.6,"operating_time_in_error_h":0,"energy_at_set_date_kwh":6347,"set_date":"2021-05-31","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heato;58496405;6884;6347;2021-05-31;1111-11-11 11:11.11'
        }
        test {
            args     = 'diehl_meter sharky774 52173898 NOKEY'
            telegram = 3E44A51198381752410C7AA80030052F2F_0C06105104000C13093835020B3B9401000C2B342600000A5A12060A5E91040AA61800004C0641460400426CFF21
            json     = '{"_":"telegram","media":"heat volume at inlet","meter":"sharky774","name":"diehl_meter","id":"52173898","total_energy_consumption_kwh":45110,"total_volume_m3":2353.809,"volume_flow_m3h":0.194,"power_kw":2.634,"flow_temperature_c":61.2,"return_temperature_c":49.1,"operating_time_in_error_h":0,"energy_at_set_date_kwh":44641,"set_date":"2023-01-31","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'diehl_meter;52173898;45110;44641;2023-01-31;1111-11-11 11:11.11'
        }
        test {
            args     = 'Heatoo sharky774 72615127 NOKEY'
            comment  = 'This test telegram has more historical data!'
            telegram = 5E44A5112751617241047A8B0050052F2F0C0E000000000C13010000000B3B0000000C2B000000000A5A26020A5E18020B260321000AA6180000C2026CBE2BCC020E00000000CC021301000000DB023B000000DC022B000000002F2F2F2F2F
            json     = '{"_":"telegram","media":"heat","meter":"sharky774","name":"Heatoo","id":"72615127","total_energy_consumption_kwh":0,"total_volume_m3":0.001,"volume_flow_m3h":0,"power_kw":0,"flow_temperature_c":22.6,"return_temperature_c":21.8,"operating_time_h":2103,"operating_time_in_error_h":0,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heatoo;72615127;0;null;null;1111-11-11 11:11.11'
        }
        test {
            args     = 'Heatooo sharky774 61243590 NOKEY'
            comment  = 'This telegram contains a negative power value encoded in bcd.'
            telegram = 3E44A5119035246141047A1A0030052F2F_0C06026301000C13688609040B3B0802000C2B220000F00A5A71020A5E72020AA61800004C0636370100426CBF25
            json     = '{"_":"telegram","media":"heat","meter":"sharky774","name":"Heatooo","id":"61243590","total_energy_consumption_kwh":16302,"total_volume_m3":4098.668,"volume_flow_m3h":0.208,"power_kw":-0.022,"flow_temperature_c":27.1,"return_temperature_c":27.2,"operating_time_in_error_h":0,"energy_at_set_date_kwh":13736,"set_date":"2021-05-31","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heatooo;61243590;16302;13736;2021-05-31;1111-11-11 11:11.11'
        }
        test {
            args     = 'Coolo sharky774 71942539 NOKEY'
            comment  = 'This telegram contains cooling data as well.'
            telegram = 5E44A51139259471410D7A720050052F2F_0C06742400008C1006000000000C13823522008C2013494400000B3B0000000C2B000000000A5A22030A5E91020AA61800004C0619130000CC100600000000426CDF252F2F2F2F2F2F2F2F2F2F2F
            json     = '{"_":"telegram","media":"heat/cooling load","meter":"sharky774","name":"Coolo","id":"71942539","total_energy_consumption_kwh":2474,"total_cooling_consumption_kwh":0,"total_volume_m3":223.582,"total_cooling_volume_m3":4.449,"volume_flow_m3h":0,"power_kw":0,"flow_temperature_c":32.2,"return_temperature_c":29.1,"operating_time_in_error_h":0,"energy_at_set_date_kwh":1319,"cooling_at_set_date_kwh":0,"set_date":"2022-05-31","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Coolo;71942539;2474;1319;2022-05-31;1111-11-11 11:11.11'
        }
    }
}