# Bedroom motion sensor rule

**URL:** <https://community.openhab.org/t/bedroom-motion-sensor-rule/91167>\
**Category:** Tutorials & Examples\
**Tags:** hue, zigbee, motionsensor\
**Created:** [January 23, 2020, 9:13pm UTC](https://community.openhab.org/t/bedroom-motion-sensor-rule/91167 "2020-01-23T21:13:13Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [January 24, 2020, 4:00pm UTC](https://community.openhab.org/t/bedroom-motion-sensor-rule/91167/5 "2020-01-24T16:00:57Z")

</div>

You don’t need to cancel motionTimer inside motionTimer’s lambda. The motionTimer has already triggered, there is nothing to cancel. Just don’t do anything and set motionTimer to null at the end.

It helps to treat as a new context just like { } and indent all the code between them. It really helps one understand what the context is.

Consider failing fast. This can shorten Rules and reduce the number of indents leading to over all simpler Rules. For example, “Motion sensor while sleeping” only does something if the Sun is OFF and MotionSensor is ON. So

```auto
when
    if(Sun.state != OFF || MotionSensor.state != ON) return;

    SleepWalk = if(Door.state == ON) 1 else 0 // this is a ternary operator
    logInfo("sleepwalk", "sleepwalk = " + SleepWalk)

end

```

The above is part of [Design Pattern: How to Structure a Rule](https://community.openhab.org/t/design-pattern-how-to-structure-a-rule/45360). The same can be applied to the Motion Sensor Rule to simplify it a little bit as well.

Using [Design Pattern: Expire Binding Based Timers](https://community.openhab.org/t/design-pattern-expire-binding-based-timers/32634) could also simplify things a little bit as it will move the Timer body to it’s own Rule.

---

_[View the full topic](https://community.openhab.org/t/bedroom-motion-sensor-rule/91167)._
