[Solved] Door sensor works properly on open and close, but turns the light back on if the door is left open

Using the Xiaomi Aqara door/window sensor, the light turns on and off when I open and close the door, but I have a button on my bedside lamp which turns off the bedroom light when I’m going to sleep. If I use that button to turn off the light but I leave the door open, the light turns back on after some time.

xiaomi.py

class MiDoor(hass.Hass):
    def initialize(self):
      if "door_sensor" in self.args:
        for sensor in self.split_device_list(self.args["door_sensor"]):
          self.listen_state(self.state_change, sensor)

    def state_change(self, entity, attribute, old, new, kwargs):
      for light in self.split_device_list(self.args["lights"]):
        if new == "on" and self.get_state(light) == "off":
          self.log("Turning " + light + " On")
          self.turn_on(light)
          # self.run_in(self.light_off, self.args["time_on"], switch=light)
        elif new == "off" and self.get_state(light) == "on":
          self.log("Turning " + light + " Off")
          self.turn_off(light)
        elif new == "on" and self.get_state(light) == "on":
          pass

    def light_off(self, args):
      self.log("Turning " + args["switch"] + " Off")
      self.turn_off(args["switch"])

apps.yaml

BedroomDoorLight:
  module: xiaomi
  class: MiDoor
  door_sensor: binary_sensor.door_window_sensor_158d0002286c1d
  lights: light.bedroom

The only code here to turn the light on is when the door sensor changes state. Is that log message appearing in the log when the light turns on?

Yeah, that’s what confuses me. The only thing I can think of is that the door sensor is incorrectly reporting a change in state, but I haven’t monitored it to see if that’s happening.

The log message appears when the light is turned on and off.

If there is no log message from the app when the light turns on, then something else is turning the light on.

Do you have any other apps or automations that are running?

I just replicated the scenario and the light came on again.

It’s definitely the app in question because the only other apps controlling that light are for manual click buttons, and the log shows:

2018-03-29 16:53:36.443649 INFO BedroomDoorLight: Turning light.bedroom On
2018-03-29 16:54:36.256225 INFO BedroomDoorLight: Turning light.bedroom On

i.e. two consecutive instances of the app turning the light on.

That makes me think, could a possible workound be to implement a listener for manual light state changes, or is that unnecessarily complicated?

So you are getting a message in the log when the light turns on. Sorry, I thought you meant you were not.

It looks like your sensor is sending the state repeatedly, every 60s, rather than just at a state change. You might be able to disable this in the sensor.

If not, you can alter your push button to set a flag somewhere (either an input_binary in HA or an appdaemon variable) to indicate you are asleep, and adjust your app.

I do a similar thing, but the flag is triggered when I put my phone on the charger bedside my bed, which stops the bedroom motion sensor turning the light on.

Thanks for the advice!

Could you please show me the code you’re using for that flag? I’m actually very new to this and most of my code is snagged and adapted from other people’s repos.

My app is structured differently, but in your case the simplest alteration would be to use an constrain_input_boolean in the yaml

 BedroomDoorLight:
    module: xiaomi
    class: MiDoor
    door_sensor: binary_sensor.door_window_sensor_158d0002286c1d
    lights: light.bedroom
    constrain_input_boolean: input_boolean.in_bed

which would stop the automation happening once the in_bed input_boolean was set.

The tricky part is getting the input_boolean set when you want it - but I don’t know enough about your system to help there.

Edit: I haven’t had a coffee yet this morning, so I maybe you have to reverse the logic of the input_boolean.

Cheers for that, great starting point.

I’ve just tested the system out again (with no changes made) and found another oddity:

I’ve programmed the button so a single click toggles the bedroom light and a double click turns of all lights in the house. If I open the door (turning on the light) then double click the button, the bedroom light turns off and stays off.

It seems like the app respects the off state when turn_off is used, but not if it’s toggled from on to off.

So I guess I’ll start by reprogramming the button to ‘if light is on, turn off’ and ‘if light is off, turn on’ since that should cover 99% of the use cases. EDIT: I was wrong, that didn’t work for some reason and now I’m even more confused.

It did just occur to me that you might be able to ignore the subsequent messages from the sensor by checking the old state

        if new == "on" and old == "off" and self.get_state(light) == "off":
2 Likes

You sir are a genius! It’s so simple!

‘Turn on the light if the door is open, but only if it was previously closed’. And here I was looking for a way to set a flag for every possible method I have of turning off the bedroom lights.

Thank you, fellow Graham, you’ve made my day! Now how do I mark your post as the solution?

1 Like