App stops every day or two

I have created an app (one of my first) to control a whole house fan. The basic idea is that every hour the fan should come one for 10 minutes. The app works but after a day or two it stops working. I am just using the schedule command. Should I be using cron instead of schedule?

 *  Fan On Schedule
 *  Copyright 2016 Christopher Stewart
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.

	name: "Fan On Schedule",
	namespace: "cestewart",
	author: "Christopher Stewart",
	description: "Turn a fan on every X minutes and run for Y minutes.",
	category: "Convenience",
	iconUrl: "",
	iconX2Url: "",
	iconX3Url: "")

preferences {
	section("Switch to control") {
		input "theswitch", "capability.switch", title: "Fan", required: true
	section("Settings") {
		input name:"runEveryMinutes", type: "number", title: "How often shoud the fan run in minutes", required: true
		input name:"runForMinutes", type: "number", title: "How long should the fan run in minutes", required: true

def installed() {
	log.debug "Installed with settings: ${settings}"

def updated() {
	log.debug "Updated with settings: ${settings}"

def initialize() {
	subscribe(theswitch, "switch", switchHandler)

def switchHandler(evt) {
	if (theswitch.currentValue("switch").equalsIgnoreCase("on")) {
		log.debug "The fan will be turned off in ${runForMinutes} minutes."
		runIn(60*runForMinutes, turnSwitchOff)

def turnSwitchOn() {
	log.debug "The fan has been turned on"
	schedule(now() + runEveryMinutes * 60 * 1000, turnSwitchOn)

def turnSwitchOff() {
	log.debug "The fan has been turned off"

A method that repeatedly schedules itself will stop working after a while.

You’re better off using something like runEvery1Hour().

1 Like