RM: Detect how a rule triggered

Well, as topic says, how can I determine how a rule was triggered? I need to know if it was triggered by;

  1. a device
  2. another rule
  3. manually clicking 'Run Actions'

I recently learned about the %device% variable and while I have been able to use it, I have found no documentation of it. Is there more like this and what info do they hold? Searched forum and RM docs...


You can't. :slight_smile: The closest you can probably get is %device%. This might be less exciting than you think: it's just a string variable that holds the name of the most recent triggering device. If there is no triggering device (e.g., because the rule was manually run via "Run Actions" button or you have a trigger that does not involve a specific device, like a periodic trigger), then it will be empty. The same applies to %value%, another string variable that holds the value of the attribute that triggered the rule. Running one rule from the actions of another rule is different: the rule that was called will populate both of these variables with the device that triggered the other rule in the first place.

But not all hope is lost. Would it work to just check a specific device state? To give a simple example, if one rule is triggered based on a switch turning on and then that rule calls another rule, would it be sufficient in that rule to test whether the switch is on (e.g., IF (Switch 1 is on) THEN...)? Of course, this doesn't mean that the switch triggered the rule, just that it's currently in this state, which is why I phased the question the way I did, but sometimes device states can be useful. (It should be noted that you can do a comparison on %device% with a conditional to see if it matches a certain string, i.e., if that device triggered the rule or something in the chain, but I'd consider that error-prone and bad practice personally.)

Alternatively, it sounds like you might be doing different actions based on different triggering devices. In that case, I'd ask: why keep them in the same rule at all? As people used to say, "rules are free" (and still do say, but this was a bigger thing back when individual rules held far less power than they do now). :slight_smile:

If you aren't sure if/how any of the above suggestions could be made to work for you, then I'd suggest providing a specific automation you're trying to accomplish where you had this approach in mind. Someone might be able to provide advice that is both more targeted and more elegant than the general suggestions above.

Good luck!

1 Like

If you really need to know you could set up a rule for each trigger. I'm sure there would be challenges for that approach but....

1 Like

Nice answer as always, thanks!

I am trying the conditional compare statement atm, but as you say, not 100% accurate always, hence my quest for a better way to do this.

I know people argue for multiple rules, but being a bit old school, I think it's easier to maintain one rule instead of several when contained code is identical except for the trigger... I did actually break down a huge RM rule into 26 smaller, but my hand still aches from all the clicking...

Reason for all this is I want to reuse the same code block to determine if motion, rule or manual triggered, to add or omit a wait for condition statement to change scene or turn off a light based on virtual switches and illumination. Sounds a bit messy when describing it... I think I will re-think this a bit more. You have confirmed what I was afraid of though. I don't have a large system, so I therefore don't have a plan of sorts either. Maybe I should draw one up, just to be consistent...

Yes, that could also work, but I think I need to re-structure my approach... Got a little messy... :smiley: Thanks