We have moved at community.getvera.com

Author Topic: Turning a SmartSwitch on/off during set times of the day  (Read 4075 times)

Offline SunnysGlimpse

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Turning a SmartSwitch on/off during set times of the day
« on: July 07, 2011, 02:52:23 pm »
I'm trying to create a scene that turns a SmartSwitch off from 2am to 5pm and turns it on during the other time of the day. To do this using the Vera2 UI, I'm assuming I would need two scenes to accomplish this right? 1 to turn it off at 2:00am and 1 to turn it on at 5:01pm?

Is there an easier way to do this via 1 scene and Luup code, the UI, or is the best way?

Thanks.

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #1 on: July 07, 2011, 03:08:00 pm »
@SunnyGlimpse

That should be possible using this wiki page:
http://wiki.micasaverde.com/index.php/Luup_Scenes_Events


And this:
http://wiki.micasaverde.com/index.php/Scripts_for_scenes#Scene_that_runs_only_in_a_user_set_time_interval



I'm trying to create a scene that turns a SmartSwitch off from 2am to 5pm and turns it on during the other time of the day. To do this using the Vera2 UI, I'm assuming I would need two scenes to accomplish this right? 1 to turn it off at 2:00am and 1 to turn it on at 5:01pm?

Is there an easier way to do this via 1 scene and Luup code, the UI, or is the best way?

Thanks.
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |

Offline SunnysGlimpse

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #2 on: July 07, 2011, 03:27:20 pm »
Thanks Henk. I've actually read both of those and currently use that Luup code for motion detection scenes I have.

I'm just not sure how to make it work for this type of scene. For instance, I understand in my other scene that when motion gets detected (an event), that Luup code is ran and checks against the time to see whether or not to run the scene.

But to apply that code to this case, what would the event be? It really just needs to trigger on at this time and off at this time. I just have no idea how to say turn SmartSwitch off at 2am and on at 5:01pm and know that code is constantly running so it doesn't miss the 2am and 5:01pm minutes.

This is the device_type under the settings of the SmartSwitch: urn:schemas-upnp-org:device:BinaryLight:1

I never understand why this says "device" and in Luup code, items are "serviceId". What's the difference between these two?

This is device #11.

Now I dug this code up to turn another switch on:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },5)

So I assume I would do something like this to turn it on:

luup.call_action("urn:upnp-org:serviceId:BinaryLight1","WhatParam?",{ WhatHere?="1" },11)

How do I find the parameter for the SmartSwitch to turn it off and on?

Then how do I know my Luup code will run all the time so that when 2am and 5:01pm? Create a timer and say run every minute and check whether the device is already on or off?

Thanks for all the help.

Offline SunnysGlimpse

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #3 on: July 07, 2011, 03:38:03 pm »
Okay, answering some of my own questions here. I just found out where I can read the BinaryLight1.xml file under MiOS Developers -> Luup Files and saw that it appears to be a SwitchPower1 and I can turn it on and off using:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },11)
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },11)

Now I just need to know the best way to have the code run all the time or at least when it is 2am and 5:01pm to run these actions.

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #4 on: July 07, 2011, 03:57:38 pm »
Okay, answering some of my own questions here.[...]
Yup! Also, take a look at this topic from earlier today.

Quote
Now I just need to know the best way to have the code run all the time or at least when it is 2am and 5:01pm to run these actions.
Perhaps you could add 2 timers, one for each time. You'd have to keep the desired time in sync with the Luup.

Not sure if you've accomplished your goal of 'easier way' though. :)
Dezwaved at the moment...

Offline MNB

  • Hero Member
  • *****
  • Posts: 689
  • Karma: +4/-9
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #5 on: July 07, 2011, 04:37:50 pm »
@sunnysGlimpse, hopefully you've solved your problem? Did you end up creating two scenes or something else? If you would be so kind and share your solution with those of us that are learning LUA through the school of hard knocks... ;D Mike
Edge-1.7.2607, VRI06-1LX Dimr (6), VS05-1LX Sw (4), GE ZW3002 Dimr (1), HA01C Outlet (12), HA02C Sw (6), HA04C Sw (4), GE Sw (2), HA05 Sw (3), MiniMote Contr (5), HSM200/100 Sensor (2), ZW100A Sensor (8), Nest thermostat (1) & Protect Fire/CO (6) Kwikset Locks (3), F18918W (1) & IP3M-9 (6) Cam's

Offline SunnysGlimpse

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #6 on: July 07, 2011, 05:25:43 pm »
Yup! Also, take a look at this topic from earlier today.

Awesome thread. Thanks for sharing. What's weird is I don't get anything when I hover over the name of a device. Maybe I need to restart Firefox...

Quote
Perhaps you could add 2 timers, one for each time. You'd have to keep the desired time in sync with the Luup.

That's what I've done. Let me know if this all sounds correct.

1. Create a new scene
2. Create a timer for M-F to run at 2am (calling it the off time)
3. Create a timer for M-F to run at 5pm (calling it the on time)

I then created the following Luup code for the scene:

Code: [Select]
local DEVICE_NO = 11 -- the SmartSwitch device number
local SID = "urn:upnp-org:serviceId:SwitchPower1" -- the SmartSwitch service ID
local offTime = "02:00"
local onTime = "17:00"
local window = 5 -- window, in minutes, around the on/off time to ensure the switch is triggered
local currentTime = os.date("*t")
currentTime = currentTime.hour * 100 + currentTime.min

--Get the onTime in the form of 1700
local hour = tonumber( onTime:sub( onTime:find("%d+") ) )
local minute = tonumber( onTime:sub(-2) )
if hour and minute then
    onTime = hour * 100 + minute
else
    luup.log("ERROR: invalid on time")
    return false
end

--Get the offTime in the form of 200
hour = tonumber( offTime:sub( offTime:find("%d+") ) )
minute = tonumber(offTime:sub(-2))
if hour and minute then
    offTime = hour * 100 + minute
else
    luup.log("ERROR: invalid end time")
    return false
end

--Does the current time fall in the onTime window
if currentTime <= (onTime + window) and currentTime >= (onTime - window) then
    local currentStatus = luup.variable_get (SID, "Status", DEVICE_NO)

    if currentStatus == "0" then
         luup.call_action(SID,"SetTarget",{ newTargetValue="1" },DEVICE_NO)
    end
end

--Does the current time fall in the offTime window
if currentTime <= (offTime + window) and currentTime >= (offTime - window) then
    local currentStatus = luup.variable_get (SID, "Status", DEVICE_NO)

    if currentStatus == "1" then
         luup.call_action(SID,"SetTarget",{ newTargetValue="0" },DEVICE_NO)
    end
end

So when either timer gets fired, it will run that Luup code, see if it falls in either of the timeframes, and turns the power switch on or off if it isn't already.

The reason I'm creating this is so our entertainment systems (with tons of vampire sucking devices like cable boxes) are off during the windows when we wouldn't use them.

Pending the above is all sound, I will now need to create a new scene or find a way to disable the scene above in the rare case that we are home and don't want power turned off/on. Has anyone done this?

Thanks.
« Last Edit: July 07, 2011, 06:05:15 pm by SunnysGlimpse »

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #7 on: July 07, 2011, 05:37:30 pm »
Answering your last question, you could use the holidayswitch code or virtual State device (Same plugin) to accomplish that.

The threads are easy to find.
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |

Offline SunnysGlimpse

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Turning a SmartSwitch on/off during set times of the day
« Reply #8 on: July 07, 2011, 06:39:35 pm »
Answering your last question, you could use the holidayswitch code or virtual State device (Same plugin) to accomplish that.

The threads are easy to find.

Absolutely awesome. Thanks Henk.

Here's the updated code with the virtual device added. I call it the "Override Switch" that when turned on, prevents the Scene from turning on or off the power:

Code: [Select]
local OVERRIDE_DEVICE_NO = 28 -- the virtual switch
local OVERRIDE_SID = "urn:upnp-org:serviceId:SwitchPower1" -- the SmartSwitch service ID
local override = luup.variable_get(OVERRIDE_SID, "Status", OVERRIDE_DEVICE_NO) or "0"
local DEVICE_NO = 11 -- the SmartSwitch device number
local SID = "urn:upnp-org:serviceId:SwitchPower1" -- the SmartSwitch service ID
local offTime = "02:00"
local onTime = "17:00"
local window = 5 -- window, in minutes, around the on/off time to ensure the switch is triggered
local currentTime = os.date("*t")

--If our override virtual switch is 1, we don't want to run the code below
if (override == "0") then
currentTime = currentTime.hour * 100 + currentTime.min

--Get the onTime in the form of 1700
local hour = tonumber( onTime:sub( onTime:find("%d+") ) )
local minute = tonumber( onTime:sub(-2) )
if hour and minute then
onTime = hour * 100 + minute
else
luup.log("ERROR: invalid on time")
return false
end

--Get the offTime in the form of 200
hour = tonumber( offTime:sub( offTime:find("%d+") ) )
minute = tonumber(offTime:sub(-2))
if hour and minute then
offTime = hour * 100 + minute
else
luup.log("ERROR: invalid end time")
return false
end

--Does the current time fall in the onTime window
if currentTime <= (onTime + window) and currentTime >= (onTime - window) then
local currentStatus = luup.variable_get (SID, "Status", DEVICE_NO)

if currentStatus == "0" then
luup.call_action(SID,"SetTarget",{ newTargetValue="1" },DEVICE_NO)
end
end

--Does the current time fall in the offTime window
if currentTime <= (offTime + window) and currentTime >= (offTime - window) then
local currentStatus = luup.variable_get (SID, "Status", DEVICE_NO)

if currentStatus == "1" then
luup.call_action(SID,"SetTarget",{ newTargetValue="0" },DEVICE_NO)
end
end
end