Function no longer working checking if time is between two input date times

Hello this code used to work but now no longer does any idea as to why? It checks if the current time falls between two times that are input date times.

const states = global.get('homeassistant').homeAssistant.states;
const now = new Date();
const start = states['input_datetime.alarm1'].attributes;
const end = states['input_datetime.alarm2'].attributes;

if(now.getHours() >= start.hour && now.getMinutes() >= start.minute
   && now.getHours() <= end.hour && now.getMinutes() <= end.minute) {
    return msg;
}

As I see it there is three possibilities.

Is your Home Assistant called homeassistant?

Does alarm1 exist?
Does alarm2 exist?

You could try the following to do some debugging

const states = global.get('homeassistant').homeAssistant.states;
const now = new Date();
const start = states['input_datetime.alarm1'].attributes;
const end = states['input_datetime.alarm2'].attributes;
msg.start = start;
msg.end = end;

return msg;

/*if(now.getHours() >= start.hour && now.getMinutes() >= start.minute
   && now.getHours() <= end.hour && now.getMinutes() <= end.minute) {
    
}*/

and add a debug node with complete message.
What does start and end say?

thanks for reply. I also tried this to rule out the first chunk and nothing works either.

const now = new Date();
const start = msg.payload1;
const end = msg.payload2;

if(now.getHours() <= start.hour && now.getMinutes() <= start.minute
   && now.getHours() >= end.hour && now.getMinutes() >= end.minute) {
    return msg;
}

nothing gets output from the function node

What is payload1 and 2?

I see now that the return msg; is inside the if.
Move that outside and try the code I suggested.

this returns this info the inputs are correct here the alarm in my example is not my inputs.

object
_msgid: "5d52b392.476a2c"
topic: ""
start: object
editable: true
has_date: false
has_time: true
hour: 8
minute: 0
second: 0
timestamp: 28800
friendly_name: "Jarvis Start Time"
icon: "mdi:clock-outline"
end: object
editable: true
has_date: false
has_time: true
hour: 22
minute: 0
second: 0
timestamp: 79200
friendly_name: "Jarvis Stop Time"
icon: "mdi:clock-outline"

That is not from a debug node.
In node red, add a debug node connect it to the function node and change the setting to complete message.

it is from debug node

Sorry…

I can’t replicate the problem.
Could you use date and time in the input datetimes? That would enable you to use a simpler if statement with the timestamp instead.

Has to be an issue with the logic if I look at any variables it gets the correct info.

This used to work so an update to node red must have broken something.

Hi!

You cannot compare times this way. Say, you would like to compare 7:56 (now) with 8:05 (start). Then now is obviously “smaller” than start, but now.getMinutes() <= start.minute is false.

Maybe, in your case it is better to calculate hours to minutes and then compare minutes, like:

if((now.getHours() * 60 + now.getMinutes()) <= (start.hour * 60 + start.minute)
   && (now.getHours() * 60 + now.getMinutes()) >= (end.hour * 60 + end.minute)) {
    return msg;
}

Or just use the commonly available timestamp.
That way someone else did all the hard work.

Also my first idea, but I think in this case start and end are no Date objects, so you can’t get a timestamp from them.

There is, but it counts from 00:00 each day.
But if the input date times are created as date and time then it will have timestamp.

You are right, then one can use modulo to strip the day part from the now timestamp:

var now = new Date().getTime() % 86400;
if(now <= start.timestamp 
   && now >= end.timestamp) {
    return msg;
}

Except that it’s not always 86400 seconds per day.

It’s always better to use known codes and functions instead of building your own

Yea these are just times no date.