Testing for undefined variables

Rule Machine also has a few "built-in" variables local to each rule that refer to the last device event, usually the most recent trigger event (except as below, or if the trigger was not a device — a schedule, a hub/location event, manually running rule actions or running from another rule, etc., in which case these variables are not guaranteed to have a specific value). These can be referred to in string inputs using:

  • %device%: name of the device that generated the event (or "timeout" after a "Wait..." action if a timeout is specified on a wait and the next action is reached because the timeout expires)
  • %value%: the value of the event (e.g., could be "off" for a switch)
  • %text%: the descriptionText property for the event (as provided by the driver, often in a format resembling "Living Room switch is off")

I have a rule with multiple triggers. Some of the triggers provide the %text% variable and others don’t.

How can I test whether or not this variable exists so I don’t trying to use it when it doesn’t exist, i.e. when it shows as “null” in RM actions?

Just checking if it's equal to the literal string "null" works for me. You do have to assign the value of this variable to a "real" variable to be able to use it in the first place, including to check its value, but you can do so after that. Here's an example:

Alternatively, depending on what you are trying to do, there may be another way. For example:

If these are all device events and the issue is just that sometimes the device driver doesn't set the descriptionText attribute on the event, this is almost always something similar to what you'd get from "%device% is %value%" (which should always be there for any device event; the descriptionText property is optional and corresponds to %text%). If you have non-device triggers, that is the kind of things the docs are referring to.

Your post might also be clearer if you edit it to show what parts are quotes from the Rule Machine documentation (use the quotes button on the editing toolbar to easily mark up the text in this way) and which is your specific question.

1 Like

Thanks, I must have mistyped something when I tried testing for n u l l.
Works as you described.

Tom