Use HA date/time input has the time on the inject node, "at a specific time"

Hi

I am running an automation with to shut down the kids PC at a given time on school days.
It uses the RPC-addon and is working fine.
Since discovering node red i have passed all, or almost all automations to node red.
This one wold be easy with an inject node, BUT i was trying to use an HA input bolean and a input time to set the on/off switch and set the hour at will.
Is there a way to use the info in the input time, the time that is set there and use it as the “at a specific time” time value?

Thx in advance

Best regards
Nuno Reis

Hi, surprisingly implementing such a basic automation like this turned out to be way too harder then I expected but I am using schedex for this. Have a look https://www.npmjs.com/package/node-red-contrib-schedex
Big timer is also another alternative but I found it too complicated.
There is an input time in ha and everytime it is updated, send the message to schedex to update the schedule time. In my case, morning alarm. I am on mobile but will try to post the flow tomorrow if you need…

Hello

Installing the node now, :stuck_out_tongue: thx.

PS: Of course the flow would be awsome too!! :smiley:

Here’s what I normally use for alarm set from HA using a input_datetime.

[{"id":"72f60bde.bf77a4","type":"inject","z":"5eb3594f.d294b8","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":726,"y":1040,"wires":[["fa8f3a6d.94aef8"]]},{"id":"be2dca8f.835398","type":"debug","z":"5eb3594f.d294b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":994,"y":1040,"wires":[]},{"id":"fa8f3a6d.94aef8","type":"function","z":"5eb3594f.d294b8","name":"","func":"const states = global.get('homeassistant').homeAssistant.states;\nconst now = new Date();\nconst alarm = states['input_datetime.alarm1'].attributes;\n\nif(now.getHours() === alarm.hour && now.getMinutes() === alarm.minute) {\n    return msg;\n}","outputs":1,"noerr":0,"x":866,"y":1040,"wires":[["be2dca8f.835398"]]}]
2 Likes

here is my whole morning alarm clock flow, doens’t handle the situation as easy as kermit’s :stuck_out_tongue: but whatever floats your boat, right… :slight_smile:

https://pastebin.com/6VZMYqC0

there are 2 current state nodes, checking if the alarm clock is “on” and if it’s a workday. maybe you can also change the workday sensor in HA and use it as a condition if “tomorrow” is a school day (come on give them some slack :wink: )
Now I don’t remember why did I put a switch node between alarm set and workday, you might get away without it if you clean the flow little bit.

1 Like

I am getting a “TypeError: Cannot read property ‘attributes’ of undefined” on the debug node.
You have on the node code homeAssistant, i tried with A and a, same error.
The input_datetime is correct, any tought on why the error on the debug?

EDIT:
Weirdly enought repasted the datetime_input name and started to work!

I will go with Kermit solution, is a smaller solution :stuck_out_tongue:
BUT thank you too, serkank, for the help also.

1 Like

How could you compare if the time is between two times that are input_datetimes? please

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;
}
1 Like

Thanks for that how would i also check if the time now is NOT between two times but the start and end times are msg.payload1 and msg.payload2?

I tried changing it to this:

const start = msg.payload1
const end = msg.payload2

and deleted the top line but nothing is happening I also dont know how to do the outside of the time bit :frowning:

tried this: but not working

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

The current time is going to be less than or greater than, it’s not going to be both.

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

Sorry to revive an old topic, but in looking for a solution to a problem I saw this post and it was exactly what I wanted to do. I however spent a bit of time trying to use this, but all I got was no output. What I came to realize is that you only should compare minutes if the hours were equal. I didn’t need the accuracy of minutes so I just removed the minute comparison. I’m also not proficient enough to rewrite it and nest everything correctly. Please don’t take this as criticism. This was the only post that got me to were I needed to be.

for some reason this doesnt work anymore for me any idea why? Thanks

Are you using the code exactly as shown in Kermit’s post? If so it will work intermittently, because it is not completely correct. In the code above the Minutes are always evaluated. They only need to be evaluated if a given hour is equal to start or end hour. In any other case only the hours need evaluated. I just don’t know how to code it…

You don;'t need to do that any more. In the latest versions of the node, Kermit has introduced a time node that does exactly that.
At input_time value it will trigger the flow.

1 Like

The function is not used to trigger a flow. It is used to evaluate any given time to see if it is currently between 2 set times.

No… The time function from the home assistant websocket palette does that. There are many time tools in various palettes, I am referring to this one.
When it is time it triggers the flow.

1 Like

Yes the new nodes trigger at one time this function is to check that the current time is between two times so it is not the same thing.

So I thought of a work around, You could create a Boolean input helper say called “In The Zone”. The set up 2 automations to turn it on and off at the required start and end times. Then you can evaluate it with current state in node red.

This would Linda work but would not be very reliable if you restart node red for example this would then fail.

I only use stuff that is fool proof so if HA and node red gets restarted for any reason everything corrects itself.

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;
const startDate = new Date();
const endDate = new Date();
startDate.setHours(start.hour);
startDate.setMinutes(start.minute);
startDate.setSeconds(0);
endDate.setHours(end.hour);
endDate.setMinutes(end.minute);
endDate.setSeconds(0);

if(now >= startDate && now <= endDate) {
  return msg;
}

2 Likes