// Copyright (C) 2026 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = uiws
    meter_type     = WaterMeter
    default_fields = name,id,status,total_m3,target_m3,timestamp
    manufacturer   = Zenner
    model          = 'IUWS ultrasonic water meter'
    detect {
        mvt = ZRI,99,07
    }
    library {
        use = total_m3
        use = meter_datetime
        use = flow_temperature_c
        use = external_temperature_c
    }
    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 = WootPleaseHelpOutHere
                       bit  = 0
                       test = Set
                   } */
            }
        }
        field {
            name       = unknown
            quantity   = Volume
            info       = 'What is this value?'
            null_value = -2147483.648
            match {
                measurement_type = Instantaneous
                vif_range        = AnyVolumeVIF
                storage_nr       = 1
            }
        }
        field {
            name         = target
            quantity     = PointInTime
            info         = 'Base date of compact profile (most recent monthly value).'
            display_unit = date
            match {
                measurement_type = Instantaneous
                vif_range        = Date
                storage_nr       = 30
            }
        }
        field {
            name     = target
            quantity = Volume
            info     = 'Base HCA value of compact profile.'
            match {
                measurement_type = Instantaneous
                vif_range        = AnyVolumeVIF
                storage_nr       = 30
            }
        }
        field {
            name       = 'target_{storage_counter-30counter}'
            quantity   = Volume
            info       = 'Compact-profile history values (most recent first after base month).'
            null_value = -2147483.648
            match {
                measurement_type = Instantaneous
                vif_range        = AnyVolumeVIF
                add_combinable   = Synthetic
                storage_nr       = 30,40
            }
        }
        field {
            name         = 'target_{storage_counter-30counter}'
            quantity     = PointInTime
            info         = 'Billing period date for each compact-profile history slot.'
            display_unit = date
            match {
                measurement_type = Instantaneous
                vif_range        = Date
                add_combinable   = Synthetic
                storage_nr       = 30,40
            }
        }
    }
    tests {
        test {
            args     = 'ZW uiws 61330764 NOKEY'
            telegram = 0E44496A6407336199077A361260252F2F_046D22484136041301000000441300000080820F6C4136840F13010000008D0F93132E34FE000000000000008000000080000000800000008000000080000000800000008000000080000000800000008002FD1728000259340702656C07
            json     = '{"_": "telegram","external_temperature_c": 19,"flow_temperature_c": 18.44,"id": "61330764","media": "water","meter": "uiws","meter_datetime": "2026-06-01 08:34","name": "ZW","status": "ERROR ERROR_FLAGS_28 TEMPORARY_ERROR","target_10_date": "2025-08-01","target_10_m3": null,"target_1_date": "2026-05-01","target_1_m3": 0,"target_2_date": "2026-04-01","target_2_m3": null,"target_3_date": "2026-03-01","target_3_m3": null,"target_4_date": "2026-02-01","target_4_m3": null,"target_5_date": "2026-01-01","target_5_m3": null,"target_6_date": "2025-12-01","target_6_m3": null,"target_7_date": "2025-11-01","target_7_m3": null,"target_8_date": "2025-10-01","target_8_m3": null,"target_9_date": "2025-09-01","target_9_m3": null,"target_date": "2026-06-01","target_m3": 0.001,"timestamp": "1111-11-11T11:11:11Z","total_m3": 0.001,"unknown_m3": null}'
            fields   = 'ZW;61330764;ERROR ERROR_FLAGS_28 TEMPORARY_ERROR;0.001;0.001;1111-11-11 11:11.11'
        }
    }
}