Help shortening one of my persistence rules

The Design Pattern postings. The DP Vincent is illustrating is Design Pattern: Associated Items.

Assuming you put the motion sensors in a Group, let’s call if MotionSensors…

rule "Motion Detection (PART)"
when
    Member of MotionSensors changed from OFF to ON
then
    if(Security_System_PART.state != ON) return; // fail fast, we do nothing if it isn't ON so just exit the Rule

    val message = triggeringItem.name.replace("_", " ") + " Detected!" // replace the underscores with spaces in the Item name an append the word "Detected!"  as the message.

    sendNotification("myemail@live.com", message) // send the message
end

This code uses Design Pattern: How to Structure a Rule.

  1. Determine if the Rule needs to run at all, and if not exit.
  2. Calculate what needs to be done. In this case create the message to send in the notification.
  3. Do the action, in this case send the notification.
1 Like