How to compare today with last_changed

Hi,

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
image

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);

Hi!
Thanks for your reply. Sorry I was out in the morning.
This is a simple inject every 1hr check state.

The check state node debug data looks like this

Yep I would try using the Date object then comparing that because at the moment you are comparing a date object with a string object.