Use variable for inject time

Hey all, I want to make an alarm clock and need to trigger it at a certain time, but set that time in Home-Assistant. So using the inject node, how can I dynamically set the time with a variable etc.

Probably better on the home-assistant forum - we really need to know what functionality you are planning to have in HA and what in Node-red.

As a simple answer - capture the time set in HA and send it across as an MQTT string, parse that string and store it as a Flow/Global variable (as appropriate)

Run a flow to check that variable every minute and then act if it is equal to the current time

Craig

Thanks.
Yeah I know I can achieve that logic, I'm not sure if I'd go down that route or just stick with manually setting it in node-red. I'm all about minimising setup at the minute and this get's rid of a lot of code I have in HA.

The question is more node-red based though. Yes I can get an update from HA to update a flow variable. The question is, is there syntax to be able to use that directly in the inject node rather than having to query and compare every minute. It just seems running it 1440 a day is a bit overkill just for that one time it needs to fire. Or is this just something I shouldn't worry about as it's such a small query.

If you are gfoing to run it at a certian time each day, you can configure the inject node to run at a specific time and date.

If I understand correctly, you want to:

  • set the alarm time in HA
  • have the alarm execute in NR
  • have the alarm fire just once a day

Instead of using an inject node, you might take a look at node-red-contrib-schedex. The time sent from HA can be used to program it, as in this flow. (I've added stuff to make it act more like an alarm clock. :slightly_smiling_face:)

[{"id":"6d19fa46.1a46bc","type":"schedex","z":"303954f6.eff9ac","name":"alarm clock","suspended":false,"lat":"38.897517","lon":"-77.036542","ontime":"","ontopic":"","onpayload":"wake up!","onoffset":0,"onrandomoffset":0,"offtime":"","offtopic":"","offpayload":"wake up!!","offoffset":"","offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":490,"y":100,"wires":[["6ef3b6c5.215be8"]]},{"id":"b49e739f.c26a4","type":"inject","z":"303954f6.eff9ac","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":100,"wires":[["3b06217a.51b59e"]]},{"id":"3b06217a.51b59e","type":"change","z":"303954f6.eff9ac","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"ontime\":\"11:40\",\"offtime\":\"11:40\",\"offoffset\":\".5\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":100,"wires":[["6d19fa46.1a46bc"]]},{"id":"6ef3b6c5.215be8","type":"debug","z":"303954f6.eff9ac","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":100,"wires":[]}]
2 Likes

Just to add some other solution after some time :slight_smile:

Home assistant & NodeRed (common nodes).

Trigerring every X minutes by Inject.

[{"id":"f0a31c7da86d0ab8","type":"time-range-switch","z":"12523ce5cc64bb3e","g":"7cda82cb754e9358","name":"Shold run?","lat":"50.0543356","lon":"14.2217939","startTime":"19:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":1010,"y":740,"wires":[["3aa73e63f73b36d6"],[]]},{"id":"70cd066a7daf482b","type":"change","z":"12523ce5cc64bb3e","g":"7cda82cb754e9358","name":"Set time","rules":[{"t":"set","p":"__config.startTime","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"__config.lat","pt":"msg","to":"5.02","tot":"num"},{"t":"set","p":"__config.lon","pt":"msg","to":"54.22","tot":"str"},{"t":"set","p":"__config.endTime","pt":"msg","to":"22:00","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":740,"wires":[["f0a31c7da86d0ab8"]]},{"id":"34ddc09e1366a337","type":"api-current-state","z":"12523ce5cc64bb3e","g":"7cda82cb754e9358","name":"Get time","server":"xy","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_datetime.locking","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":640,"y":740,"wires":[["70cd066a7daf482b"]]}]

Hope all is well uploaded and possible to digest...