Payload Parser - time variable

Hello,

I can use some help / sample code if possible (consider me a newbie ! when it come to writing code).

I have my sensor reporting ‘time’ (device time) as part of the payload data.

I would like to change the value of this ‘device time’ to $TIME$ (the time data was received).

What would the custom parser snippet look like ?

Thank you in advance .

-------

Long version:- I am using dragino sensors with Helium, to the best of my knowledge there is no simple way to sync the device time clock .. (unless i am missing something) … hence I am looking to correct this via payload parser ..

If someone has a better suggestion, I am all ears !.

-------

Faisal

Hi Faisal,

From what I understood, your device has the incorrect time and so it is sending the payload data with that incorrect time property.

You can modify the time property of all the existing variables in the payload by using the example code below:

// Handle Received Data

const data = payload.find((x) => x.variable === “payload”);

if (data) {

const currentTime = new Date().toISOString();

payload = payload.map((item) => {

return {

  ...item,

  time: currentTime,

};

});

}

Awesome ,

Can I ask you some sample code if I wanted to create a new variable called ‘modifiedtime’ which just the hour and min, in 24hr format without date or seconds etc ?

Thank you very much in advance.

Faisal

sanity check.. is this syntax correct ? can it be improved ?

Thank you in advance..

inserting a new variable "notify’ when the alarm =True and it is between 7am and 9pm .

--------------------------------------

const alarm = payload.find( x => x.variable === ‘alarm’);
if (alarm) {
const c_time = new Date();
const c_hour = c_time.getHours();
if (alarm.value == “True” && (c_hour >= 7 && c_hour <= 21) {
payload.push({“variable”: “alert”, “value”: “notify”, “group”: alarm.group});
};

Your code is almost correct, but there’s a small syntax error. The closing parenthesis for the if condition is missing. Here’s the corrected version:

const alarm = payload.find(x => x.variable === 'alarm');
if (alarm) {
  const c_time = new Date();
  const c_hour = c_time.getHours();
  if (alarm.value == "True" && (c_hour >= 7 && c_hour <= 21)) {
    payload.push({ "variable": "alert", "value": "notify", "group": alarm.group });
  }
}

Awesome ! Thank you very much !

Greatly appreciated !.