Can't figure out what's turning off my bedroom light

Apart from a Xiaomi button that controls the light, I don’t have any other automations that turn it off, but every now and then it turns off by itself.

Here’s the app I have for controlling the light (controlled by a door sensor):

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 old == "off" and self.get_state(light) == "off":
      self.log("Turning " + "on " + light)
      self.turn_on(light, brightness='254')
    elif new == "off" and self.get_state(light) == "on":
      self.log("Turning " + "off " + light)
      self.turn_off(light)

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

Hello,

You said only a Xiaomi button is used to control the light. At the same time, you make mention of a door sensor. Can you kindly state how it’s controlled exactly?

Also how many door sensors or lights do you have? It’s looking like you trying to control several since you have the split_device_list function in there

Lastly can you check your history to see if by any chance the Sensors used to control the door is not sending false signals intermittently?

Regards

1 Like

besides the questions that odianosen asks, it would be helpfull to know if you get any log entries when the lights turn off.

you have this line

self.log("Turning " + "off " + light)

so if the lights turn off unwanted and you have an entry in your log, then you probably have a false signal from the sensor.
if there are no entries, then the automation doesnt turn of the lights, but something else does. (powerblip, a remote somewhere, signal from something else, broken device, etc.)

1 Like

You said only a Xiaomi button is used to control the light. At the same time, you make mention of a door sensor. Can you kindly state how it’s controlled exactly?

Sorry I’m not sure what you mean by “how it’s controlled exactly” apart from the code snippet I posted. It’s connected to the Xiaomi Aqara gateway.

Also how many door sensors or lights do you have? It’s looking like you trying to control several since you have the split_device_list function in there

I only have one door sensor that controls any lights, but there are 7 lights in my Home Assistant setup. I think I copied the split_device_list from code that someone else had posted, so admittedly I’m not quite sure what it’s doing.

Lastly can you check your history to see if by any chance the Sensors used to control the door is not sending false signals intermittently?

It doesn’t look like there are any false signals being sent. In the Home Assistant log there’s no mention of the door closing like there usually would be, just ‘Bedroom light turned off’.

besides the questions that odianosen asks, it would be helpfull to know if you get any log entries when the lights turn off.

Truth be told I haven’t been able to access the Appdaemon frontend for a while, but it hasn’t bothered me because I haven’t touched anything for a while. my_ip:5050 returns a 404 error.

Can you post your apps.yaml config for this app?

appdaemon doesnt have a frontend.
its just a plain python program.

if you cant reach your device, you cant change anything and if you cant change anything and cant look at the logs there is noone that can help you :wink:

i suppose that you have the dashboard running on 5050.
in that case the dashboard is probably not running. and that lets me suspect that appdaemon isnt running at all. and when AD isnt running, then the app cant be the problem :wink:
and the only way to see if AD is running is to look at the logs.

Can you post your apps.yaml config for this app?

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

@ReneTode ahh, I must be getting confused because I had some kind of frontend back when I was originally running appdaemon through hassio, which showed the logs etc. How do I set it up so I can view the logs?

My appdaemon is definitely running because it’s the only thing controlling my motion and sensors too, and everything works normally apart from this strange issue with the bedroom light turning off by itself.

Ok, I think I have an idea what might be going on.

You see in this side of your code

elif new == "off" and self.get_state(light) == "on": 
    self.log("Turning " + "off " + light) 
    self.turn_off(light)

change it to

elif new == "off" and old == "on" and self.get_state(light) == "on": 
    self.log("Turning " + "off " + light) 
    self.turn_off(light)

Reason being that I have seen strange cases where both old and new are the same thing. This might be how the component is being processed in HA, and it might be messing with AppD. In such cases, especially in Media players where I have had both old and new as playing and of course it bugs up my code, I had to specify each.

Do it that way, and see if it still happens.

Regards

Thanks! I’ll give that a try and see if it fixes it :slight_smile:

that “frontend” was hassio.

in your appdaemon.yaml you can configure were your logs should be placed.
if you havent set that it will go to your default linux logging.

it all depends on how you use and setup your system.
i use winscp to view the system that is running my appdaemon.
that gives me the option to view the HD from the device quite like in explorer.
but others use samba or have a monitor, keyboard and mouse attached to the device.

so ineed to know more about how you use your system to tell you how to work with it.

@odianosen that could work if the sensor has attributes that can change.
if the sensor doesnt have attributes it wouldnt do anything.

listen_state listens to a change in the entity to are listenen to.
and that doesnt need to be the main state but can also be any change in the state from an attribute.