# State managment in homey?

**URL:** <https://community.homey.app/t/state-managment-in-homey/47294>\
**Category:** Flows\
**Created:** [May 11, 2021, 3:02pm UTC](https://community.homey.app/t/state-managment-in-homey/47294 "2021-05-11T15:02:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Medin](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/michael_medin/32/2311_2.png) [@Michael\_Medin](https://community.homey.app/u/Michael_Medin)\
**Post date:** [May 11, 2021, 3:02pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/1 "2021-05-11T15:02:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 11, 2021, 3:38pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/2 "2021-05-11T15:38:52Z")

</div>

> [@Michael\_Medin](#):
>
> Flow 1: if value changed and value \> 50 and foo = false; then set foo = true

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

> [@Michael\_Medin](#):
>
> Flow 3: if variable has changed and is true; then do something

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

---

<div class="post-metadata">

**Author:** ![Henrik\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrik_johnsson/32/13707_2.png) [@Henrik\_Johnsson](https://community.homey.app/u/Henrik_Johnsson)\
**Post date:** [May 11, 2021, 3:43pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/3 "2021-05-11T15:43:26Z")

</div>

> [@Rocodamelshekima](#):
>
> Flow 1: if value changed and value \> 50 and foo = false; then set foo = true ELSE set to false  
> Flow 2: not needed anymore.

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

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 11, 2021, 3:45pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/4 "2021-05-11T15:45:09Z")

</div>

> [@Henrik\_Johnsson](#):
>
> 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](https://community.homey.app/t/get-a-notification-when-the-washingmachine-is-ready/9372).  
Am just reacting on numbers he gave.

---

<div class="post-metadata">

**Author:** ![Michael\_Medin](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/michael_medin/32/2311_2.png) [@Michael\_Medin](https://community.homey.app/u/Michael_Medin)\
**Post date:** [May 11, 2021, 3:46pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/5 "2021-05-11T15:46:11Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 11, 2021, 3:47pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/6 "2021-05-11T15:47:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_Medin](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/michael_medin/32/2311_2.png) [@Michael\_Medin](https://community.homey.app/u/Michael_Medin)\
**Post date:** [May 11, 2021, 3:54pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/7 "2021-05-11T15:54:27Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Henrik\_Johnsson](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/henrik_johnsson/32/13707_2.png) [@Henrik\_Johnsson](https://community.homey.app/u/Henrik_Johnsson)\
**Post date:** [May 11, 2021, 3:55pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/8 "2021-05-11T15:55:52Z")

</div>

> [@Rocodamelshekima](#):
>
> All depends of where u wanna use it for. If u wanna check if the dishwasher is ready best check [this topic](https://community.homey.app/t/get-a-notification-when-the-washingmachine-is-ready/9372).  
> Am just reacting on numbers he gave.

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

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 11, 2021, 3:57pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/9 "2021-05-11T15:57:12Z")

</div>

> [@Henrik\_Johnsson](#):
>
> your version is not equivalent.

Ow, i missed a 0 there yes, sorry.

![chrome_2021-05-11_17-58-01](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/9/a/9aca07bb29214644f88a448fd9bfc50508e98181.png)

---

<div class="post-metadata">

**Author:** ![Rocodamelshekima](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/rocodamelshekima/32/8209_2.png) [@Rocodamelshekima](https://community.homey.app/u/Rocodamelshekima)\
**Post date:** [May 11, 2021, 3:59pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/10 "2021-05-11T15:59:55Z")

</div>

But then it’s time for [H.O.O.P.](https://community.homey.app/t/h-o-o-p-hope-homey-object-oriented-programming/43709)

---

<div class="post-metadata">

**Author:** ![Edwin\_D](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_d/32/136417_2.png) [@Edwin\_D](https://community.homey.app/u/Edwin_D)\
**Post date:** [May 11, 2021, 7:32pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/11 "2021-05-11T19:32:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Eternity](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/eternity/32/102113_2.png) [@Eternity](https://community.homey.app/u/Eternity)\
**Post date:** [May 12, 2021, 12:29pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/12 "2021-05-12T12:29:07Z")

</div>

> [@Edwin\_D](#):
>
> capability became greater then/less than” clauses. T

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

---

<div class="post-metadata">

**Author:** ![Michael\_Medin](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/michael_medin/32/2311_2.png) [@Michael\_Medin](https://community.homey.app/u/Michael_Medin)\
**Post date:** [May 12, 2021, 1:03pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/13 "2021-05-12T13:03:53Z")

</div>

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

[https://homey.app/sv-se/flow/0zQGwB/](https://homey.app/sv-se/flow/0zQGwB/)

[https://homey.app/f/\_Y2HwB/](https://homey.app/f/_Y2HwB/)

---

<div class="post-metadata">

**Author:** ![Eternity](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/eternity/32/102113_2.png) [@Eternity](https://community.homey.app/u/Eternity)\
**Post date:** [May 12, 2021, 1:14pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/14 "2021-05-12T13:14:46Z")

</div>

Thanks @Michael_Medin !

---

<div class="post-metadata">

**Author:** ![Edwin\_D](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/edwin_d/32/136417_2.png) [@Edwin\_D](https://community.homey.app/u/Edwin_D)\
**Post date:** [May 12, 2021, 3:24pm UTC](https://community.homey.app/t/state-managment-in-homey/47294/15 "2021-05-12T15:24:23Z")

</div>

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