// Copyright (C) 2022 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = enercal
    meter_type     = HeatMeter
    default_fields = name,id,status,total_kwh,target_kwh,total_m3,target_m3,timestamp
    detect {
        mvt = GWF,08,04
    }
    library {
        use = total_kwh
        use = target_kwh
        use = total_m3
        use = target_m3
        use = target_date
        use = operating_time_h
        use = on_time_h
        use = meter_datetime
    }
    fields {
        field {
            name       = status
            quantity   = Text
            info       = 'Meter status.'
            attributes = STATUS,INCLUDE_TPL_STATUS
        }
        field {
            name     = power
            quantity = Power
            info     = 'The active power consumption.'
            match {
                measurement_type = Instantaneous
                vif_range        = AnyPowerVIF
            }
        }
        field {
            name     = flow
            quantity = Flow
            info     = 'The flow of water.'
            match {
                measurement_type = Instantaneous
                vif_range        = VolumeFlow
            }
        }
        field {
            name     = flow_max
            quantity = Flow
            info     = 'The maximum forward flow of water since the last set date?'
            match {
                measurement_type = Maximum
                vif_range        = VolumeFlow
            }
        }
        field {
            name     = forward
            quantity = Temperature
            info     = 'The forward temperature of the water.'
            match {
                measurement_type = Instantaneous
                vif_range        = FlowTemperature
            }
        }
        field {
            name     = return
            quantity = Temperature
            info     = 'The return temperature of the water.'
            match {
                measurement_type = Instantaneous
                vif_range        = ReturnTemperature
            }
        }
        field {
            name     = difference
            quantity = Temperature
            info     = 'The temperature difference forward-return for the water.'
            match {
                measurement_type = Instantaneous
                vif_range        = TemperatureDifference
            }
        }
        field {
            name     = subunit1
            quantity = Volume
            info     = 'The amount of water that has passed through subunit 1.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                subunit_nr       = 1
            }
        }
        field {
            name     = subunit1_target
            quantity = Volume
            info     = 'The amount of water that had passed through the subunit 1 at the set date.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1
                subunit_nr       = 1
            }
        }
        field {
            name     = subunit1
            quantity = HCA
            info     = 'The current heat cost allocation for subunit 1.'
            match {
                measurement_type = Instantaneous
                vif_range        = HeatCostAllocation
                subunit_nr       = 1
            }
        }
        field {
            name     = subunit1_target
            quantity = HCA
            info     = 'The current heat cost allocation for subunit 1 at the target date.'
            match {
                measurement_type = Instantaneous
                vif_range        = HeatCostAllocation
                subunit_nr       = 1
                storage_nr       = 1
            }
        }
        field {
            name     = subunit2
            quantity = HCA
            info     = 'The current heat cost allocation for subunit 2.'
            match {
                measurement_type = Instantaneous
                vif_range        = HeatCostAllocation
                subunit_nr       = 2
            }
        }
        field {
            name     = subunit2_target
            quantity = HCA
            info     = 'The current heat cost allocation for subunit 2 at the target date.'
            match {
                measurement_type = Instantaneous
                vif_range        = HeatCostAllocation
                subunit_nr       = 2
                storage_nr       = 1
            }
        }
    }
    tests {
        test {
            args     = 'Heat enercal 00309924 NOKEY'
            telegram = 688e8e6808017224993000e61e080406000000040681460600041488f2350084401426e02600025B2600025f220002622a00042247880200042647880200043B00000000042c00000000046d2d0ede2784406e000000008480406e00000000c4800006B1450600c48000143Be43500c4c000142fd22600c4c0006e00000000c480406e00000000c280006cc1271f00000000f416
            json     = '{"_":"telegram","media":"heat","meter":"enercal","name":"Heat","id":"00309924","status":"OK","total_kwh":411265,"target_kwh":411057,"power_kw":0,"flow_m3h":0,"forward_c":38,"return_c":34,"difference_c":4.2,"total_m3":35354.96,"target_m3":35318.35,"subunit1_m3":25477.5,"subunit1_target_m3":25441.75,"subunit1_hca":0,"subunit1_target_hca":0,"subunit2_hca":0,"subunit2_target_hca":0,"target_date":"2022-07-01","operating_time_h":165959,"on_time_h":165959,"meter_datetime":"2022-07-30 14:45","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heat;00309924;OK;411265;411057;35354.96;35318.35;1111-11-11 11:11.11'
        }
    }
}