// Copyright (C) 2026 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = kaden
    meter_type     = HeatCostAllocationMeter
    default_fields = name,id,status,consumption_hca,timestamp
    detect {
        mvt = VIP,1E,08
    }
    library {
        use = consumption_hca
        use = target_hca
        use = meter_datetime
        use = target_date
        use = fabrication_no
        use = 'external_temperature_c|Corrected room temperature.'
        use = 'flow_temperature_c|Radiator surface temperature.'
    }
    fields {
        field {
            name       = status
            quantity   = Text
            info       = 'Status and error flags'
            attributes = STATUS,INCLUDE_TPL_STATUS
            match {
                measurement_type = Instantaneous
                vif_range        = ErrorFlags
            }
            lookup {
                name            = ERROR_FLAGS
                map_type        = BitToString
                mask_bits       = 0xffff
                default_message = OK
                map {
                    name  = VOLTAGE_INTERRUPTED
                    value = 0x0001
                    test  = Set
                }
                map {
                    name  = SENSOR_T2_OUTSIDE_MEASURING_RANGE
                    value = 0x0004
                    test  = Set
                }
                map {
                    name  = TAMPERING
                    value = 0x0008
                    test  = Set
                }
                map {
                    name  = SENSOR_T3_OUTSIDE_MEASURING_RANGE
                    value = 0x0020
                    test  = Set
                }
            }
        }
        field {
            name         = no_measurement_duration
            quantity     = Time
            info         = 'How many hours the meter has been removed from the element during winter.'
            display_unit = h
            match {
                difvifkey = 0475
            }
        }
        field {
            name         = no_measurement_duration_last_year
            quantity     = Time
            info         = 'How many hours the meter has been removed from the element previous winter.'
            display_unit = h
            match {
                difvifkey = 4475
            }
        }
        field {
            name         = tampering_duration
            quantity     = Time
            info         = 'Total time the device has been removed from the radiator.'
            display_unit = h
            match {
                difvifkey = 0474
            }
        }
    }
    tests {
        test {
            args     = 'KadenD10 kaden 23800604 82B0551191F51D66EFCDAB8967452301'
            telegram = 4e443059040680231e087ac40040050e6aa476257c0c3adae8277edc999b39b38222fcb387a91e94cb6ed47ceec6470f5f686f89a8574415fa262bd43c88f7f153ce3c66e8e44da338a06c62ab21b1
            json     = '{"_": "telegram","consumption_hca": 0,"external_temperature_c": 19.6,"fabrication_no": "23800604","flow_temperature_c": 19.7,"id": "23800604","media": "heat cost allocation","meter": "kaden","meter_datetime": "2026-02-09 04:01","name": "KadenD10","no_measurement_duration_h": 940.016667,"no_measurement_duration_last_year_h": 5088,"status": "TAMPERING","tampering_duration_h": 0.003611,"target_date": "2127-01-01","target_hca": 3,"timestamp": "1111-11-11T11:11:11Z"}'
            fields   = 'KadenD10;23800604;TAMPERING;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'KadenC10 kaden 23701267 82B0551191F51D66EFCDAB8967452301'
            telegram = 4e443059671270231e087a6f0040051183e178afb952391c01f78104bc3fd33a5232ba7e70f514a062dac99059a7c74a55227dfae9d9590145f685f4ae6a62288bbba6eaf92d86797254644a2cdf46
            json     = '{"_": "telegram","consumption_hca": 859,"external_temperature_c": 32,"fabrication_no": "23701267","flow_temperature_c": 39.3,"id": "23701267","media": "heat cost allocation","meter": "kaden","meter_datetime": "2026-02-09 09:41","name": "KadenC10","no_measurement_duration_h": 2.266667,"no_measurement_duration_last_year_h": 1234.483333,"status": "OK","tampering_duration_h": 0,"target_date": "2127-01-01","target_hca": 2264,"timestamp": "1111-11-11T11:11:11Z"}'
            fields   = 'KadenC10;23701267;OK;859;1111-11-11 11:11.11'
        }
    }
}