|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# (c) 2023 Andreas Motl <[email protected]> |
| 3 | +import typing as t |
| 4 | + |
| 5 | + |
| 6 | +class FineOffsetDecoder: |
| 7 | + """ |
| 8 | + Decode data format submitted by Fine Offset (FOSHK) weather stations, using the |
| 9 | + excellent `ecowitt2mqtt` machinery [1]. |
| 10 | +
|
| 11 | + By wrapping it into a Kotori decoder, it will simplify operation and maintenance. |
| 12 | + Effectively, there are fewer moving parts involved, yet all features can be leveraged: |
| 13 | +
|
| 14 | + - anonymization of data |
| 15 | + - convenience of unit conversion |
| 16 | + - additional calculated values |
| 17 | + - integrated test coverage |
| 18 | + - no installation overhead |
| 19 | +
|
| 20 | + Despite the name of the library, `ecowitt2mqtt` [2] supports any weather |
| 21 | + station/gateway that is produced by Shenzhen Fine Offset Electronics Co., Ltd. [3] |
| 22 | + aka. Fine Offset aka. OFFSET. This includes brands that white-label Fine Offset |
| 23 | + equipment, such as: |
| 24 | +
|
| 25 | + - Ambient Weather (U.S.) |
| 26 | + - Ecowitt (China, Hong Kong) |
| 27 | + - Froggit (Germany) |
| 28 | +
|
| 29 | + ...and many others. For more information on how these brands relate to one another, |
| 30 | + see the forum post at [4]. |
| 31 | +
|
| 32 | + Although there are some small differences between how these various branded devices |
| 33 | + are configured, `ecowitt2mqtt` endeavors to incorporate them all with minimal effort |
| 34 | + on the user's part [5]. |
| 35 | +
|
| 36 | + `ecowitt2mqtt` currently supports the following input data formats [6]: |
| 37 | +
|
| 38 | + - `ambient_weather` |
| 39 | + - `ecowitt` |
| 40 | +
|
| 41 | + [1] https://community.hiveeyes.org/t/more-data-acquisition-payload-formats-for-kotori/1421/17 |
| 42 | + [2] https://github.com/bachya/ecowitt2mqtt |
| 43 | + [3] https://www.foshk.com/ |
| 44 | + [4] https://www.wxforum.net/index.php?topic=40730.0 |
| 45 | + [5] https://github.com/bachya/ecowitt2mqtt/tree/dev#supported-brands |
| 46 | + [6] https://github.com/bachya/ecowitt2mqtt/tree/dev#input-data-formats |
| 47 | +
|
| 48 | + Example data |
| 49 | + ============ |
| 50 | +
|
| 51 | + This is an input data sample provided by an Ecowitt weather station, then |
| 52 | + converted to the "metric" unit system and with additional computed values |
| 53 | + by `ecowitt2mqtt`, displayed in the "Output" section. |
| 54 | +
|
| 55 | + Input |
| 56 | + ----- |
| 57 | + :: |
| 58 | +
|
| 59 | + { |
| 60 | + "PASSKEY": "B950C...[obliterated]", |
| 61 | + "stationtype": "EasyWeatherPro_V5.0.6", |
| 62 | + "runtime": "456128", |
| 63 | + "dateutc": "2023-02-20 16:02:19", |
| 64 | + "tempinf": "69.8", |
| 65 | + "humidityin": "47", |
| 66 | + "baromrelin": "29.713", |
| 67 | + "baromabsin": "29.713", |
| 68 | + "tempf": "48.4", |
| 69 | + "humidity": "80", |
| 70 | + "winddir": "108", |
| 71 | + "windspeedmph": "1.12", |
| 72 | + "windgustmph": "4.92", |
| 73 | + "maxdailygust": "12.97", |
| 74 | + "solarradiation": "1.89", |
| 75 | + "uv": "0", |
| 76 | + "rainratein": "0.000", |
| 77 | + "eventrainin": "0.000", |
| 78 | + "hourlyrainin": "0.000", |
| 79 | + "dailyrainin": "0.028", |
| 80 | + "weeklyrainin": "0.098", |
| 81 | + "monthlyrainin": "0.909", |
| 82 | + "yearlyrainin": "0.909", |
| 83 | + "temp1f": "45.0", |
| 84 | + "humidity1": "90", |
| 85 | + "soilmoisture1": "46", |
| 86 | + "soilmoisture2": "53", |
| 87 | + "tf_ch1": "41.9", |
| 88 | + "rrain_piezo": "0.000", |
| 89 | + "erain_piezo": "0.000", |
| 90 | + "hrain_piezo": "0.000", |
| 91 | + "drain_piezo": "0.028", |
| 92 | + "wrain_piezo": "0.043", |
| 93 | + "mrain_piezo": "0.492", |
| 94 | + "yrain_piezo": "0.492", |
| 95 | + "wh65batt": "0", |
| 96 | + "wh25batt": "0", |
| 97 | + "batt1": "0", |
| 98 | + "soilbatt1": "1.6", |
| 99 | + "soilbatt2": "1.6", |
| 100 | + "tf_batt1": "1.60", |
| 101 | + "wh90batt": "3.04", |
| 102 | + "freq": "868M", |
| 103 | + "model": "HP1000SE-PRO_Pro_V1.8.5", |
| 104 | + } |
| 105 | +
|
| 106 | + Output |
| 107 | + ------ |
| 108 | + :: |
| 109 | +
|
| 110 | + { |
| 111 | + "runtime": 456128.0, |
| 112 | + "tempin": 20.999999999999996, |
| 113 | + "humidityin": 47.0, |
| 114 | + "baromrel": 1006.1976567045213, |
| 115 | + "baromabs": 1006.1976567045213, |
| 116 | + "temp": 9.11111111111111, |
| 117 | + "humidity": 80.0, |
| 118 | + "winddir": 108.0, |
| 119 | + "windspeed": 1.8024652800000003, |
| 120 | + "windgust": 7.91797248, |
| 121 | + "maxdailygust": 20.873191679999998, |
| 122 | + "solarradiation": 1.89, |
| 123 | + "uv": 0.0, |
| 124 | + "rainrate": 0.0, |
| 125 | + "eventrain": 0.0, |
| 126 | + "hourlyrain": 0.0, |
| 127 | + "dailyrain": 0.7112, |
| 128 | + "weeklyrain": 2.4892000000000003, |
| 129 | + "monthlyrain": 23.0886, |
| 130 | + "yearlyrain": 23.0886, |
| 131 | + "temp1": 7.222222222222222, |
| 132 | + "humidity1": 90.0, |
| 133 | + "soilmoisture1": 46.0, |
| 134 | + "soilmoisture2": 53.0, |
| 135 | + "tf_ch1": 5.499999999999999, |
| 136 | + "rrain_piezo": 0.0, |
| 137 | + "erain_piezo": 0.0, |
| 138 | + "hrain_piezo": 0.0, |
| 139 | + "drain_piezo": 0.7112, |
| 140 | + "wrain_piezo": 1.0921999999999998, |
| 141 | + "mrain_piezo": 12.4968, |
| 142 | + "yrain_piezo": 12.4968, |
| 143 | + "wh65batt": "OFF", |
| 144 | + "wh25batt": "OFF", |
| 145 | + "batt1": "OFF", |
| 146 | + "soilbatt1": 1.6, |
| 147 | + "soilbatt2": 1.6, |
| 148 | + "tf_batt1": 1.6, |
| 149 | + "wh90batt": 3.04, |
| 150 | + "beaufortscale": 1, |
| 151 | + "dewpoint": 5.846942096976985, |
| 152 | + "feelslike": 9.11111111111111, |
| 153 | + "frostpoint": 4.706401162443284, |
| 154 | + "frostrisk": "No risk", |
| 155 | + "heatindex": 8.166666666666668, |
| 156 | + "humidex": 9, |
| 157 | + "humidex_perception": "Comfortable", |
| 158 | + "humidityabs": 7.101409765339333, |
| 159 | + "humidityabsin": 7.101409765339333, |
| 160 | + "relative_strain_index": null, |
| 161 | + "relative_strain_index_perception": null, |
| 162 | + "safe_exposure_time_skin_type_1": null, |
| 163 | + "safe_exposure_time_skin_type_2": null, |
| 164 | + "safe_exposure_time_skin_type_3": null, |
| 165 | + "safe_exposure_time_skin_type_4": null, |
| 166 | + "safe_exposure_time_skin_type_5": null, |
| 167 | + "safe_exposure_time_skin_type_6": null, |
| 168 | + "simmerindex": null, |
| 169 | + "simmerzone": null, |
| 170 | + "solarradiation_perceived": 47.57669425765605, |
| 171 | + "thermalperception": "Dry", |
| 172 | + "windchill": null |
| 173 | + } |
| 174 | +
|
| 175 | + """ |
| 176 | + |
| 177 | + @staticmethod |
| 178 | + def detect(data: t.Dict[str, str]) -> bool: |
| 179 | + """ |
| 180 | + Determine whether the data payload is submitted by a Fine Offset device. |
| 181 | +
|
| 182 | + TODO: Maybe leverage field names in `ecowitt2mqtt.data.DEFAULT_KEYS_TO_IGNORE`? |
| 183 | + """ |
| 184 | + return "PASSKEY" in data and "stationtype" in data and "model" in data |
| 185 | + |
| 186 | + @staticmethod |
| 187 | + def decode(data: t.Dict[str, str]) -> t.Dict[str, t.Any]: |
| 188 | + """ |
| 189 | + Decode data payload submitted by a Fine Offset device, using `ecowitt2mqtt`. |
| 190 | + """ |
| 191 | + |
| 192 | + # This variant is compatible with modern releases like `ecowitt2mqtt-2023.2.1`. |
| 193 | + try: |
| 194 | + from ecowitt2mqtt.config import Config |
| 195 | + from ecowitt2mqtt.data import ProcessedData |
| 196 | + |
| 197 | + config = Config( |
| 198 | + { |
| 199 | + # Both configuration variables are currently *required* by `ecowitt2mqtt`. |
| 200 | + # Fortunately, `mqtt_broker` can be left empty. |
| 201 | + # TODO: Can this be improved if upstream would accept a corresponding patch? |
| 202 | + "hass_discovery": True, |
| 203 | + "mqtt_broker": "", |
| 204 | + # Output values in *metric* unit system by default. |
| 205 | + # TODO: Make output unit system configurable. |
| 206 | + "output_unit_system": "metric", |
| 207 | + } |
| 208 | + ) |
| 209 | + processed_data = ProcessedData(config=config, data=data) |
| 210 | + data = {key: value.value for key, value in processed_data.output.items()} |
| 211 | + |
| 212 | + # This variant is compatible with earlier releases like `ecowitt2mqtt-2022.5.0`. |
| 213 | + except ImportError: |
| 214 | + import argparse |
| 215 | + |
| 216 | + from ecowitt2mqtt.data import DataProcessor |
| 217 | + |
| 218 | + args = argparse.Namespace() |
| 219 | + args.input_unit_system = "imperial" |
| 220 | + args.output_unit_system = "metric" |
| 221 | + args.raw_data = None |
| 222 | + |
| 223 | + data_processor = DataProcessor(data, args) |
| 224 | + data = data_processor.generate_data() |
| 225 | + |
| 226 | + return data |
0 commit comments