Automation Direct Stride MQTT Gateway Parser

Any advice writing a MQTT Parse for the Automation Direct Stride MQTT Gateway?

I have tried hacking on several different parses without luck.

 // prevent the code from running for normal inpputed variables (like from analysis or dashboards)
 const mqtt_data = payload.find(data => data.variable === "payload" && data.metadata && data.metadata.mqtt_topic);

 // Suppose that mqtt_data = [{ variable: "payload", value: "0.5,1.5", metadata: { mqtt_topic: "temp hum"} } ];
 console.log(mqtt_data); // outputs content to Live Inspector.

 if (mqtt_data) {
   // Split the topic by the "/", to result in an array
   const splited_topic = mqtt_data.metadata.mqtt_topic;
   // splited_topic is [ 'temp', 'hum' ]
   const splitted_value = mqtt_data.value;
   // splited_topic is [ '0.5', '1.5' ]

   // generate a serial based on timestamp. If you add more
   // than one data, the serial will make sure it shows in the same line
   // in a dynamic table widget for example.
   const serie = String(new Date().getTime());

   // now you should start building your normalized data to TagoIO following specifications at https://docs.tago.io/en/articles/34-sending-data
   // remember to cast strings to Number to prevent issues in the future, like showing data in a chart.
   payload = [
    { variable: splited_topic, value: Bool(splitted_value), serie }
   ];
 }This text will be hidden

Below is a copy of the live inspector. Is this in JSON format? The heading of the topic needs to be changed to variable to match Tago format. Also, the gateway will send a keep alive message with its configuration. Is there a good way to implement this in Tago? Eventually, I would like to trigger an alert if connection is lost after 10 minutes.

Thanks in advance.
Jacob

`2021-10-02 16:19:01: [MQTT] Device publish {"topic":"alarm","payload":"{\"value\":1}","qos":1,"messageId":1031}
2021-10-02 16:19:01: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"alarm"}}]
2021-10-02 16:19:01: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:19:05: [MQTT] Device publish {"topic":"lag","payload":"{\"value\":1}","qos":0}
2021-10-02 16:19:05: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"lag"}}]
2021-10-02 16:19:05: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:19:23: [MQTT] Device publish {"topic":"count","payload":"{\"value\":8254}","qos":0}
2021-10-02 16:19:23: Raw payload: [{"value":8254,"metadata":{"mqtt_topic":"count"}}]
2021-10-02 16:19:23: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:54: [MQTT] Device publish {"topic":"alarm","payload":"{\"value\":\"\"}","qos":1,"messageId":4944}
2021-10-02 16:20:54: [MQTT] Device publish {"topic":"lag","payload":"{\"value\":\"\"}","qos":0}
2021-10-02 16:20:54: Raw payload: [{"value":"","metadata":{"mqtt_topic":"lag"}}]
2021-10-02 16:20:54: Raw payload: [{"value":"","metadata":{"mqtt_topic":"alarm"}}]
2021-10-02 16:20:54: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:54: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:55: [MQTT] Device publish {"topic":"count","payload":"{\"value\":\"\"}","qos":0}
2021-10-02 16:20:55: Raw payload: [{"value":"","metadata":{"mqtt_topic":"count"}}]
2021-10-02 16:20:55: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:56: [MQTT] Device publish {"topic":"alarm","payload":"{\"value\":1}","qos":1,"messageId":6124}
2021-10-02 16:20:56: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"alarm"}}]
2021-10-02 16:20:56: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:56: [MQTT] Device publish {"topic":"lag","payload":"{\"value\":1}","qos":0}
2021-10-02 16:20:56: [MQTT] Device publish {"topic":"count","payload":"{\"value\":8300}","qos":0}
2021-10-02 16:20:56: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"lag"}}]
2021-10-02 16:20:56: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:56: Raw payload: [{"value":8300,"metadata":{"mqtt_topic":"count"}}]
2021-10-02 16:20:56: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:59: [MQTT] Device publish {"topic":"alarm","payload":"{\"value\":\"\"}","qos":1,"messageId":9201}
2021-10-02 16:20:59: Raw payload: [{"value":"","metadata":{"mqtt_topic":"alarm"}}]
2021-10-02 16:20:59: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:20:59: [MQTT] Device publish {"topic":"lag","payload":"{\"value\":\"\"}","qos":0}
2021-10-02 16:20:59: Raw payload: [{"value":"","metadata":{"mqtt_topic":"lag"}}]
2021-10-02 16:20:59: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:21:00: [MQTT] Device publish {"topic":"alarm","payload":"{\"value\":1}","qos":1,"messageId":338}
2021-10-02 16:21:00: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"alarm"}}]
2021-10-02 16:21:00: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:21:00: [MQTT] Device publish {"topic":"lag","payload":"{\"value\":1}","qos":0}
2021-10-02 16:21:00: Raw payload: [{"value":1,"metadata":{"mqtt_topic":"lag"}}]
2021-10-02 16:21:00: Connection refused, invalid payload: Field [variable] is missing
2021-10-02 16:22:36: [MQTT] Device publish {"topic":"count","payload":"{\"value\":8350}","qos":0}
2021-10-02 16:22:36: Raw payload: [{"value":8350,"metadata":{"mqtt_topic":"count"}}]
2021-10-02 16:22:36: Connection refused, invalid payload: Field [variable] is missing
------`

Here is a better attempt at coding a parser

const payload_data = JSON.parse(payload_raw);

// check if payload_raw exists
if (payload_raw) {

    console.log(payload_data);
    console.log(payload_data.value);
    payload = [{ variable: payload_data.metadata.mqtt_topic, value: payload_data.value }];
}

Hi @jacobertel

For example, a json string like this: ‘[{“value”: 1, “metadata”: {“mqtt_topic”: “alarm”}}]’ const payload_data = JSON.parse(payload_raw); is required, be careful with that, if already coming as an object, there is no need. Your raw payload is an array, you need to found what you want inside of it, and since it’s in your code, won’t work, you need to treat it as an array.
Follow your corrected code:

 const payload_data = JSON.parse(payload_raw);

if(payload_raw){
    const mqtt_topic = payload_data[0].metadata.mqtt_topic;
    const value =  payload_data[0].value;
    payload = [{ variable:mqtt_topic, value: value }];
}

Hope it helps!

Thanks

1 Like

hi @jacobertel,

Just a reminder.

Did you create the Action MQTT?
By default, the TagoIO accepts data automatically when use topic tago/data/post . If you are using another topic, like temperature , you’ll need to create an Action MQTT.

Thx,
Fabiano.