Is There a Variable for Rules?

Hi. I probably didn’t spend enough time combing through all the resources available, filtering out old outdated information from the new (which seems to become increasingly difficult), but I thought I’d post this question anyway.

I want to create a rule that seems pretty darn simple. I want to turn on a light if a motion sensor triggers, and then turn that light off after 3 minutes. Easy. Also, I only want to do this if the light isn’t already ON at the time, which is also pretty easy.

HOWEVER, if the light is manually turned on during this 3-minute window, I want to “get out” of this “security light 3-minute mode” and NOT continue the rule, and NOT turn it off at the 3-minute mark.

The developer in me seems to want to simply set a VARIABLE to indicate whether I am in this 3-minute mode or not, and base rule decisions off of that. I however don’t see any such support for this.

SharpTools does have this sort of concept of variables but I would prefer to keep it all within SmartThings and also prefer to keep it all in the hub if possible.

I am guessing that I could set up a virtual device to act as a sort-of variable, and use that in the logic, but I figured I’d ask here to see what the most preferred way in the year 2024 is to do this.

Thanks!

2 Likes

Unfortunately, no variables in Routines or Rules currently.

Couple of ways to approach this. One way would be to use the Edge Counter Utility V1.1 from @taustin to start a 3 minute timer when the motion detection Routine is triggered. Then if the light is turned on by button press, turn off the timer. If you use this method, you would want separate Routines for the motion triggering to turn on the light and turn off the light after 3 mins. This is because you can’t stop the Auto turn off Action once it’s been triggered.

Another approach would be to use a text field in Aplicaciones Virtuales Mc Edge driver from @Mariano_Colmenarejo as a proxy for a variable. Here you could write some value to the text field when motion is detected. Then if the text field still has the value after 3 mins, clear the text field and turn off the light. If a button push is detected, you can clear the field so that the light will not be turned off after 3 mins.

3 Likes

What you mean by manually turned on?
Do you have separate switch / button for manual operation?

Simple Zigbee ST switch (light) doesn’t know if it’s operated using automation mode or manual mode.

1 Like

You didn’t mention whether you have a smartthings/Aeotec hub or not. You do have to have one in order to use any edge driver. But the vast majority of smartthings users do not have one at the time of this posting (they have a Samsung product like a galaxy phone or tablet, a Samsung smart vacuum, Or a Samsung smart appliance, or television, although more of the latter group are getting built-in hub capability these days).

Anyway, you asked about best practices for 2024. They are different if you have a hub or not.

  1. If you have a hub, one of the edge drivers that @h0ckeysk8er mentioned is probably the most popular method.

  2. If you don’t have a hub, as you already noted, you can do this with a virtual switch, which can be created through the advanced page of the official web interface to your SmartThings account. No hub required.

my.smartthings.com/advanced

The trick in all cases is to separate turning on from turning off and having a precondition for the time-based turning off, based on a virtual switch state or one of the edge driver values as @h0ckeysk8er indicated.

People handle this slightly different ways for different use cases, but that’s the basic idea right now.

2 Likes

Some switches, particularly zwave devices, do know if they are turned on manually at the wall or from a network command.

But in most cases, people are looking at two different trigger devices: motion sensor or occupancy sensor, which has the time turning off clause and a wall switch. So it can be distinguished that way.

In fact, I’d say the most popular use case of this type is

“Turn off after no motion for x minutes UNLESS The wall switch is used during that interval.”

A common example is normally you have your front porch lights turn off automatically after a few minutes, but if the dog gets out and you’ve turned the light on with the switch, you want it to stay on until you turn it off again. (Or until some other automation is triggered like your good night routine. It all depends on the details.)

4 Likes

Plus, using preconditions, you know whether the light is on or not and whether you’re in the “motion detected” mode. So, you have three Routines:

If Light is off (precondition)
   Motion is detected
Then
  Turn on light
  Write value to text field
If Text field has value (preconditon)
   Light turns on 
Then
   Clear text field
If Text field has value (precondition)
   Light is on for 3 mins
Then
  Turn off light
  Clear text field

Similarly, you can use the timer like:

If Light is off (precondition)
   Motion is detected
Then
  Turn on light
  Start Timer
If Timer is less than 3 min (preconditon)
   Light turns on 
Then
   Turn off timer
   Reset timer
If Light is on (precondition)
   Timer is 3 mins
Then
  Turn off light
  Stop Timer
  Reset Timer
If Light Turns off
Then
  Stop Timer
  Reset Timer

You need the extra Routine with the timer to handle both the automated 3 min use case of turning off the light and the manually triggered use case for turning off the light so that the timer is stopped and reset in both cases.

3 Likes

It is possible to use a virtual switch instead of a text field.

2 Likes

Sure, turned on/off is just state checking the same as looking at the value of a text field. I use a virtual switch to track when it’s “Night Time” at my house because I need both a fixed and relative time to define that interval (Sunset to 1am).

That is the method @JDRoberts suggested in the event you don’t have a hub to run one of the Edge drivers.

2 Likes

How will these routines run? Wont the light already be on from the first motion routine? I have switches that a routine can be triggered with a toggle up or toggle down, but then I also have switches that the only option in the routine is on or off.

Yes, if your driver(s) support toggle up/down, then those are a better trigger than just on/off. Whether the light is actually on or not, the act of pushing a toggle up or flipping a switch to the on position should generate an “on” event and that event is the trigger. Now, I suppose there may be devices that don’t generate the “on” event if they are already on and in that case, you are correct, the methods I suggested won’t work. For his use case, in general, you have to be able to detect that a toggle is pushed or a switch is flipped or a light that is on is “turned on” to distinguish between an automated event vs a manual event.

2 Likes

I have a bunch of switches, both Zigbee and Z-Wave, and the only ones that have the option based on button push are my Zooz switches. The OP didn’t specify the switch he has or what he meant by manual turn-on. But I agree with you; this will work with the right switch. I can’t think of a way to do it without it.

2 Likes

Some of my GE/Jasco models support pushed/toggled actions. The ones with motion detection don’t (even though I’m guessing a presentation could be built to add in button actions to the automations section).

1 Like

I agree with this also, with the right driver.

1 Like

Thank you all for your numerous and rapid comments.

Hi!

(1) Yes, I have a SmartThings Hub.

(2) Yes, in fact the switch of interest happens to be a Zooz switch.

(3) Yes, by “manual turn on” I did mean physically toggling the physical switch. It is however a 3-way Zooz switch whose counterpart works with a traditional wired on/off switch, so I do not know if the “toggle” event will be raised when the 3-way traditional switch is flipped or just when the real Zooz switch is flipped, but I’ll deal with that when I get further.

I will investigate the edge driver text-field @Mariano_Colmenarejo edge driver as that feels like the cleanest. My only other question/thought is if there is a way to, within a routine, say “trigger this OTHER routine after 3 minutes” to do the logic of “then if the text field still has the value after 3 mins, clear the text field and turn off the light.”

Alternatively, is it possible to just write all this logic succinctly and run it on the hub instead of messing with all these multiple/cascading routines?

Thanks!

You can figure that out by doing some driver logging with the ST CLI. Load that onto a Windows/MacOS/Linux system and use the command “smartthings edge:drivers:logcat” and then select the driver that is being used by your Zooz switch. You can test whether an event is generated for various actions such as turning on the wired 3 way and pushing the toggle up with the light is on.

Yes, you could create a Manual Routine (formerly a Scene) to perform those Actions and call that from a Routine. I do that with my evening and bedtime lighting Routines. The automated versions run at specific times and call the Scene which also allows me to tell Alexa to turn them on/off via voice command if I need them earlier/later than scheduled.

Nope. Routines are not sophisticated enough to handle this level of complexity. You could probably do it with Sharptools but it won’t run locally. If you have a Hubitat hub, you could probably do it in Webcore and use one of the drivers that sync’s device state between ST and Hubitat. Personally, I don’t think having 3 Routines is too complex to achieve the behaviors you want and adheres to the KISS principal IMO.

4 Likes

You may be able to achieve this with Rules API, and create a single Rule containing all the mentioned conditions that will run in the background, but I am not sure.

Ok thanks, yes this is what I was thinking. I have a handful of routines set up right now to make it happen but will also look into the rules.

Thanks!

@mlchelp

So, after a little testing, here is what I’ve found:

  • If you use “Light is on” as a precondition, the app won’t let you create a Routine with “Light turns on” as the trigger
  • If the light is on, turning on the light generates “the light turned on” event which can be seen in driver logging. However, because there is no state change from off to on, the Routine will not be triggered for “Light turns on”
  • With my GE Motion/Dimmers using either Phil’s or Corey’s driver (after I fixed a presentation issue caused by the wrong profile being assigned), I had a Toggle Up and Toggle Down trigger and button events were always reported as up or down regardless of multi-push.
  • For my GE Dimmer Switches, I only have pushed_2x, up_2x, and down_2x for triggers and button events are NOT generated for single toggle up/down, only for 2x pushes.

So, long story short, you were correct that my suggestions on how to turn off and reset the timer or clear the text field will not work and the only way to support this use case will be if the device generates something to indicate that buttons were pushed or some other indicator other than the SWITCH or SWITCH_MULTILEVEL commands which simply tell you that the device has turned on/off and the current dimmer level (if appropriate).

2 Likes

That’s really down to how Rules work. The precondition becomes true when the light is turned on, the same as the trigger condition does.

The actually underlying condition would be ‘light is on’ and that will happily trigger without state changes.

The default behaviour of ST is for only state changes to propagate through the system. That can be changed. The CLI created local Virtual Switches, for example, do so in order to mimic the legacy DTH based ones. With buttons it is absolutely essential because buttons don’t reset to a standby state (the mobile apps just make it look like they do).

It’s again really down to the hardware. If the hardware can send repeated sequences of on or off then ST can follow suit. If the hardware is only sending on/off/on/off then there is little point doing otherwise.

However even if the hardware can send on events you still don’t know that they were local.

That’s pretty much the bottom line.

To override the behaviour of motion controlled lights I use virtual motion sensors (if you use Rules directly it simplifies the logic, if you use Routines you might just as well use a virtual switch) but I separate out the motion controlled lighting from other room lighting. Indeed all rooms retain their original dumb lighting. So it is easy for me.

Updated to make it clear that is the local virtual switches that allow all events out.

1 Like

That’s not my experience in testing. I created a Routine with the precondition and trigger I suggested:

Although the hardware will generate “on” events even when in the “on state”, the Routine is only triggered if the state changes from “off” to “on”.

connecting... connected
2024-03-05T16:41:44.545142724+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:44.548418204+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=7}, cmd_class="BASIC", cmd_id="REPORT", dst_channels={}, encap="CRC16", payload="\x07", src_channel=0, version=1}
2024-03-05T16:41:44.559379829+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:44.569688995+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"switch","capability_id":"switch","component_id":"main","state":{"value":"on"}}
2024-03-05T16:41:44.614415599+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"level","capability_id":"switchLevel","component_id":"main","state":{"value":7}}
2024-03-05T16:41:44.675076849+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:44.679007787+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:44.681340912+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=255}, cmd_class="BASIC", cmd_id="SET", dst_channels={}, encap="CRC16", payload="\xFF", src_channel=0, version=1}
2024-03-05T16:41:44.716880245+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:44.727413724+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"button","capability_id":"button","component_id":"main","state":{"value":"up"},"state_change":true}
2024-03-05T16:41:44.752034078+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:44.927459807+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:44.929634203+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=99}, cmd_class="SWITCH_MULTILEVEL", cmd_id="REPORT", dst_channels={0}, encap="CRC16", payload="c", src_channel=1, version=1}
2024-03-05T16:41:44.956749786+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:44.958870703+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"switch","capability_id":"switch","component_id":"main","state":{"value":"on"}}
2024-03-05T16:41:45.002933495+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:45.004773641+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"level","capability_id":"switchLevel","component_id":"main","state":{"value":100}}
2024-03-05T16:41:45.026765432+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:54.302436819+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:54.304540507+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=99}, cmd_class="BASIC", cmd_id="REPORT", dst_channels={}, encap="CRC16", payload="c", src_channel=0, version=1}
2024-03-05T16:41:54.335414361+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:54.337529757+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"switch","capability_id":"switch","component_id":"main","state":{"value":"on"}}
2024-03-05T16:41:54.369956236+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"level","capability_id":"switchLevel","component_id":"main","state":{"value":100}}
2024-03-05T16:41:54.393329007+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:54.465598965+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:54.466581361+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=255}, cmd_class="BASIC", cmd_id="SET", dst_channels={}, encap="CRC16", payload="\xFF", src_channel=0, version=1}
2024-03-05T16:41:54.495112902+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:54.499746048+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"button","capability_id":"button","component_id":"main","state":{"value":"up"},"state_change":true}
2024-03-05T16:41:54.524779548+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:54.525785923+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:54.528376944+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=99}, cmd_class="SWITCH_MULTILEVEL", cmd_id="REPORT", dst_channels={0}, encap="CRC16", payload="c", src_channel=1, version=1}
2024-03-05T16:41:54.541682277+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:54.542667006+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"switch","capability_id":"switch","component_id":"main","state":{"value":"on"}}
2024-03-05T16:41:54.557661402+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:54.558620486+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"level","capability_id":"switchLevel","component_id":"main","state":{"value":100}}
2024-03-05T16:41:54.565070756+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:57.270944608+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:57.274293629+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=255}, cmd_class="BASIC", cmd_id="SET", dst_channels={}, encap="CRC16", payload="\xFF", src_channel=0, version=1}
2024-03-05T16:41:57.315599962+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:57.317962629+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"button","capability_id":"button","component_id":"main","state":{"value":"up"},"state_change":true}
2024-03-05T16:41:57.373254941+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled
2024-03-05T16:41:57.375216441+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Received event with handler unnamed
2024-03-05T16:41:57.378622691+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> received Z-Wave command: {args={value=99}, cmd_class="SWITCH_MULTILEVEL", cmd_id="REPORT", dst_channels={0}, encap="CRC16", payload="c", src_channel=1, version=1}
2024-03-05T16:41:57.405178066+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:57.408104254+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"switch","capability_id":"switch","component_id":"main","state":{"value":"on"}}
2024-03-05T16:41:57.439712191+00:00 TRACE GE Z-Wave Motion Switch/Dimmer  Found ZwaveDispatcher handler in ge-zwave-motion-switch
2024-03-05T16:41:57.440756879+00:00 INFO GE Z-Wave Motion Switch/Dimmer  <ZwaveDevice: 1459e898-1875-465f-872a-606f2ff8461c [7D] (Guest Bedroom Light)> emitting event: {"attribute_id":"level","capability_id":"switchLevel","component_id":"main","state":{"value":100}}
2024-03-05T16:41:57.447206816+00:00 DEBUG GE Z-Wave Motion Switch/Dimmer  Guest Bedroom Light device thread event handled