# Determine if device was turned on/off by user or system

**URL:** <https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519>\
**Category:** Questions & Help\
**Tags:** app, flow\
**Created:** [March 31, 2021, 6:32am UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519 "2021-03-31T06:32:42Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [March 31, 2021, 6:32am UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/1 "2021-03-31T06:32:42Z")

</div>

Hi!

I currently control the lightning in the apartment based on presence in the room (motion sensors etc…). However, I need to implement some kind of temporary override behavior such that If I manually change the lightning, (using the app, homekit or physically flipping switch) i want the system to stop automation of the device for x amount of time. I have not seen any easy way of implementing this, how did you do it?

Typical use case is when I want to take a power nap in the living room or watch a movie.

---

<div class="post-metadata">

**Author:** ![Hugo](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/hugo/32/11530_2.png) [@Hugo](https://community.homey.app/u/Hugo)\
**Post date:** [March 31, 2021, 7:19am UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/2 "2021-03-31T07:19:41Z")

</div>

I use counters for the automation of the lights.  
So, if sensor sees movement switch on lights and start counter for x minutes  
If counter is empty turn of lights.  
This way if there is more movement the counter will be reset to x minutes and the lights stay on.

If i switch the switch:  
Turn of the flow for the lights and stop the counter.

When I turn of the lights by the switch (or when we go to sleep or we go out)  
Turn on the flow for the lights and start the timer for a few seconds.

---

<div class="post-metadata">

**Author:** ![apps](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/apps/32/30000_2.png) [@apps](https://community.homey.app/u/apps)\
**Post date:** [March 31, 2021, 8:08am UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/3 "2021-03-31T08:08:16Z")

</div>

![SmartSelect_20210331-110546_MX Player Pro](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/2/1/216163b549fab7465d4a9cd95c7b8811be8d43e5.gif)

AK auto - auto (in terms of time and motion, intensity…)  
AK mode 100, 50 25 sleep, sylence media - manual mode (turn on manual mode or tv\*, auto off…)  
AK mode z off - turn all modes off, after x sec. turn on mode auto (or if the zone is inactive for x min. turn off all, turn on auto)

---

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [April 1, 2021, 10:21pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/4 "2021-04-01T22:21:13Z")

</div>

What is this? An app?

---

<div class="post-metadata">

**Author:** ![apps](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/apps/32/30000_2.png) [@apps](https://community.homey.app/u/apps)\
**Post date:** [April 2, 2021, 6:45am UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/5 "2021-04-02T06:45:49Z")

</div>

> **[Virtual Devices](https://homey.app/en-us/app/com.arjankranenburg.virtual/Virtual-Devices/)**
>
> Virtual Devices to simulate 1 or more real devices

---

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [April 2, 2021, 3:30pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/6 "2021-04-02T15:30:54Z")

</div>

Want to explain some more what this is?

---

<div class="post-metadata">

**Author:** ![apps](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/apps/32/30000_2.png) [@apps](https://community.homey.app/u/apps)\
**Post date:** [April 2, 2021, 5:51pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/7 "2021-04-02T17:51:03Z")

</div>

> [@Marius\_Kohmann](#):
>
> … in the living room or watch a movie.

example  
[https://homey.app/f/VmhIjy](https://homey.app/f/VmhIjy)  
[https://homey.app/f/RlBIjy](https://homey.app/f/RlBIjy)

> [@Marius\_Kohmann](#):
>
> system to stop automation

[https://homey.app/f/NP\_Njy](https://homey.app/f/NP_Njy)  
[https://homey.app/f/cMMKjy](https://homey.app/f/cMMKjy)

did you understand?

---

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [April 2, 2021, 10:28pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/8 "2021-04-02T22:28:58Z")

</div>

I understand, but it unfortunately it does not answer my question in how one could determine if a user or flow caused a light to be switched on/off.

I am not looking for how to implement different modes or how to use virtual devices.

What I have come up with so far is creating two flows that will listen for state changes on the light (on/off) and then set a variable (wasAutomated = false).

Then for every flow that changes the state of that light(other light automations based on presence) set wasAutomated = true. Since these flows also will trigger the flow mentioned above, If I then can make sure that the flow that was triggered by automation is ran _after_ the flow that will react to both user and automation events, I can know that the wasAutomated variable will hold the value that I am looking for.

However, since this is kind of hairy logic, I don’t know if I even managed to explain it nicely, I was looking for a better way to do it.

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 2, 2021, 11:12pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/9 "2021-04-02T23:12:56Z")

</div>

Oh my… trying to get it. Is a flow chart drawing (by hand if you like) an idea, to clarify?

---

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [April 3, 2021, 10:26pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/10 "2021-04-03T22:26:58Z")

</div>

I am going to see if I can implement it, and will report back how it goes. Also found this [The order of cards executed in flows](https://community.homey.app/t/the-order-of-cards-executed-in-flows/31877) which essentially tries to solve the same thing.

---

<div class="post-metadata">

**Author:** ![Marius\_Kohmann](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/marius_kohmann/32/32319_2.png) [@Marius\_Kohmann](https://community.homey.app/u/Marius_Kohmann)\
**Post date:** [April 6, 2021, 2:57pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/11 "2021-04-06T14:57:03Z")

</div>

I managed to implement it - now I am able to determine if there was a user or a flow that turned on/off a device, and then, if the system was manually overridden or not. It gets quite hairy and ugly, so I am very open for improvements. Here goes:

The overall idea is the following: We start a timer whenever the device was turned on/off. This flow will run regardless of the system was turning on/off the device, or if the device was turned on/off manually by a user. All the automated flows needs to stop this timer to mark that the system turned on/off the device. If the timer finishes we know that a user turned on/off the device. When we know that a user turned on/off the device we can start another timer that determines for how long we want to keep the device manually overridden, before the device goes back to automated state.

Flows that run regardless of user/system turning on/off device:

Automated flows that will try to stop the timer:

When timer runs out, we know that a user turned on/off the device. We can therefore safely disable the flows that we don’t want to run when the device is overridden, and start the new timer that determines for how long the system is overridden.

When our timer runs out we can enable the automated flows again:

I very much welcome improvements! 6 flows and 2 timers is way too much for implementing this functionality for one single device. Wish me good luck implementing this for all my lights 😆

---

<div class="post-metadata">

**Author:** ![Peter\_Kawa](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/peter_kawa/32/173848_2.png) [@Peter\_Kawa](https://community.homey.app/u/Peter_Kawa)\
**Post date:** [April 6, 2021, 3:18pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/12 "2021-04-06T15:18:21Z")

</div>

I’m completely lost here, sorry 😉

---

<div class="post-metadata">

**Author:** ![Crimly](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/crimly/32/14213_2.png) [@Crimly](https://community.homey.app/u/Crimly)\
**Post date:** [April 9, 2021, 7:47pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/13 "2021-04-09T19:47:28Z")

</div>

Why not create a flow that disables the automation flow and enables it after x amount of time? Or toggle it manually when starting/finishing.  
I am using one simple flow to disable automatic room ventilation for one hour when i use my sauna.

---

<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:** [April 9, 2021, 8:43pm UTC](https://community.homey.app/t/determine-if-device-was-turned-on-off-by-user-or-system/45519/14 "2021-04-09T20:43:44Z")

</div>

I think there is an cleaner approach by reversing the logic. You know when you automatically start something in advance, al the other activity is manual.

First create the flow you use for automated light control. You can call this flow in any situation you want to have automation. So instead of turning the light on or off directly from Homey, call the flow: “Automated turn on” or “Automated turn off”.

Automated on: If this flow was started  
And: Timer “Disable auto on” is not running  
Then: Start timer “Automated control” for 2 seconds, and turn on light

Automated off: If this flow was started  
And “Disable auto off” is not running  
Then start timer “Automated control” for 2 seconds, and turn off light

Then in the flow that listens for the light:

If the light is turned on  
And timer “Automated control” is not running  
Then: Start timer “Disable auto off” for x time and stop timer “Disable auto on”

If the light is turned off  
And the timer “ Automated control” is not running  
Then: Start timer “ Disable auto on” for x time and stop timer “Disable auto off”

There are also variations to this approach you can use by setting variables in the flows that listen for the lights, so you can see if the last action was manual or automatic.

Notice I reversed the timer checks for on and off: manual on overrides auto off, manual off overrides auto on.
