Wait until FOR?

Anyone have an idea how to create a wait until for sequence?
Like wait until binary sensor is on for 10 seconds?

The only way I can think of is to use wait until “on”, then have a wait until “off” with 10 seconds timeout.
But is there any way to make it more elegant or better?

I use either this:

Is it on? > fow how long? > if it’s been on for more than 10’’, then do, otherwise set a delay as long as the remaining time in a trigger node.

[{"id":"f2260be1.473cb8","type":"api-current-state","z":"d4c74d4.b950eb","name":"binary on?","server":"9405c3fe.d0a6c","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"binary","state_type":"str","blockInputOverrides":true,"outputProperties":[{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":0,"forType":"num","forUnits":"minutes","x":1290,"y":1360,"wires":[["bafa0e06.5d5e6"],[]]},{"id":"bafa0e06.5d5e6","type":"switch","z":"d4c74d4.b950eb","name":">= 10''","property":"data.timeSinceChangedMs","propertyType":"msg","rules":[{"t":"gte","v":"10000","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":2,"x":1450,"y":1360,"wires":[["a552b218.c68"],["40e2e746.da1e08"]]},{"id":"25f094db.337a5c","type":"trigger","z":"d4c74d4.b950eb","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"250","extend":true,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1435,"y":1420,"wires":[["f2260be1.473cb8"]],"l":false},{"id":"a552b218.c68","type":"debug","z":"d4c74d4.b950eb","name":"Do","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1610,"y":1340,"wires":[]},{"id":"40e2e746.da1e08","type":"change","z":"d4c74d4.b950eb","name":"Set delay","rules":[{"t":"set","p":"delay","pt":"msg","to":"10000 - $number(data.timeSinceChangedMs)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1320,"y":1420,"wires":[["25f094db.337a5c"]]},{"id":"502e9d10.37d554","type":"inject","z":"d4c74d4.b950eb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1120,"y":1360,"wires":[["f2260be1.473cb8"]]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

I also use this one:

Using the opposite logic in a wait until node. If it goes off, do nothing. If it doesn’t go off in 10’’, use the timeout output to Do.

[{"id":"206f7975.7118c6","type":"ha-wait-until","z":"d4c74d4.b950eb","name":"","server":"9405c3fe.d0a6c","version":0,"outputs":2,"entityId":"binary","entityIdFilterType":"exact","property":"state","comparator":"is","value":"off","valueType":"str","timeout":"10","timeoutType":"num","timeoutUnits":"seconds","entityLocation":"","entityLocationType":"none","checkCurrentState":true,"blockInputOverrides":true,"x":1350,"y":1600,"wires":[[],["db6d7bae.6ae518"]]},{"id":"3fadcae9.103f96","type":"inject","z":"d4c74d4.b950eb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1170,"y":1600,"wires":[["206f7975.7118c6"]]},{"id":"db6d7bae.6ae518","type":"debug","z":"d4c74d4.b950eb","name":"Do","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1510,"y":1620,"wires":[]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

Depending on the scenario one works better than the other.

1 Like

Neither of those work.
Just to give some more context on the problem.
I have a bed sensor that determine if it needs to send another alarm to my phone or not.
So the phone rings in the morning then the sensor waits for me to get out of bed, what can happen is I have been pushed out to a corner of the bed and in order to reach the phone I need to lift myself and twist to get on my other side. This action lifts the pressure from the bed sensor and the wait until node says “Ok, he’s up!” when I’m actually just moving around in bed.
So therefore I need something that says I have been out of bed for 10 consecutive seconds, not just on, off, on, off, on, and accidentally being on at the moment of next read.

The first one just waits for ten seconds and looks again, and that could be fooled by toggling sensor.
Another issue seems to be that it only works once. I tried it with the interactive sensor on the phone and the first time the delay happened, the second time I injected the message (while screen being on the whole time) just passed through.

The second just looks for off, which is not really the same. Or I have misunderstood it.

there is a delay node you can reset every time you need (ie your bed sensor changes from off to on)

I believe none of the previous will work. I do use this when I want to have an advanced restart persistent wait until node. I believe it’ll serve your needs. Set your binary and on/off states properly.

Phone rings > enable binary node > binary on, start countdown. binary off, stop countdown. > when countdown ends, disable binary node.

[{"id":"871766e7.5884c8","type":"inject","z":"88e031b0.7ec12","name":"Phone rings","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1570,"y":1380,"wires":[["bf3b17c9.42ebb8"]]},{"id":"bf3b17c9.42ebb8","type":"change","z":"88e031b0.7ec12","name":"enable","rules":[{"t":"set","p":"payload","pt":"msg","to":"enable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1740,"y":1380,"wires":[["94c39b02.a761f8"]]},{"id":"76aff5ee.0ab55c","type":"change","z":"88e031b0.7ec12","name":"disable","rules":[{"t":"set","p":"payload","pt":"msg","to":"disable","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1740,"y":1420,"wires":[["94c39b02.a761f8"]]},{"id":"94c39b02.a761f8","type":"trigger-state","z":"88e031b0.7ec12","name":"binary","server":"9405c3fe.d0a6c","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary","entityidfiltertype":"exact","debugenabled":false,"constraints":[],"inputs":1,"outputs":4,"customoutputs":[{"messageType":"default","messageValue":"","messageValueType":"json","comparatorPropertyType":"current_state","comparatorType":"is","comparatorValue":"on","comparatorValueDataType":"str","comparatorPropertyValue":"new_state.state"},{"messageType":"payload","messageValue":"reset","messageValueType":"str","comparatorPropertyType":"current_state","comparatorType":"is","comparatorValue":"off","comparatorValueDataType":"str","comparatorPropertyValue":"new_state.state"}],"outputinitially":false,"state_type":"str","enableInput":true,"x":1870,"y":1400,"wires":[[],[],["2a84c890.63f098"],["2a84c890.63f098"]]},{"id":"2a84c890.63f098","type":"trigger","z":"88e031b0.7ec12","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"10","extend":false,"overrideDelay":false,"units":"s","reset":"reset","bytopic":"all","topic":"topic","outputs":1,"x":2040,"y":1420,"wires":[["76aff5ee.0ab55c","5737665f.160ac8"]]},{"id":"5737665f.160ac8","type":"debug","z":"88e031b0.7ec12","name":"Do","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2200,"y":1420,"wires":[]},{"id":"9405c3fe.d0a6c","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]
1 Like

I think it’s far easier if you flip your perspective and use a different trigger. For instance, the events:state node has an actual trigger to fire if the entity’s state is off for a certain amount of time. You can then set that to 1 minute. Only if the entity is off for one whole minute (so 60s in a row), will the node trigger and thus start the flow. If the state becomed on even for a millisecond in that minute, it resets and wait’s till the state off has been met for one consecutive minute.

Use your bed sensor as the trigger and use conditions to determine wether or not the flow should continue (like a time range, input from your alarm or an input_boolean that you turn on automatically when your alarm goes off).

ALTERNATIVE

You could also use a current:state node where it continues if the state is off for X amount of time. If it hasn’t met the condition, you can loop it back with a delay node. So it keeps checking it untill the state has been off for the amount of time.

Short example with a loop:
image

Make sure that if there is a scenario where it will not reach off state for that amount of time, you can reset it so the automation doesn’t loop non-stop (maybe by using your alarm input or an input_boolean as a condition in the flow).

2 Likes

I see that this works but I don’t understand it. I have never used the binary node and unsure what it does, I need to have a much deeper look at what this does.
Thanks

I understand what you say but I don’t think I can use a different trigger.
I’m on the bed so the state doesn’t change if I’m asleep. I could poll and see if time is == alarm time, but that is essentially the same as triggering on time.

This however makes sense. I did not have for in my current state node so I had to update to see how that works.
I think I could even do without the loop part.

I’m not sure either of these makes it more robust than a double wait until nodes.
I will have to see what I can do with these concepts and test them out in real life.

Thank you both!

Just to clarify, my later suggestion is the same as this one.

Phone rings > Turn on the switch.binary node. Switch.binary node checks the state of your pressure sensor. When the pressure sensor is on for 10 seconds > turn off switch.binary so it triggers just once and continue with the flow.

Still, you may find your solution better.

What about using a state machine?

I did this:

[
    {
        "id": "1219f0491bb6de0e",
        "type": "state-machine",
        "z": "418add2555d0b90c",
        "name": "",
        "triggerProperty": "payload",
        "triggerPropertyType": "msg",
        "stateProperty": "payload",
        "statePropertyType": "msg",
        "initialDelay": "0",
        "persistOnReload": true,
        "outputStateChangeOnly": true,
        "throwException": false,
        "states": [
            "pending",
            "start",
            "end"
        ],
        "transitions": [
            {
                "name": "start",
                "from": "*",
                "to": "start"
            },
            {
                "name": "end",
                "from": "start",
                "to": "end"
            },
            {
                "name": "pending",
                "from": "end",
                "to": "pending"
            }
        ],
        "x": 480,
        "y": 860,
        "wires": [
            [
                "54902641425219d1"
            ]
        ]
    },
    {
        "id": "57fe02b003784955",
        "type": "server-state-changed",
        "z": "418add2555d0b90c",
        "name": "Waschmaschine angeschaltet",
        "server": "1aba71d.8a9a38e",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "sensor.shellyplus1pm_b48a0a21d8b4_switch_0_power",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "150",
        "halt_if_type": "num",
        "halt_if_compare": "gt",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": "2",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "start",
                "valueType": "str"
            }
        ],
        "x": 160,
        "y": 800,
        "wires": [
            [
                "1219f0491bb6de0e"
            ],
            []
        ]
    },
    {
        "id": "f08c4d543e6ff02b",
        "type": "server-state-changed",
        "z": "418add2555d0b90c",
        "name": "Waschmaschine ausgeschaltet",
        "server": "1aba71d.8a9a38e",
        "version": 4,
        "exposeToHomeAssistant": false,
        "haConfig": [
            {
                "property": "name",
                "value": ""
            },
            {
                "property": "icon",
                "value": ""
            }
        ],
        "entityidfilter": "sensor.shellyplus1pm_b48a0a21d8b4_switch_0_power",
        "entityidfiltertype": "exact",
        "outputinitially": false,
        "state_type": "str",
        "haltifstate": "3",
        "halt_if_type": "num",
        "halt_if_compare": "lte",
        "outputs": 2,
        "output_only_on_state_change": true,
        "for": "2",
        "forType": "num",
        "forUnits": "minutes",
        "ignorePrevStateNull": false,
        "ignorePrevStateUnknown": false,
        "ignorePrevStateUnavailable": false,
        "ignoreCurrentStateUnknown": false,
        "ignoreCurrentStateUnavailable": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "end",
                "valueType": "str"
            }
        ],
        "x": 150,
        "y": 880,
        "wires": [
            [
                "1219f0491bb6de0e"
            ],
            []
        ]
    },
    {
        "id": "4ca3507749b998ce",
        "type": "api-call-service",
        "z": "418add2555d0b90c",
        "name": "Push notification senden",
        "server": "1aba71d.8a9a38e",
        "version": 5,
        "debugenabled": false,
        "domain": "notify",
        "service": "notify",
        "areaId": [],
        "deviceId": [],
        "entityId": [],
        "data": "{\t   \"title\":\"Waschmaschine fertig!\",\t   \"message\":\"Bitte ausräumen\"\t}",
        "dataType": "jsonata",
        "mergeContext": "",
        "mustacheAltTags": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "pending",
                "valueType": "str"
            }
        ],
        "queue": "none",
        "x": 970,
        "y": 840,
        "wires": [
            [
                "1219f0491bb6de0e"
            ]
        ]
    },
    {
        "id": "54902641425219d1",
        "type": "switch",
        "z": "418add2555d0b90c",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "end",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 750,
        "y": 780,
        "wires": [
            [
                "4ca3507749b998ce"
            ]
        ]
    },
    {
        "id": "1aba71d.8a9a38e",
        "type": "server",
        "name": "Home Assistant",
        "addon": true
    }
]

I think you want the “delay” function

image