# Switching based on runtime for day or week

**URL:** https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970
**Category:** General
**Created:** [8 September 2018 20:08 UTC](https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970 "2018-09-08T20:08:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![whatsupskip](https://avatars.discourse-cdn.com/v4/letter/w/5f9b8f/32.png) [@whatsupskip](https://discourse.nodered.org/u/whatsupskip)
#### Post date: [8 September 2018 20:08 UTC](https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970/1 "2018-09-08T20:08:07Z")

</div>

What is the easiest/best way to switch a device on/off based on how long a device has been on (running) for during the current day (since 00 hours) or the last 7 days?

I have been the time at which the device is turned on and off in SQLite. I have had great difficulties determining the required queries to find the required information. I figure this must be something that many people want to do, so hopefully there is a node or example I can work from.

Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [9 September 2018 07:11 UTC](https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970/2 "2018-09-09T07:11:16Z")

</div>

A bit more detail needed ?? Is the on/off sacrosanct and must survive a restart of Node-red ?

Will the two conditions interact ? i.e. it has run longer during the week than the set point , but not longer than the daily setpoint ?

Essentially the easiest way is to create a countdown timer - and monitor that for when it expires - have a look at Ncherrys mytimeout node - should be able to do what you want

Feed the input with whatever it is that triggers the start of the runtime - feed the output of both into a node to act on the off condition - be it MQTT message sending etc

Craig

---

<div class="post-metadata">

### Author: ![whatsupskip](https://avatars.discourse-cdn.com/v4/letter/w/5f9b8f/32.png) [@whatsupskip](https://discourse.nodered.org/u/whatsupskip)
#### Post date: [19 September 2018 00:23 UTC](https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970/3 "2018-09-19T00:23:16Z")

</div>

Thanks for this. It looks like it might be the way to go.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [19 September 2018 02:15 UTC](https://discourse.nodered.org/t/switching-based-on-runtime-for-day-or-week/2970/4 "2018-09-19T02:15:05Z")

</div>

Excellent thanks for letting us know

Maybe mark it as solved when you confirm it works

Craig
