State managment in homey?

How do people handle states in homey flows?

A simple example: If power of my dishwasher is below 5 it is “stopped” and above 50 it is “started”.

The way I tend to handle these now are by setting a boolean variable and then listening to that variable.
Then I have a listener for that variable and when the value changes I run various flows.

This means I have:
Variable: foo
Flow 1: if value changed and value > 50 and foo = false; then set foo = true
Flow 2: if value changed and value < 5 and foo = true; then set foo = false
Flow 3: if variable has changed and is true; then do something
Flow 4: if variable has changed and is false; then do something

Now this works really well, but it is kind of verbose to create 4 flows and variable, especially if I have a lot of these, so is there a better way to handle these kind of scenarios?

// Michael Medin

Flow 1: if value changed and value > 50 and foo = false; then set foo = true ELSE set to false
Flow 2: not needed anymore.

Flow 3: if variable has changed and is true; then do something ELSE do something else
Flow 4: not needed anymore.

1 Like

Often you willl want to have a level of hysteresis to get a more robust behaviour, your example is missing that.

All depends of where u wanna use it for. If u wanna check if the dishwasher is ready best check this topic.
Am just reacting on numbers he gave.

Sure this was maybe too simplified. The reason for the other flows is that there often is more than one criteria like “it is also day”, or “the sun is shining” , you still want to update the state but not do the action and similar things… But I am looking more for a way to eliminate flow 1/3 and the variable…

Ok. But u wanna know if the dishwasher is ready AND the sun is shining?
That will indeed make it a bit more complicating.

A few more concrete examples I currently have:

TV Power dictates “tv is on” and then depending on if time and sunny I lower the blind and other times I control the lights.

Co2 level dictates “good air” if computer is on and daytime: flash the light red if bad and green if good (either case send notification).

That was just the point of my comment, your version is not equivalent.

Ow, i missed a 0 there yes, sorry.

chrome_2021-05-11_17-58-01

But then it’s time for H.O.O.P.

The new firmware 6.1.0 should eliminate the need for most, if not all of this with the new “capability became greater then/less than” clauses. They trigger only on the change from higher to lower or vice versa without the need of introducing booleans to see the toggle.

Also, for more complex cases I do not use booleans anymore, but instead switch on or off the flows that I want to stop acting. So I have a lot of flows that disable themselves, and have their counterparts re-enable them.

So basically, the state is kept in the enabled flag of the flow, also making Homey more efficient because it doesn’t need to check disabled flows.

2 Likes

Could you be so kind to show an example in a flow. Thanks!

Here are two flows I tried it out on, and it works really well… Glad I asked, will probably make my flows much simpler…

1 Like

Thanks @Michael_Medin !

@Michael_Medin has beaten me to the punch with examples. Couldn’t do much better.

1 Like