Inject node at a specific time and making this time configurable from Home Assistant UI?

Normally I use the inject node for example executing something at everyday at 22:30.

What I want is to have this time configurable from Home Assistant UI.

So I have made a input_datetime helper (only time) for storing this time value.

Unfortunately you couldn’t “bind” this time helper to the node-red inject node.

So what is the best way to do this?

Using a poll state node (check every second) and a function node. And in the function node you compare the current time with the HA helper time (so when hour and minute are the same) and then setting a context/flow/global variable with the date of today and execute an “output” of the function node.

And at the start of the function node you check if context/flow/global variable is set to today then you have to do nothing.

Is this easiest way or is there a better way?

Just curious…why do you want to adjust the time from the UI and how often is the time adjusted currently?

Yes I know it will be not very often. But it is nice to have all the setup from the home assistant ui possible.

This should give you some ideas:

Edit: put your global variable created by your input_datetime helper into the time node.

Thanks! That node I have never used before :slight_smile:

What I was planned to use was this kind of construction:


.

[{"id":"53512b122b46f88d","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"4d7009beb27da1cd","type":"poll-state","z":"53512b122b46f88d","name":"Some entity","server":"63f26c18.71f4f4","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"1","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.zijraam_woonkamer_buiten_lux","state_type":"str","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":150,"y":500,"wires":[["5a27f4298ac28e2f"]]},{"id":"5a27f4298ac28e2f","type":"function","z":"53512b122b46f88d","name":"Some logic","func":"// Outputs\n// [\n//      signal to simulate the inject node at the specific time from the HA helper\n//      signal to do something else...\n// ]\n\nvar msgInjectSimulation, msgSomethingElse = null;\n\nconst haStates = global.get(\"homeassistant.homeAssistant.states\");\n\nconst closeCoversStaticTime = haStates[\"input_datetime.close_all_covers_static_time\"];\n\nvar injectLastExecutedDate = global.get(\"some_name_executed_date\", \"file\");\n\nconst currentDateTime = new Date(Date.now());\n\nif (currentDateTime.getHours() == closeCoversStaticTime.attributes.hour &&\n    currentDateTime.getMinutes() == closeCoversStaticTime.attributes.minute &&\n    (injectLastExecutedDate == undefined || \n     currentDateTime.getFullYear() != injectLastExecutedDate.getFullYear() ||\n     currentDateTime.getMonth() != injectLastExecutedDate.getMonth() ||\n     currentDateTime.getDate() != injectLastExecutedDate.getDate())\n   )\n{\n    global.set(\"some_name_executed_date\", currentDateTime, \"file\")\n\n    msgInjectSimulation = {\n        \"payload\": \"Injecting...\"\n    };\n}\n\n// ..... all other logics ...\n// ...\n// ...\n// ...\n\nreturn [msgInjectSimulation, msgSomethingElse];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":500,"wires":[["c4195a96f5427ca9"],["cd5b898deaec76b7"]]},{"id":"c4195a96f5427ca9","type":"debug","z":"53512b122b46f88d","name":"Inject once at specific time from input helper HA","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":460,"wires":[]},{"id":"cd5b898deaec76b7","type":"debug","z":"53512b122b46f88d","name":"Something else....","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":570,"y":540,"wires":[]},{"id":"63f26c18.71f4f4","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"id","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

But with this time node it is very easy!

I suppose my comment is quite late.

However, you could also make an input button in HA, and then a state change node in NR would trigger the flow when the input button is pressed.

You could place the button on a dashboard, or in a widget on your Android / iOS Action on Apple device.

I use this type of setup to submit form data, on demand, instead of on schedule.