Hi,
I wrote an app to turn on lights for a period of time when a virtual switch (switch capability device) is turned on. I can see it working fine in the simulator. (I actually have an remote camera watching one of the real lights to verify that it’s working.) I can also see the virtual switch turned on and off in the device log when I turn it on or off from the mobile app (or the simulator for that matter). I’ve published the app for myself and can configure it on the mobile device just like I can in the simulator. The thing that’s not working is nothing happens it I turn the virtual switch on from the mobile device. I ultimately want to have the motion sensor on the Arlo camera via IFTTT turn on the virtual switch. That part is working too because I can see corresponding log times in the Arlo motion sensing and the device log. Any ideas on what I might be doing wrong? I used the smart night light as my starting template for the app and changed the trigger from the motion sensor to the virtual switch. Thanks.
Tim
Here’s the code. The sunrise/sunset part does nothing at this point.
/**
- Copyright 2015 SmartThings
- 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:
-
http://www.apache.org/licenses/LICENSE-2.0
- 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.
- Let There Be Light!
- Turn your lights on when an open/close sensor opens and off when the sensor closes.
- Author: Tim Hemmelman
*/
definition(
name: “IFTTT virtual switch trigger”,
namespace: “smartthings”,
author: “SmartThings”,
description: “Turn on real lights when virtual switch is turned on by IFTTT motion trigger”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png”
)
preferences
{
section(“When virtual switch turns on…”)
{
input “virtual_switch”, “capability.switch”
}
section(“Turn on lights…”)
{
input “switches”, “capability.switch”, multiple: true
}
section(“Turn off light after so many seconds…”)
{
input “delay”, “number”, title: “Seconds?”
}
section ("Sunrise offset (optional)...")
{
input "sunriseOffsetValue", "text", title: "HH:MM", required: false
input "sunriseOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
}
section ("Sunset offset (optional)...")
{
input "sunsetOffsetValue", "text", title: "HH:MM", required: false
input "sunsetOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
}
}
def installed()
{
initialize()
}
def updated()
{
unsubscribe()
initialize()
}
def initialize()
{
state.firstTime = true
subscribe(virtual_switch, “switch”, virtualSwitchHandler)
subscribe(location, “position”, locationPositionChange)
subscribe(location, “sunriseTime”, sunriseSunsetTimeHandler)
subscribe(location, “sunsetTime”, sunriseSunsetTimeHandler)
astroCheck()
}
def locationPositionChange(evt)
{
log.trace "locationChange()"
astroCheck()
}
def sunriseSunsetTimeHandler(evt)
{
state.lastSunriseSunsetEvent = now()
log.debug "SunriseSunsetTimeHandler($app.id)"
astroCheck()
}
def turnOffLightAfterDelay()
{
log.trace "In turnOffLightAfterDelay, state.lightOffTime = $state.lightOffTime, state.lastStatus = $state.lastStatus"
if (state.lightOffTime && state.lastStatus != “off”)
{
def elapsed = now() - state.lightOffTime
log.trace "elapsed = $elapsed"
if (elapsed >= ((delay ?: 0)1000) - 2000)
//if (elapsed >= ((delayMinutes ?: 0) * 60000L) - 2000)
{
state.lastStatus = "off"
log.debug “Turning off lights”
//switches.off()
TurnLightsOff()
}
else
runIn(delay, turnOffLightAfterDelay, [overwrite: false])
//runIn(delayMinutes60, turnOffLightAfterDelay, [overwrite: false])
}
}
def astroCheck()
{
def s = getSunriseAndSunset(zipCode: location.zipCode, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
state.riseTime = s.sunrise.time
state.setTime = s.sunset.time
log.debug “${location.zipCode} – rise: ${new Date(state.riseTime)}($state.riseTime), set: ${new Date(state.setTime)}($state.setTime)”
}
def virtualSwitchHandler(evt)
{
//log.trace “In virtualSwitchHandler, state.firstTime = $state.firstTime”
if (true == state.firstTime)
{
log.debug "NUMBER OF SWITCHES is ${switches.size()}"
for (int i=0; switches.size() > i; i++)
{
state."bAlreadyOn$i" = false;
if ("on" == switches[i].currentSwitch)
state."bAlreadyOn$i" = true;
log.debug "##################"
log.debug "SWITCH is ${switches[i]}"
log.debug "## state.bAlreadyOn$i ## = #### ${state."bAlreadyOn$i"} ####"
log.debug "##################"
}
state.firstTime = false
log.debug "$evt.name : $evt.value"
if (evt.value == "on")
{
switches.on()
state.lastStatus = "on"
state.lightOffTime = now()
turnOffLightAfterDelay()
virtual_switch.off()
}
else if (evt.value == "off")
{
switches.off()
}
}
else
{
state.firstTime = true
}
}
def TurnLightsOff()
{
for (int i=0; switches.size() > i; i++)
{
if (false == state.“bAlreadyOn$i”)
switches[i].off()
}
}