Help needed to change code

Can someone help me to change this code so there is a time range to humidifier start and stop time.


  • Smart Humidifier/Dehumidifier
  • Copyright 2015 Sheikh Dawood
  • 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.


// Source maintained at

name: “Smart Humidifier/Dehumidifier”,
namespace: “Sheikhsphere”,
author: “Sheikh Dawood”,
description: “Turn on/off a humidifier or dehumidifier based on relative humidity from a sensor.”,
category: “Green Living”,
iconUrl: “”,
iconX2Url: “

preferences {
section(“Smart Humidifier/Dehumidifier”) {
paragraph “Version 1.0.1”
section(“Devices to sense and control humidity:”) {
input “humiditySensor1”, “capability.relativeHumidityMeasurement”, title: “Humidity Sensor”, required: true
input “switch1”, “capability.switch”, title: “Switch”, required: true
section(“Settings:”) {
paragraph “Turn off humidifier when humidity is above the High threshold value and turn it on when it is below the Low threshold value. This works the opposite way when selecting the Dehumidifier mode.”
input(name: “humidityMode”, type: “enum”, title: “Mode”, options: [“Humidifier”,“Dehumidifier”], required: true, defaultValue: “Humidifier”)
input “humidityHigh”, “number”, title: “High humidity Threshold value (RH%):”, required: true
input “humidityLow”, “number”, title: “Low humidity Threshold value (RH%):”, required: true
input “delay”, “number”, title: “Polling delay (minutes):”, required: true, defaultValue: 30
section( “Notifications” ) {
input “sendPushMessage”, “enum”, title: “Send a push notification?”, metadata:[values:[“Yes”,“No”]], required:false
input “phone1”, “phone”, title: “Send a Text Message?”, required: false

def installed() {

def updated() {
try {
} catch (all) {
log.warn (“unschedule error”)

def initialize() {
state.lastSwitchStatus = null
state.lastHumidity = null
subscribe(humiditySensor1, “humidity”, humidityHandler)
subscribe(switch1, “switch”, switchHandler)

def scheduleChecks() {
def sec = Math.round(Math.floor(Math.random() * 60))
def exp1 = “sec */{delay} * * * ?”
log.debug “pollAndCheck: $exp1”
schedule(exp1, pollAndCheck)

def pollAndCheck() {
state.lastSwitchStatus = switch1.latestValue(“switch”)
state.lastHumidity = humiditySensor1.currentValue(“humidity”)
log.debug “pollAndCheck: state.lastHumidity: $state.lastHumidity, state.lastSwitchStatus: $state.lastSwitchStatus”
runIn(1, statusCheck)

def tryPoll() {
try {
} catch (all) {
log.warn ("${switch1.label} does not support polling")

def statusCheck() {
if (state.lastHumidity) {
if (state.lastHumidity >= humidityHigh) {
if (state.lastSwitchStatus != “off” && humidityMode == “Humidifier”) {
send(“It’s too humid! Turning off {switch1.label}") switch1?.off() state.lastSwitchStatus = "off" } else if (state.lastSwitchStatus != "on" && humidityMode == "Dehumidifier") { send("It's too humid! Turning on {switch1.label}”)
state.lastSwitchStatus = “on”
else if (state.lastHumidity <= humidityLow) {
if (state.lastSwitchStatus != “on” && humidityMode == “Humidifier”) {
send(“It’s too dry! Turning on {switch1.label}") switch1?.on() state.lastSwitchStatus = "on" } else if (state.lastSwitchStatus != "off" && humidityMode == "Dehumidifier") { send("It's too dry! Turning off {switch1.label}”)
state.lastSwitchStatus = “off”

def switchHandler(evt) {
log.trace “$ $evt.value: $evt.displayName”
state.lastSwitchStatus = evt.value

def humidityHandler(evt) {
log.trace “$ state.lastHumidity: $state.lastHumidity, humidity: $evt.value, humidityHigh: $humidityHigh, humidityLow: $humidityLow. humidityMode: $humidityMode”
state.lastHumidity = Double.parseDouble(evt.value.replace("%", “”))
runIn(1, statusCheck)

private send(msg) {
if ( sendPushMessage == “Yes” ) {
log.debug( “Sending push message” )
sendPush( msg )

if ( phone1 ) {
    log.debug( "Sending text message" )
    sendSms( phone1, msg )

log.debug msg


1 Like

You could probably do this with two automations using the automation creator in the new app.

i did this wit automation needed 3 ones but somereason it did not activate every time it should…

Hi @Tetzu! Have you checked the new Rules API? It might serve the purpose of what you are trying to achieve, you can use multiple conditions to decide if a device should be turned on or off.

© 2019 SmartThings, Inc. All Rights Reserved. Terms of Use | Privacy Policy

SmartThings; SmartApps®; Physical Graph; Hello, Home; and Hello, Smart Home are all trademarks of the SmartThings, Inc.