bellrichm / WeeWX-MQTTSubscribe

A WeeWX service and driver that receives data from MQTT.
GNU General Public License v3.0
52 stars 13 forks source link

Error with date fields #156

Closed jmceara closed 2 years ago

jmceara commented 2 years ago

Hi!

I have my weewx running for a while with MQTTSubscribe and individual mode. Now, I'm trying to use both Individual and JSON formats at the same time, but it's not working.....I don't think it's related to both modes, but something related to formatting.

This is what I'm getting in logs: May 18 14:25:08 Skynet weewx[258144] DEBUG user.MQTTSubscribe: (Driver) MessageCallbackProvider data-> incoming topic: zigbee2mqtt/Escritorio_-_Temperatura, QOS: 0, retain: 0, payload: b'{"battery":71,"humidity":43.77,"last_seen":"2022-05-18T14:25:08-03:00","linkquality":98,"temperature":21.13,"voltage":2900}' May 18 14:25:08 Skynet weewx[258144] ERROR user.MQTTSubscribe: (Driver) MessageCallbackProvider on_message_json failed with <class 'user.MQTTSubscribe.ConversionError'> and reason Failed converting field last_seen with value 2022-05-18T14:25:08-03:00 using 'lambda x: to_float(x)' with reason could not convert string to float: '2022-05-18T14:25:08-03:00'.. May 18 14:25:08 Skynet weewx[258144] ERROR user.MQTTSubscribe: (Driver) **** MessageCallbackProvider Ignoring topic=zigbee2mqtt/Escritorio_-_Temperatura and payload=b'{"battery":71,"humidity":43.77,"last_seen":"2022-05-18T14:25:08-03:00","linkquality":98,"temperature":21.13,"voltage":2900}' May 18 14:25:08 Skynet weewx[258144] ERROR user.MQTTSubscribe: (Driver) **** MessageCallbackProvider Traceback (most recent call last):#012 File "/usr/share/weewx/user/MQTTSubscribe.py", line 1280, in _convert_value#012 return conversion_func['compiled'](value)#012 File "<string>", line 1, in <lambda>#012 File "/usr/share/weewx/weeutil/weeutil.py", line 1348, in to_float#012 return float(x) if x is not None else None#012ValueError: could not convert string to float: '2022-05-18T14:25:08-03:00'#012#012During handling of the above exception, another exception occurred:#012#012Traceback (most recent call last):#012 File "/usr/share/weewx/user/MQTTSubscribe.py", line 1463, in _on_message_json#012 (fieldname, value) = self._update_data(fields, fields_conversion_func, lookup_key, data_flattened[key], unit_system)#012 File "/usr/share/weewx/user/MQTTSubscribe.py", line 1247, in _update_data#012 value = self._convert_value(fields, default_field_conversion_func, orig_name, orig_value)#012 File "/usr/share/weewx/user/MQTTSubscribe.py", line 1285, in _convert_value#012 raise ConversionError("Failed converting field %s with value %s using '%s' with reason %s." \#012user.MQTTSubscribe.ConversionError: Failed converting field last_seen with value 2022-05-18T14:25:08-03:00 using 'lambda x: to_float(x)' with reason could not convert string to float: '2022-05-18T14:25:08-03:00'.

And this is my configuration:

[MQTTSubscribeDriver]
    # This section is for the MQTTSubscribe driver.

    # The driver to use:
    driver = user.MQTTSubscribe

    # The MQTT server.
    # Default is localhost.
    host = 192.168.A.B

    # The port to connect to.
    # Default is 1883.
    port = 1883

    # Maximum period in seconds allowed between communications with the broker.
    # Default is 60.
    keepalive = 60

    # username for broker authentication.
    # Default is None.
    username = ABC

    # password for broker authentication.
    # Default is None.
    password = XXX
    use_server_datetime = true

    log = false

    # Configuration for the message callback.
    [[message_callback]]
        type = individual

    # The topics to subscribe to.
    [[topics]]
        unit_system = METRIC
        #use_topic_as_fieldname = true
        use_server_datetime = True

        [[[message]]]
            type = individual

        [[[rtl_433/devices/Fineoffset-WHx080/temperature_C]]]
            name = outTemp

        [[[rtl_433/devices/Fineoffset-WHx080/humidity]]]
            name = outHumidity

        [[[rtl_433/devices/Fineoffset-WHx080/rain_mm]]]
            name = rain
            contains_total = True
            units = mm

        [[[iaq-board-1/sensor/iaq_1_-_bme280_pressure/state]]]
            name = pressure
            units = hPa

        [[[iaq-board-1/sensor/iaq_1_-_pm25_24h_average/state]]]
            name = pm2_5

        [[[iaq-board-1/sensor/iaq_1_-_pm_1_concentration/state]]]
            name = pm1_0

        [[[iaq-board-1/sensor/iaq_1_-_pm_10_concentration/state]]]
            name = pm10_0

        [[[iaq-board-1/sensor/iaq_1_-_co2/state]]]
            name = co2

        [[[rtl_433/devices/Fineoffset-WHx080/wind_avg_km_h]]]
            name = windSpeed
            units = km_per_hour

        [[[rtl_433/devices/Fineoffset-WHx080/wind_max_km_h]]]
            name = windGust
            units = km_per_hour

        [[[rtl_433/devices/Fineoffset-WHx080/wind_dir_deg]]]
            name = windDir

        [[[esp32-cam-1/sensor/esp32_-_cam_1_-_temperatura_boiler-a/state]]]
            name = extraTemp1

        [[[esp32-cam-1/sensor/esp32_-_cam_1_-_temperatura_painel_solar/state]]]
            name = extraTemp2

        #[[[esp32-cam-1/sensor/bme280_temperature/state]]]
        #    name = extraTemp3

        #[[[esp32_cam_1/sensor/bme280_humidity/state]]]
        #    name = extraHumid1

        #[[[esp32-ext-1/sensor/temperature_caixa/state]]]
        #    name = extraTemp4

        [[[sensor-uv/sensor/sensor_uv_-_veml6075-uvi/state]]]
            name = UV

        [[[zigbee2mqtt/Escritorio_-_Temperatura]]]
       #     [[[[message]]]]
            type = json
            [[[[message]]]]
                type = json
            [[[[temperature]]]]
                name = inTemp

All fields are working, except inTemp

jmceara commented 2 years ago

Forgot to say: using version 2.1.0

bellrichm commented 2 years ago

Your json payload is

{
  "battery":71,
  "humidity":43.77,
  "last_seen":"2022-05-18T14:25:08-03:00",
  "linkquality":98,
  "temperature":21.13,
  "voltage":2900
}

MQTTSubscribe attempts to convert all incoming data to float. The easisest, if you don't care about last_seen is to ignore it.

        [[[zigbee2mqtt/Escritorio_-_Temperatura]]]
            [[[[message]]]]
                type = json
            [[[[temperature]]]]
                name = inTemp
            [[[[last_seen]]]]
                ignore = true

Alternately you could keep it as a string

        [[[zigbee2mqtt/Escritorio_-_Temperatura]]]
            [[[[message]]]]
                type = json
            [[[[temperature]]]]
                name = inTemp
            [[[[last_seen]]]]
                conversion_type = none
jmceara commented 2 years ago

Tks! I will try this tonorrow.

One sugestion is to parse only specified fields from condifuration. I mean, default state would be ignore and just include fields from condiguration.

bellrichm commented 2 years ago

Agree that if I was starting from scratch, 'opt in' woukd make sense. When I started I wanted everyting in the json and now with MQTTSubscribe 3 years old, I am hesitate to make that kind of breaking change. If one wants to 'opt in', you could do something like this.

        [[[zigbee2mqtt/Escritorio_-_Temperatura]]]
            ignore = true
            [[[[message]]]]
                type = json
            [[[[temperature]]]]
                name = inTemp
                ignore = false
bellrichm commented 2 years ago

Are you all set?

bellrichm commented 2 years ago

Closing due to inactivity. Feel free to reopen if necessary.