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"])
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.
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.
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.
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.
‘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?