Help with payload parse script for power meter

Hello, I would like if someone would be kind enough to assist with a payload parse script,

We are looking for the variables

temperature
ext_temperature
energy
power

any help would be greatly appreciated

2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0”,“payload”:“off”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/input/0”,“payload”:“0”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0/power”,“payload”:“0.00”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0/energy”,“payload”:“0”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature”,“payload”:“41.94”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature_f”,“payload”:“107.50”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/overtemperature”,“payload”:“0”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature_status”,“payload”:“Normal”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature/0”,“payload”:“23.9”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature_f/0”,“payload”:“75.0”,“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tC”:23.9}}",“qos”:0}
2021-05-18 10:48:52: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures_f”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tF”:75.0}}",“qos”:0}

1 Like

Hi Tyler,
You just need to handle this as any string.

First, make sure you created an action for your devices that are using MQTT.
You need to go to Actions > Actions > + Create.
Select MQTT Topic for the type of the trigger, and to Store into bucket for the type of the action.

The value of the “payload” variable in your payload parser is what you see in the Device Publish from the live inspector.
That means, you can handle this as:

  // 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);
  if (mqtt_data) {
    // Split the topic by the "/", to result in an array
    const splited_topic = mqtt_data.metadata.mqtt_topic.split("/");
    // splited_topic is now [ 'meters', 'powerm-2D73', 'relay', '0' ]

    // 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 = new Date().getTime();
    payload = [{ variable: splited_topic[2], value: mqtt_data.value, serie }];
  }

Hello Vitor,

thank you for your assistance, I thought I would try with a simple device before we moved to a more complex one. But this seems to be not as easy as it should be for some reason the only variables that get added to the bucket are

overtemperature
relay
input
temperature_status

It seems like I am missing something.

Tyler

It can be that you didn’t set the Topic correct in the Action.

If topic is not set correct, it will not try to store the data that is being send through the MQTT, and neither will run the parser.

From the screenshots you send me, you would need to setup two topic as:

Notice that, in the Live Inspector, for the data to be accepted and added to TagoIO, it must look like this:

If it shows up like the screenshot below, it means only that you received something from the MQTT, but it is not being handlded by the action.

I was using #, rather than +, but have since changed as you suggested. no new variables have populated.

but you can see that some of the data is accepted but others is not

2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0”,“payload”:“off”,“qos”:1,“messageId”:9144}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/input/0”,“payload”:“0”,“qos”:1,“messageId”:9144}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0/power”,“payload”:“0.00”,“qos”:1,“messageId”:9144}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/relay/0/energy”,“payload”:“0”,“qos”:1,“messageId”:9144}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature”,“payload”:“51.36”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature_f”,“payload”:“124.44”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/overtemperature”,“payload”:“0”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/temperature_status”,“payload”:“Normal”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature/0”,“payload”:“26.0”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature_f/0”,“payload”:“78.8”,“qos”:1,“messageId”:9145}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tC”:26.0}}",“qos”:1,“messageId”:9146}
2021-05-19 16:13:59: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures_f”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.8}}",“qos”:1,“messageId”:9146}
2021-05-19 16:13:59: Raw payload: [{“variable”:“payload”,“value”:“off”,“metadata”:{“mqtt_topic”:“meters/powerm-2D73/relay/0”}}]
2021-05-19 16:13:59: Raw payload: [51.36]
2021-05-19 16:13:59: Raw payload: [{“variable”:“payload”,“value”:“Normal”,“metadata”:{“mqtt_topic”:“meters/powerm-2D73/temperature_status”}}]
2021-05-19 16:13:59: Raw payload: [{“variable”:“payload”,“value”:“0”,“metadata”:{“mqtt_topic”:“meters/powerm-2D73/input/0”}}]
2021-05-19 16:13:59: Raw payload: [26]
2021-05-19 16:13:59: Raw payload: [124.44]
2021-05-19 16:13:59: Raw payload: [{“variable”:“payload”,“value”:“0”,“metadata”:{“mqtt_topic”:“meters/powerm-2D73/overtemperature”}}]
2021-05-19 16:13:59: Raw payload: [78.8]
2021-05-19 16:13:59: Result of [device] payload parser: [{“variable”:“relay”,“value”:“off”,“serie”:1621455239172}]
2021-05-19 16:13:59: Raw payload: [{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.8},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures_f”}}]
2021-05-19 16:13:59: Raw payload: [{“0”:{“hwID”:“28061a75d0013c84”,“tC”:26},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures”}}]
2021-05-19 16:13:59: Result of [device] payload parser: [51.36]
2021-05-19 16:13:59: Result of [device] payload parser: [{“variable”:“temperature_status”,“value”:“Normal”,“serie”:1621455239178}]
2021-05-19 16:13:59: Result of [device] payload parser: [{“variable”:“input”,“value”:“0”,“serie”:1621455239182}]
2021-05-19 16:13:59: Result of [device] payload parser: [26]
2021-05-19 16:13:59: Result of [device] payload parser: [124.44]
2021-05-19 16:13:59: Result of [device] payload parser: [78.8]
2021-05-19 16:13:59: Result of [device] payload parser: [{“variable”:“overtemperature”,“value”:“0”,“serie”:1621455239191}]
2021-05-19 16:13:59: Result of [device] payload parser: [{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.8},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures_f”}}]
2021-05-19 16:13:59: Result of [device] payload parser: [{“0”:{“hwID”:“28061a75d0013c84”,“tC”:26},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures”}}]
2021-05-19 16:13:59: Bucket [3674]: 1 Data Added
2021-05-19 16:13:59: Bucket [3674]: 1 Data Added
2021-05-19 16:13:59: Bucket [3674]: 1 Data Added
2021-05-19 16:13:59: Bucket [3674]: 1 Data Added
2021-05-19 16:14:12: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature/0”,“payload”:“25.9”,“qos”:1,“messageId”:2898}
2021-05-19 16:14:12: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperature_f/0”,“payload”:“78.6”,“qos”:1,“messageId”:2898}
2021-05-19 16:14:12: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tC”:25.9}}",“qos”:1,“messageId”:2898}
2021-05-19 16:14:12: [MQTT] Device publish {“topic”:“meters/powerm-2D73/ext_temperatures_f”,“payload”:"{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.6}}",“qos”:1,“messageId”:2898}
2021-05-19 16:14:12: Raw payload: [25.9]
2021-05-19 16:14:12: Raw payload: [{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.6},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures_f”}}]
2021-05-19 16:14:12: Raw payload: [{“0”:{“hwID”:“28061a75d0013c84”,“tC”:25.9},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures”}}]
2021-05-19 16:14:12: Raw payload: [78.6]
2021-05-19 16:14:12: Result of [device] payload parser: [{“0”:{“hwID”:“28061a75d0013c84”,“tF”:78.6},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures_f”}}]
2021-05-19 16:14:12: Result of [device] payload parser: [25.9]
2021-05-19 16:14:12: Result of [device] payload parser: [{“0”:{“hwID”:“28061a75d0013c84”,“tC”:25.9},“metadata”:{“mqtt_topic”:“meters/powerm-2D73/ext_temperatures”}}]
2021-05-19 16:14:12: Result of [device] payload parser: [78.6]

Hi Tyler,
The support team just bring your post to my attention, as it seems I didn’t notice your answer.

I would need you to paste your payload parser script and a screenshot of your action in order to further help you.

here is the script and action.

// 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);
if (mqtt_data) {
// Split the topic by the “/”, to result in an array
const splited_topic = mqtt_data.metadata.mqtt_topic.split("/");
// splited_topic is now [ ‘meters’, ‘powerm-2D73’, ‘relay’, ‘0’ ]
// 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 = new Date().getTime();
payload = [{ variable: splited_topic[2], value: mqtt_data.value, serie }];
}

Hi Tyler,
It seems you did set different topics from the ones you provided the Live Inspector logs. So I’m not sure if it did miss your attention, or if you did also changed that in the device.

The topic described in the Live Inspector is:
{“topic”:“meters/powerm-2D73/ext_temperature/0”,“payload”:“25.9”,“qos”:1,“messageId”:2898}

And the ones at the action screenshot you provided start with “shellies/+/+/+”. So the topic is not being captured by your action, since it doesn’t start with “shellies”.

Regarding the Payload Parser, it seems correct for me, if you are sending only 1 data by publish.
I would only change the last line to cast the value to a Number, so it can be plotted in charts and others.

payload = [{ variable: splited_topic[2], value: Number(mqtt_data.value), serie }];

If you plan later on to send string payloads, such as ON or OFF for example (not boolean), you may need to add an extra code to check if it can be converted. Such as:

const formated_value = Number.isNaN(Number(mqtt_data.value)) ? mqtt_data.value : Number(mqtt_data.value);
payload = [{ variable: splited_topic[2], value: formated_value , serie }];

because the reply was so late , i had deleted the device and re added one quick for the screen capture. unfortunately i had a cut and paste error. but regardless the variables are not found

Well, that’s unfortunatelly. Anyway, if you don’t see Result of [device] payload parser in your Live Inspector, it means the action is not running for your topic, and not trying to store the data in the device’s bucket.
If you would check this again, at least you know here the issue is.

Actually i still do not know that the issue is, it was set up exactly how you told me to set it up last month, and It was not working, so after a month of no response from you or the rest of the support team i deleted it