Using moment.js to compare times in function node

Hi, I can't seem to work out why I always get a 'false' result when comparing 2 times in a nodered function with moment.js.

Taking the current time, and the sunset time and trying to test if it's before sunset.

appreciate any help

var sunset_time = global.get('homeassistant').homeAssistant.states["sun.sun"].attributes.next_setting;


var current_time = moment().local().format('HH:mm');
var sunset = moment.utc(sunset_time).local().format('HH:mm')

var test = moment(current_time).isBefore(moment(sunset))? "Yes" : "No"

var msg = {"payload": "current: "+current_time+" - sunset: "+sunset, "before sunset? ": test};

return msg;

Do your conditional ternary before you format the time object moment creates.

var sunset_time = global.get('homeassistant').homeAssistant.states["sun.sun"].attributes.next_setting;


var current_time = moment().local();
var sunset = moment.utc(sunset_time).local();

var test = moment(current_time).isBefore(moment(sunset))? "Yes" : "No"
var msg = {"payload": "current: "+current_time.format('HH:mm')+" - sunset: "+sunset.format('HH:mm'), "before sunset? ": test};

return msg;

or something similar, untested.

That did it !!!

THANK YOU

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.