I’ve got a simple function to compare new Date(); with msg.data.last_changed but for some reason the if statement doesn’t trigger. Im not really good at JS and not sure what the issue could be here. Any ideas?
var sensorname = msg.data.attributes.friendly_name
var lastchanged = msg.data.last_changed
var today = new Date();
if (today > lastchanged) {
msg.payload = {
"today": today,
"sensorname": sensorname,
"lastchanged": lastchanged
};
return msg;
}
When I run debug without the IF statement I can see the today var is larger than lastchanged but IF doesnt trigger
Can you share the node that is loading the initial data?
You might want to move the return msg; line to the very end (outside the if statement) since if the if statement doesn’t get hit it won’t be returning anything.
Other than that it might be an issue with comparing dates, try converting the lastchanged variable to a date object then compare it.
var lastchanged = new Date(msg.data.last_changed);