SmartThings Community

Delayed actions... need help


(Chrisb) #1

Okay, my knowledge of groovy is obviously lacking here. wrestled with this for a while can just can’t figure this out. I’m trying to write an app that will let me use my daughter’s lights in her room as a nightlight. I want to input a percentage and then, over time, dim the lights from that level to off.

I’ve thought about doing this a number of different ways. My first thought was to do it sorta brute force and have the app for the user to pick from a list, but I’m not sure how to call for enum input. Then using if-then statements I’d just decrease using the runIn command. But, like I said, not sure how to invoke enum.

Next thought was to run using a ‘while’ loop that calls a subroutine(delay: xxxx), but you can’t call a subroutine with a delay apparently. And even when I didn’t put a delay in it didn’t seem like the loop was working because the subroutine that was decreasing my percentage number and then dimming the lights wasn’t running.

Any suggestions on how to do this?


(Solardave1) #2

Use a candle.


(Chrisb) #3

I tried searching thru groovy documentation for the candle function, but I can’t find anything on it.


(Solardave1) #4

Its likely an un documented function. Try asking support@smarthings.com


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #5

@chrisb:

I have several ideas; but I also would like to be sure I understand your use-case.

Because I am following several concurrent Forum threads (many with drifting tails…), I’d prefer if you send me a Private Message on the system here if you want to delve into this together.

Msg: @tgauchat

Thanks!
…CP.


(C Chen) #6

I don’t quite understand the enum part, but for the dimming part, something like below should work. Not ‘compiled’ or tested though.

def initialize() {
   state.currentLevel = initialLevel; // http://support.smartthings.com/entries/21601924-SmartApp-Application-State
   schedule( startTime, dimEvent )
}

def dimEvent() {
   unschedule( dimEvent ) // I think a handler can only show up in the pending job list once. 
                          // So it has to be unscheduled for the runIn below to work. 
   dimIt( state.currentLevel )
   state.currentLevel -= 5 // lower the level for next time
   runIn( delaySeconds, dimEvent )
}

(Jay) #7

just remember to stop dimming when currentLevel reaches 0


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #8

@stepchen’s post definitely is a good method to try as a starting point.

If that runs into problems, there may be some bizarre alternatives, but no need to explore those yet.


(Chrisb) #9

Thanks for the helps everyone… I appreciate it.

@C.Chen … if I’m understanding what you’re code does:

Initialize section:
1.) Makes state.currentLevel equal to the initialLevel, which is presumably the number the user entered when installing the app.
2.) Schedules the process to start at the given time. I think I’m probably just going to have this as a “app tap” rather than scheduled as bedtime isn’t always exactly the same. I’m trying to figure out a way to trigger it manually a different way so I don’t have to dig out my phone, but I don’t know of a way right now. My Evolve switches here don’t seem to like double tap.

DimEvent section:
1.) unschedules dimEvent so it can be rescheduled later.
2.) run the dimIt section which presumably is a separate sections that sets the dim level. Do I really need to call a new section here though? Couldn’t I just run the command switch.setLevel( state.currentLevel ) here?
3.) Decreases setLevel by 5 for next run. Can you explain the syntax here? I’ve seen == used in some programs. I’ve seen – or ++ in other groovy documentation, and now you’ve got -= here. How does this work?
4.) Setup to run the DimEvent against x number of seconds later.

Does that sound right?


(Chrisb) #10

@Jay wrote:

just remember to stop dimming when currentLevel reaches 0

Good point. I think I’ll just us a simple IF - THEN here.

Instead of the runIn at the end of DimEvent, I’ll do something like this:

IF state.currentLevel > 5 THEN do the normal runIn. ELSE, call offEvent.

Then my offEvent will be something like this:

switches.off
runIn (120, resetEvent)

My resetEvent will be something like this:

switches.setLevel(99)
switches.off

What this should do then is as long as the currentLevel is above 5%, it’s just going to repeat the events dimEvent (dim the lights some more, decrease currentLevel for the next run). But if currentLevel is at or below 5, then we turn the lights off and schedule an event for 2 hours later. That event 2 hours later turns the lights on full brightness, then turns them off right away.

If I didn’t do this reset event then the light level would still be at 5% or less. So in the morning when we turn on the lights they would barely come on. The reset event turns them back on full early in the morning when she’s hopefully sleeping very soundly and won’t be bothered by a momentary flash. This way when we flip the switch in the morning they come on full.


(C Chen) #11
Does that sound right?

Yes

My Evolve switches here don’t seem to like double tap.

Why does it have to be a double tap? Can’t a single tap (or any other kind of events) do? (I don’t own an Evolve switch)

You can even subscribe to an event from a switch (one that you turn on/off when you go to bed) in your own bedroom.

Do I really need to call a new section here though? Couldn’t I just run the command switch.setLevel( state.currentLevel ) here?

You could. It is just that I don’t have a dimmer switch so I don’t know what command to use. I figured that you would know :slight_smile:

state.currentLevel -= 5

-= is a compound assignment operator. x -= y is similar to x = x - y, except that x is only evaluated once.


(Chrisb) #12

@C.Chen wrote:

-= is a compound assignment operator. x -= y is similar to x = x - y, except that x is only evaluated once.

Thanks again for the help. I figured the syntax was something like that, but wasn’t sure. I’ve gotta do more reading up on groovy. It probably helps a lot if you know Java, which I don’t. :frowning:

You could. It is just that I don’t have a dimmer switch so I don’t know what command to use. I figured that you would know :)

Ah, okay… yeah, the command is switch.setLevel(##) where the ## = the level number between 0 and 99%

Why does it have to be a double tap? Can’t a single tap (or any other kind of events) do? (I don’t own an Evolve switch)

You can even subscribe to an event from a switch (one that you turn on/off when you go to bed) in your own bedroom.

The problem is that I don’t want to interfere with the normal operation of the switch. I have one dimmer switch in her room that controls the over head lights. I want these to work as normal… turn on the switch, lights go on. Turn off the switch, lights go off. I just want to have this extra function as a slowly dimming night light for when she goes to bed.

I do have some GE switches that I’m running a specific Double-Yap function on. If I double tap Switch-A, it runs program B. But when I tried to run that on the Evolve switches, it didn’t work. It might be that Evolve doesn’t respond in the same way, or it might be that these switches (which are upstairs, further away from the Hub then the GE ones) are simply out of range for this. ST reported at one point that GE and related switches (ie, ones that do NOT license the Lutron patent) can only really double tap when in direct communication with the Hub (not communicating thru the mesh).

I might be able to do something like turn off her light, then turn on my light, then turn off my light. If I figure out a way to capture that series of events I might be able to say that that triggers the app. But that might be as complicated as pulling out my phone. :slight_smile:


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #13

A little off-topic but since we are talking about SmartApp triggers…

Once we get LAN IP connectivity to the hub, it should be possible to create some simple switches / button devices as activators / triggers for SmartApps.

A mini-co-hub could be created (even Arduino + SmartShield…?) that uses light-weight (cheap) serial radios to talk to other nearby switches. The range won’t be as good as ZigBee/Z-Wave (or even WiFi) of course; but some experimenting is worthwhile.

The point is that some inexpensive Things would come in really handy for the SmartThings ecosystem; starting with SmartApp triggers. Since the SmartHub did not include Bluetooth in the current model, it becomes an interesting question as to what choice of RF connectivity to use – cheap, but must have a “usable” range to any co-hubs.

This opens the ecosystem to a world of activation methods, including this nifty “whistle-based” PCB (~$30): https://www.tindie.com/products/limpkin/the-whistled-assembled-pcb/

Persons wishing to discuss further are welcome to private message me (at)tgauchat, and/or start a new thread or Project.

…CP.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #14

Further to message #4495

I don’t have any experience in this regard (yet)… but ~$20 + power + a “button” gives a whole Arduino compatible tiny board WITH apparently quite long-range bi-directional RF communication.

In fact, this is probably overkill (since we don’t necessary have to transmit / receive complex messages from many basic remote trigger switches or sensors).

http://lowpowerlab.com/moteino/

…CP.


(Solardave1) #15

Just FyI, the. Xbee pro models with the wire antenna absolutely blew me away with the range issue. They make one with an external antenna connector and I’m sure I could hit the moon with a yagi. They come in all sorts of flavors in terms of rf output.


(Solardave1) #16

I just found a 12vdc RF switch with 2 key fobs that I must have ordered from china off eBay a while ago. They say 433MhZ. I don’t remember why I bought them but they will find their way into an ST project. Remote trip a pin on an Arduino with a smart shield? I think it was like $20 bucks and 3 weeks shipping. I found 2 sets. Couple of what look like variable something’s on the board so the we’ll see what the freq counter says. This may have possibilities.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #17

Hey Mr. Hacker @solardave1

Have you speculated as to what the extra header-holes on the SmartMulti PCB might be usable for?

(Don’t say a pin cushion or colander …)…

…CP.


(Andrew Urman) #18

@tgauchat It’s how our programming jigs write the firmware to the device when first being manufactured and tested.


(Chrisb) #19

Thanks again to everyone for your help. I wrote the program over this weekend and have it running. If you didn’t see it in the other thread I posted, I did share the app (called Nightlight) and I documented the heck out of it. A comment on virtually every line. Because I’ve gotten so much help from others here I wanted to help out others so I tried to explain every step of the way in this program so others could use it as a sort of training manual if they, like me, just learning groovy and the SmartThings development environment.

Unfortunately the stupid Lutron patent is biting me in the butt again. What I ended up doing was taking about a Intermatic screw in module (http://www.amazon.com/Intermatic-HA05C-Settings-Screw-Module/dp/B000J17QWU/ref=sr_1_3?ie=UTF8&qid=1375710186&sr=8-3&keywords=intermatic+zwave) and wired it to an old cord I have. I put the guts into an electrical box and hacked up the button to be pressed thru a old light-switch plate. What I essentially created is a “portable” plug-in switch. A little bit of 3M command strips to mount it on my daughter’s bed post and she’s got a way to turn on the light any time she wants.

But… here’s the problem. Because of the stupid Lutron patent, a switch can’t report back to a z-wave hub that it’s been pressed unless the company licenses the patent from Lutron. Cooper and Leviton do. But GE, Evolve, and Intermatic do not. So this only triggers after SmartThings sees the switch has been change later doing a routine ‘update’ rather than right away. This can be minutes of delay. Not a huge deal sometimes… but if it’s the middle of the night and there is a scared little girl… then it’s a big deal.

Anyway, I’ve decided to bit the bullet and ordered a Leviton switch to put in place of this. Now that I know the setup and program will work I’m more willing to spend the money. Not happy about it… I’ve already spent WAY too much on z-wave stuff. But it will be nice to have this project working correctly.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #20

I haven’t read the Lutron patent you refer to… I wonder how broadly it can be interpreted (scope)…

You mention that Z-Wave light switches are required to license or desist.

  • What about ZigBee based switches?
  • What about the little on/off black button on the SmartenIt SmartThings power outlet (ZigBee)?
  • What about Z-Wave switched and/or dimmable outlets --> I use Remotec -- hmmm... I have not explicitly checked to see if they report back upon button press: Wait: I'll do it now...

testing … testing … testing …

It works! I tested with the “Notify Me When” SmartApp, and an in-wall Z-Wave outlet (I think GE branded, not sure), a Z-Wave dimmable switch module with a manual press button for physical override, and the SmartThings Power Outlet: All of them immediately report the event.

So:

(a) Either the patent does not apply to power outlets with override switches (as opposed to a wall light switch?).
or
(b) These also license or violate the patent.

Right??!?

…CP.


3-way Auxiliary Switch with LED indicator light?