For in self args targeting not selecting correct devices

The code below will turn off all lights and switches rather than the one specified in the apps.yaml. Hoping someone could assist with changing it so it will only turn off the entities listed in apps.yaml.

ten_minutelighttimer:
  module: closettimers
  class: tenminutelighttimer
  entities: 
    - switch.zooz_zen21_switch_v2_0_switch
  constrain_input_boolean: input_boolean.ten_minute_timer_toggle
import appdaemon.plugins.hass.hassapi as hass
# Closet Timer


class tenminutelighttimer(hass.Hass):

    def initialize(self):
        for switch in self.args["entities"]:
            self.listen_state(self.closetlightcontrol, new="on",
                              duration=5)  # wait for 10 min

    def closetlightcontrol(self, entity, attribute, old, new, kwargs):
        if new == "on":
            self.turn_off(entity)
            self.log("""Line: __line__, function: __function__,
                      Message: Closet Light Turned off""")

First, this line is not needed, as the closetlightcontrol will only be called if the new state is “on” for 5 seconds (duration=5).

if new == "on":

The code you wrote will turn off “switch.zooz_zen21_switch_v2_0_switch” if it has been in the state “on” for 5 seconds. This will not affect any other light, could it be that you have other automations that do something when this switch is turned off?

If you could tell me what you exactly want to do, I can help you to write the code. Do you just want the light to go off after 10 minutes?

1 Like

Hi,

I am trying to create an app that will turn off any switches or lights that are listed in the entities list of the apps.yaml after 10 minutes. I will use these mostly for closets but also for the laundry room as my kids often leave these on.

Also I just turned off all of my automation’s and reloaded automation’s and tested and it’s turning off the following entities: light.jasco_products_14294_in_wall_smart_dimmer_level, switch.lamp and switch.zooz_zen21_switch_v2_0_switch. I haven’t tested with any other items but I would imaging it would turn off other as well. This baffles me as I don’t run node red or anything else that could be causing this to happen. I even changed the entity in the apps.yaml to switch.lamp and it seems to affect all switches and lights.

Here is the updated code I tested with

import appdaemon.plugins.hass.hassapi as hass
# Closet Timer


class tenminutelighttimer(hass.Hass):

    def initialize(self):
        for switch in self.args["entities"]:
            self.listen_state(self.closetlightcontrol, new="on",
                              duration=5)  # wait for 10 min

    def closetlightcontrol(self, entity, attribute, old, new, kwargs):
        self.turn_off(entity)
        self.log("""Line: __line__, function: __function__,
                    Message: Closet Light Turned off""")

Can you please add

self.log(entity)

right before

self.turn_off(entity)

and then show us the log when it turns off all the lights. Could you please also show the full apps.yaml file?

I toggled on switch.lamp and switch.zooz_zen21_switch_v2_0_switch

Log:


2019-11-25 19:35:02.012876 INFO ten_minutelighttimer: switch.zooz_zen21_switch_v2_0_switch,
2019-11-25 19:35:02.024498 INFO ten_minutelighttimer: Line: 16, function: closetlightcontrol,,
2019-11-25 19:35:32.012461 INFO ten_minutelighttimer: switch.lamp,
2019-11-25 19:35:32.797177 INFO ten_minutelighttimer: Line: 16, function: closetlightcontrol,,
2019-11-25 19:34:43.752424 INFO AppDaemon: Initializing app ten_minutelighttimer using class tenminutelighttimer from module closettimers,
2019-11-25 19:34:43.751044 INFO AppDaemon: Reloading Module: /conf/apps/closettimers.py,
2019-11-25 19:34:43.750712 INFO AppDaemon: Terminating ten_minutelighttimer,
                    Message: Closet Light Turned off

apps.yaml:

audio_control:
  module: audio-control
  class: audiocontrol
  mediaplayer: media_player.mpd
  switch: switch.office_amp
  constrain_input_boolean: input_boolean.amp_control_toggle

ten_minutelighttimer:
  module: closettimers
  class: tenminutelighttimer
  entities: 
    - switch.zooz_zen21_switch_v2_0_switch
  constrain_input_boolean: input_boolean.ten_minute_timer_toggle
import appdaemon.plugins.hass.hassapi as hass
# Closet Timer


class tenminutelighttimer(hass.Hass):

    def initialize(self):
        for switch in self.args["entities"]:
            self.listen_state(self.closetlightcontrol, new="on",
                              duration=5)  # wait for 10 min

    def closetlightcontrol(self, entity, attribute, old, new, kwargs):
        self.log(entity)
        self.turn_off(entity)
        self.log("""Line: __line__, function: __function__,
                    Message: Closet Light Turned off""")

Ahhh I see the issue know, should have seen it in the first post already.

You are missing the entity you want to listen to in your listen_state. Change to this:

def initialize(self):
    for switch in self.args["entities"]:
        self.listen_state(self.closetlightcontrol, switch, new="on",
                          duration=5)  # wait for 10 min

The one you have know will listen for all entities and if any entity has been turned on for 5 seconds it will be turned off.

1 Like

Thanks a bunch. This resolved the issue. I can’t believe I missed this. I am very new to appdaemon and I’m hoping to use it to start learning python. Thanks again!

1 Like

I feel you, it was the same for me in the beginning.

You can check out my repo if you want, I do all my automations with AppDaemon.

1 Like