thingsboard / thingsboard-gateway

Open-source IoT Gateway - integrates devices connected to legacy and third-party systems with ThingsBoard IoT Platform using Modbus, CAN bus, BACnet, BLE, OPC-UA, MQTT, ODBC and REST protocols
https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
Apache License 2.0
1.74k stars 844 forks source link

[BUG] MQTT converter fails if data itself is an array of objects #544

Closed rduivenvoorde closed 3 years ago

rduivenvoorde commented 3 years ago

Describe the bug Not able to retrieve timeseries value if the data/message is a list itself

The data the gateway received is something like: [{"id":"123456"}] (note: the message is actually a docker inspect result) Using the MQTT converter, I seem to be able to retrieve the value by using "${ [0].id }" But I receive the traceback below, I think because: https://github.com/thingsboard/thingsboard-gateway/blob/master/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py#L72 tries to find a ts or timestamp key in the (straight) data:

 if datatype == 'timeseries' and (data.get("ts") is not None or data.get("timestamp") is not None):

but as said: data is an array itself so a call to data.get(...) will result in the error below.

A solution would maybe be to check if data is a list, and if not do current lines, else either take only the first one, or use a json expression?

Connector name (If bug in the some connector): MQTT Connector

Error traceback (If available):

""2021-07-27 14:48:00" - ERROR - [json_mqtt_uplink_converter.py] - json_mqtt_uplink_converter - 78 - 'list' object has no attribute 'get'"
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/thingsboard_gateway-2.6-py3.9.egg/thingsboard_gateway/connectors/mqtt/json_mqtt_uplink_converter.py", line 72, in convert
    if datatype == 'timeseries' and (data.get("ts") is not None or data.get("timestamp") is not None):
AttributeError: 'list' object has no attribute 'get'
****

Versions (please complete the following information):

samson0v commented 3 years ago

Hi Richard Duivenvoorde, fixed the issue, please, update your gateway to the newest version via the master branch and let us know if it helps you.

rduivenvoorde commented 3 years ago

@samson0v Thanks! Will do after my holidays when I'm back at work and report back. But thanks for the swift reaction!