Help with wait until?

Hello, I am extremely new to home assist let alone node red lol.

I have begun to create a flow which so far works as intended however I have hit a stumbling block.

I want the intention shown in the screenshot as a wait until node but I dont know how to convert the two.

So the alarm set on my phone in the screenshot if I link that to a button it fires -15 seconds before the alarm goes off which I want in the flow before the next step, I assume its the wait until node but idk?

Tldr how do I make it so the toggle off then goes onto wait until 15 seconds before my phones next alarm before going onto triggering the next event.

I hoped it was a case of simply connecting the two but the time node doesnt acept inputs only outputs.

Node-RED flows start with a trigger - time/event/manual and typically end with doing something like an Action.

The Time Node (not to be confused with the Time Entity node) is a ‘trigger node’ to start a flow, and will accept any entity that holds a valid ‘time’ in either the state or another attribute property.

I assume that you have the HA companion on your phone, which provides several phone-sensors, one of which is ‘next alarm’. This will have a state value of an ISO timestamp if set (pending) or ‘unavailable’ if not set.

Using ‘sensor.zach_phone_next_alarm’ state property will set the Time node to trigger (start a new message into the flow) when the alarm time is reached.

If you want to continue your start a new flow, staring at 15 seconds before the alarm time, then you can simply add -15 seconds Offset in the node setting. This will cause the Time node to trigger a new message, 15 seconds (or thereabouts) before the next phone alarm, and you can then continue your flow from this. Naturally this ‘breaks the flow’ and if you need to carry over existing data in your msg.payload, then this may have to go into a temporary flow context variable.

When the actual alarm does go off, then the phone-next-alarm sensor goes to ‘unavailable’ (since there is no alarm set) and the Time node appears to generate an error as it does not like ‘unavailable’, however I don’t think that this stops the flow from working.

TL:DR - you don’t need to, just set -15 second offset in the Time node.

I hope that this answers your question!

Hi thanks for helping.

Sorry I probably didnt make it clear, what you suggested I do I already was doing, I saw that I could use the - 15 seconds to trigger an action at - 15 seconds before my phones alarm already.

What I need is the same time value but in a wait until node so I can have it as a variable in my current flow not to start a new flow.

So my flow is I scan a nfc tag which then checks the state of an input boolean, if true it sets it to false, the next step I want is for it to wait until - 15 seconds of my phone alarm time before moving onto doing the next action.

I tried using the wait until with the sensor and i notice you can set wait until in the node however it acepts more broad stuff eg regex, boolean, string, number etc.

I couldnt figure out how to use this for - 15 seconds?

I tried to use the debug node like you have but I wasnt sure really how to understand the output of the sensor.

You can use node-red-contrib-traffic for this. Connect the “action” node that toggles the input_boolean to the “traffic” node to enable it and allow data to pass through. Then, connect the “time” node to the “traffic” node as well, so it only passes messages when the “traffic” node is enabled. After the data has passed through, ensure the “traffic” node is disabled again to stop further messages.

Hi thanks.
Reading the documentation for this node, it does seem this is what could work, however… when I use it the light goes green and allows the data to go through but the next action doesn’t seem to trigger?

Here’s a working example setup:

  • The “allow” inject node represents the “action” node that turns off the input_boolean. Set the output properties of this node to msg.payload = "allow", which opens the “traffic” node.
  • The “data” inject node represents the “time” node. When the traffic node is open, and it’s 15 minutes before your alarm, it triggers and passes through the “traffic” node to the “debug” node, continuing the flow.
  • The “change” node named “block” sets the “traffic” node back to the blocking state after the data passes through.

image

[{"id":"abd086c3ab8e5f97","type":"traffic","z":"c89d915bdff0f798","name":"","property_allow":"payload","filter_allow":"allow","ignore_case_allow":false,"negate_allow":false,"send_allow":false,"property_stop":"payload","filter_stop":"block","ignore_case_stop":false,"negate_stop":false,"send_stop":false,"default_start":false,"differ":false,"x":810,"y":848,"wires":[["e4e1390f67abb8b5","7341db5cfdda18ae"]]},{"id":"dc55187da5be824a","type":"inject","z":"c89d915bdff0f798","name":"allow","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"allow","payloadType":"str","x":626,"y":848,"wires":[["abd086c3ab8e5f97"]]},{"id":"e4e1390f67abb8b5","type":"debug","z":"c89d915bdff0f798","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":980,"y":848,"wires":[]},{"id":"4422749035e827ad","type":"inject","z":"c89d915bdff0f798","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":626,"y":896,"wires":[["abd086c3ab8e5f97"]]},{"id":"7341db5cfdda18ae","type":"change","z":"c89d915bdff0f798","name":"block","rules":[{"t":"set","p":"payload","pt":"msg","to":"block","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":974,"y":896,"wires":[["abd086c3ab8e5f97"]]}]
1 Like

I am sure that Kermit’s solution is indubitably the best one all-round, but as an academic exercise I wanted to follow this through to see if I get ‘wait until’ working.

I confess I am still unsure as to exactly what you want here. However, if you are indeed looking for ‘wait until 15 seconds before the alarm time set on my phone’ then I have three (other) ways to achieve this.

Task: Message arrives - held (wait / pause) until 15 seconds before the phone-next-alarm time (hh:mm but could be tomorrow…)

  1. Use the standard delay node

On message arrival, use the Current state node to obtain the alarm time and compute a millisecond delay between now and (alarm -15 seconds). Use this in msg.delay as input to the Delay node. The Delay node will wait for the correct duration.
Simple enough and it works, but I don’t like having delays like this in a flow for longer than a minute or so as it is not recoverable over a restart or redeploy.

  1. Use a Time node and set a new time based on alarm time less 15 seconds.

This works, but since the time node only accepts hh:mm:ss it can only be used for alarms set for later today. There is also the challenge of dealing correctly with the alarm time and timezones.

  1. Use the ‘Wait Until’ node

Normally, the subject entity of the Wait node is also the one used for the conditional testing, so we could easily ‘wait until the alarm goes off’. Triggering the node 15 seconds before the alarm requires a bit of upside-down thinking.

I use the sensor.time entity a lot - easy to set up (use the Time & Date integration) and you have “hh:mm” available in local time. HA updates this every minute. If we use this entity as the Wait until subject, then the node will ‘wake up’ every minute. We can then use a bit of JSONata in the test condition which will read the alarm state and return ‘true’ if we are 2 minutes before the alarm time.

(
    $alarm:=$entities('sensor.a015_next_alarm').state;
    $to_ms:= $exists($alarm) and $alarm!='unavailable' ? $toMillis($alarm) : "not-set";
    
    $type($to_ms)="number" and $to_ms-$millis() -120000 <0; 
)

I had to add in extra code since the JSONata $toMillis() function throws an error if presented with anything other than an ISO formatted timestamp, and ‘unavailable’ generates an error messages in the log every minute.

So, this will (it works) release the message 2 minutes ahead of time. HA updates sensor.time every minute, but randomly during the minute, hence the node will release the message somewhere between 2 and 1 minutes ahead of the alarm time. The bit I had to sleep on was getting to the exact second you want, and this is done by using a Delay node with the delay time calculated in the output properties on the Wait node trigger, again using JSONata.

$toMillis($entities('sensor.a015_next_alarm').state)-$millis()-18000

I am using 18 seconds to fine-tune to the exact time required.
Since we are triggering the node ahead of the alarm time, the alarm time will be ISO formatted and not ‘unavailable’ so this should not error out.

It works - 11:00 alarm, Wait until released 2 minutes ahead, Delay node releases message 15 seconds ahead.

As an added bonus, sending a messing into the Wait Until node when an alarm is not set will cause the node to wait (potentially forever) but subsequently setting an alarm will release the message when the correct time comes around.

Purely an academic exercise… Interesting though, made me think of how I could control my Solar Inverter battery charging or Grid Saving Sessions from my smart phone.

Not sure what I am doing wrong but it doesn’t seem to be continuing (in my case turning a switch off for a test).


I see the input boolen switch off, and the sensor turns on the traffic node, but once it reaches the switch action node nothing happens.

As for @Biscuit answer those are quite over my head sadly. As expressed in my OP I am extremely new to this.

Edit: I also just tried to manually test if the problem was the action node and it works fine.
Screenshot_20250125_174953

Further edit:NVM not sure really what I did to change things but its working as intended now lol.Thanks. Kermit answer did what I want

Okay. Different issue now. It was working fine with the action node set to notify.mobile_app_mobilename. However due to some other reasons I decided to keep using the home assistant YAML for the notification side and as such made scripts for these, now when I pipe the traffic node into the action node of script.scriptname or script.turn_on it no longer works. Do I need to do something different to have it run scripts?

nvm I put the allow msg.payload on the wrong node :sweat_smile: