Search code examples
mqttthingsboardthingsboard-gateway

ThingsBoard IoT Gateway doesn't update MQTT values


I try to receive simple text values from external MQTT broker topics with IoT Gateway.

For this purpose I simplify the existing script (extensions/mqtt/custom_mqtt_uplink_converter.py):

from thingsboard_gateway.connectors.mqtt.mqtt_uplink_converter import MqttUplinkConverter, log

class CustomMqttUplinkConverter(MqttUplinkConverter):
    def __init__(self, config):
        self.__config = config.get('converter')
        self.dict_result = {}

    def convert(self, topic, body):
        try:
            log.debug("New data received: %s: %s" % (topic,body))
            # if topic = '/devices/buzzer/controls/volume' device name will be 'buzzer'.
            self.dict_result["deviceName"] = topic.split("/")[2]
            # just hardcode this
            self.dict_result["deviceType"] = "buzzer"
            self.dict_result["telemetry"] = {"data": body}
            log.debug("Result: %s" % (self.dict_result))
            return self.dict_result

        except ...

When I start gateway I see in his log that he successfully connected and read the values:

INFO ... MQTT Broker Connector connected to 10.1.1.2:1883 - successfully.'
DEBUG ... Client <paho.mqtt.client.Client object at 0x7fb42d19dd68>, userdata None, flags {'session present': 0}, extra_params ()'

DEBUG ... <module 'CustomMqttUplinkConverter' from '/var/lib/thingsboard_gateway/extensions/mqtt/custom_mqtt_uplink_converter.py'>'
DEBUG ... Import CustomMqttUplinkConverter from /var/lib/thingsboard_gateway/extensions/mqtt.'
DEBUG ... Converter CustomMqttUplinkConverter for topic /devices/buzzer/controls/volume - found!'
INFO ... Connector "MQTT Broker Connector" subscribe to /devices/buzzer/controls/volume'
DEBUG ... Received data: {}'
DEBUG ... (None,)'
INFO ... "MQTT Broker Connector" subscription success to topic /devices/buzzer/controls/volume, subscription message id = 1'
DEBUG ... New data received: /devices/buzzer/controls/volume: 66'
DEBUG ... Result: {'deviceName': 'buzzer', 'deviceType': 'buzzer', 'telemetry': {'data': 66}}'

But this values are the last values he can read. If I change volume one broker new values will not appear neither in the log nor in TB UI. (I control updates with mosquitto_sub.)

Seems this converter will never called again until gateway restarted. Is it correct behaveour?

How can I make sure that my code is correct if I don't see the result?


Solution

  • Hi I have tried your version of the custom converter, it didn't work, but when I changed
    self.dict_result["telemetry"] = {"data": body}
    to
    self.dict_result["telemetry"] = [{"data": body}]
    It sent data correctly.

    The gateway requires an array of telemetry of attributes from the converter.