// Copyright (C) 2020-2022 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = engelmann_faw
    aliases        = engelmann-faw
    meter_type     = WaterMeter
    default_fields = name,id,status,reporting_date,consumption_at_reporting_date_m3,timestamp
    detect {
        mvt = EFE,00,07
    }
    compact_frame_formats {
        difvif = 426C441301FD17840113C40113840213C40213840313C40313840413C40413840513C40513840613C40613840713C40713840813046D0413
    }
    fields {
        field {
            name     = total
            quantity = Volume
            info     = 'The current total water consumption.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 0
            }
        }
        field {
            name     = meter
            quantity = PointInTime
            info     = 'Date and time of the meter.'
            match {
                measurement_type = Instantaneous
                vif_range        = DateTime
                storage_nr       = 0
            }
        }
        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       = 0xff
                default_message = OK
                map {
                    name  = VOLUME_DETECTION_COILS_DEFECT
                    value = 0x01
                    test  = Set
                }
                map {
                    name  = RESET
                    value = 0x02
                    test  = Set
                }
                map {
                    name  = CRC_ERROR
                    value = 0x04
                    test  = Set
                }
                map {
                    name  = REMOVAL_DETECTED
                    value = 0x08
                    test  = Set
                }
                map {
                    name  = MAGNETIC_MANIPULATION
                    value = 0x10
                    test  = Set
                }
                map {
                    name  = LEAKAGE
                    value = 0x20
                    test  = Set
                }
                map {
                    name  = BLOCKED
                    value = 0x40
                    test  = Set
                }
                map {
                    name  = REVERSE_FLOW
                    value = 0x80
                    test  = Set
                }
            }
        }
        field {
            name         = reporting
            quantity     = PointInTime
            info         = 'The reporting date of the last billing period.'
            display_unit = date
            match {
                measurement_type = Instantaneous
                vif_range        = Date
                storage_nr       = 1
            }
        }
        field {
            name     = consumption_at_reporting_date
            quantity = Volume
            info     = 'The water consumption at the last billing period date.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1
            }
        }
        field {
            name     = 'consumption_{storage_counter-1counter}_months_ago'
            quantity = Volume
            info     = 'Water consumption N month(s) ago.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 2,16
            }
        }
    }
    tests {
        test {
            args     = 'Wasserzaehler engelmann-faw 20254060 NOKEY'
            telegram = 8f44c5145502004301077260402520c51400076b0000002f2f426cbf2c441322e9000001fd17008401133c340100c40113ae2d010084021303290100c402137e21010084031313180100c403138a0e010084041337060100c40413b2fc00008405139af30000c4051322e90000840613c1df0000c40613cdd5000084071365ce0000c407136dc500008408138dbf0000
            json     = '{"_":"telegram","media":"water","meter":"engelmann_faw","name":"Wasserzaehler","id":"20254060","status":"OK","reporting_date":"2021-12-31","consumption_at_reporting_date_m3":59.682,"consumption_1_months_ago_m3":78.908,"consumption_2_months_ago_m3":77.23,"consumption_3_months_ago_m3":76.035,"consumption_4_months_ago_m3":74.11,"consumption_5_months_ago_m3":71.699,"consumption_6_months_ago_m3":69.258,"consumption_7_months_ago_m3":67.127,"consumption_8_months_ago_m3":64.69,"consumption_9_months_ago_m3":62.362,"consumption_10_months_ago_m3":59.682,"consumption_11_months_ago_m3":57.281,"consumption_12_months_ago_m3":54.733,"consumption_13_months_ago_m3":52.837,"consumption_14_months_ago_m3":50.541,"consumption_15_months_ago_m3":49.037,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Wasserzaehler;20254060;OK;2021-12-31;59.682;1111-11-11 11:11.11'
        }
        test {
            args     = 'Wasserzaehler engelmann-faw 22862420 5CDB04C8EDFDC09EC49B5E76A24D4822'
            telegram = 7644c5145528502202077320248622c5140007700060255f2aa2967eb77d0bf310341e3636d9acc17cc5d69ce5632d7b494cc0718e2dbb916bbec0c3ff2b0d6087fd0abb3ba0779564cc089e4527987ef2221d26d7ea448ca59759acfbea4e5b0026ef463592b6668fbf680896e78b461dbb77c32903e0
            json     = '{"_":"telegram","media":"water","meter":"engelmann_faw","name":"Wasserzaehler","id":"22862420","status":"OK","reporting_date":"2025-12-31","consumption_at_reporting_date_m3":228.854,"consumption_1_months_ago_m3":252.159,"consumption_2_months_ago_m3":245.162,"consumption_3_months_ago_m3":239.697,"consumption_4_months_ago_m3":235.11,"consumption_5_months_ago_m3":228.854,"consumption_6_months_ago_m3":222.898,"consumption_7_months_ago_m3":218.216,"consumption_8_months_ago_m3":213.264,"consumption_9_months_ago_m3":207.555,"consumption_10_months_ago_m3":203.639,"consumption_11_months_ago_m3":195.67,"consumption_12_months_ago_m3":188.6,"consumption_13_months_ago_m3":184.596,"consumption_14_months_ago_m3":177.827,"consumption_15_months_ago_m3":172.538,"meter_datetime":"2026-05-04 11:58","total_m3":253.409,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Wasserzaehler;22862420;OK;2025-12-31;228.854;1111-11-11 11:11.11'
        }
    }
}