A clean way to delay until a state has changed or for a period of time, whichever happens first

I’m adding a new type of sensor to my irrigation system that has a variable response time. it’s still in prerelease so it has some rough edges. I have 10 of them.

Sometimes it responds instantly but it can take up to 5 minutes depending on how many calculations it needs to do.

This is a hardware limitation due to its low powered SOC.

sometimes it returns an answer That HA can’t understand so its return is read as Unkown or it looks like it’s unavailable. this is due to the pre release firmware and should be fixed in the final product.

I want to introduce a wait period into my script that after the script has been triggered by something and will ask tbe sensor for a response and then will wait until the devices state changes, or for a fixed period of time. whichever happens first before moving on to the next sensor.

For example if it takes 10 seconds the script pauses until the entity state changes and then continues, but if it hasn’t changed after 2 minutes it gives up and continious with out it.

I’ve just strung these together using triggers, Or statements and timed waits. it functions but it lacks elegance.

is there a cleaner way to do this?

You need to give us your current script/setup if you have one.

But broadly, yes, this should be doable in a cleaner way. I’d be looking at a wait_for_trigger or wait_template with a timeout, so it continues as soon as the sensor changes, but gives up after whatever limit you set and moves on. Home Assistant scripts support both waiting for a trigger/condition and adding a timeout, with control over whether the script continues after the timeout.

If your sensor can briefly go unknown or unavailable, you’ll probably also want to show how you’re handling those states now, because that may matter more than the wait itself.

I’m dealing with a pre release so I can’t share anything that they gave me, even if it’s basic code.

It’s making getting outside help tricky. I can’t even say what it’s measuring.

If you can’t share anything, go to the HA Docs ans read up on triggers and templates and try things.
We can’t really help much without causing accidental leaks of proprietary info, and we don’t want that.

1 Like

it’s amazing how complicated simple problems get

Then I suggest you ask AI for help :slight_smile:

I like problems :clown_face:

oop cant share anything well you have 2 options here now.

break the rules
or
ask ai lol

I think that you may have nailed the solution in your first post.

1 Like

I did, and it told me that I shouldn’t make presumptions that the existing code was inelegant because standards of beauty were often cultural and changed with time.