Appdaemon app doesn't execute until the end, no errors in log

Hi everyone,

I tried to build an app that turns on the light to a certain brightness based on the state of the day.

I already have an app, which changes an input select to the current state of the day, e.g. Monday between 9:00 and 23:00 the input select would show “Tag Wochentag”.

The app I trying to build does not execute until the end.

The app called montion_light_v2.py looks like this:

import appdaemon.plugins.hass.hassapi as hass

###############################################################################
# App to turn on light when motion is detected, then turn off after few delay
# If the computer is on, a trigger of the motion sensor has no effect
# args:
#
# sensor: motion sensor to use as trigger
# light_entity: light to turn on/off
# delay: time until light turns off again, default 30 seconds
# input_select: name of the input_select which shows time of day
# brightness_level: brightness level in % for different periods of day
#   Morgen Wochentag: 30
#   Tag Wochentag: 70
#   Nacht Wochentag:
#   Morgen Wochenende:
#   Tag Wochenende:
#   Nacht Wochenende:
###############################################################################

class MotionLight(hass.Hass):

    def initialize(self):

        self.handle = None

        if "delay" in self.args:
            self.delay = self.args["delay"]
        else:
            self.delay = 30 # default delay

        if "sensor" in self.args:
            self.listen_state(self.motion, self.args["sensor"])

    def motion(self, entity, attribute, old, new, kwargs):
        if new == "on":
            self.log("Motion detected")
            if self.get_state(self.args["light_entity"]) == "on":
                self.log("Light is already on, restart timer")
                self.cancel_timer(self.handle)
                self.handle = self.run_in(self.light_off, self.delay)
            else:
                self.log("Turn on light and set timer")
                self.brightness_map = self.args["brightness_level"]
                self.log("error here")
                self.log(self.brightness_map)
                state_of_day = get_state(self.args["input_select"])
                self.log(state_of_day)
                self.log("error later")
                desired_brightness = self.brightness_map[state_of_day]
                self.log(desired_brightness)
                self.turn_on(self.args["light_entity"], brightness = desired_brightness)
                self.handle = self.run_in(self.light_off, self.delay)

    def light_off(self, kwargs):
        self.turn_off(self.args["light_entity"])

The configuration looks like this:

Light Dress Room:
  module: motion_light_v2
  class: MotionLight
  delay: 300
  sensor: binary_sensor.aeotec_zw100_multisensor_6_sensor_9
  light_entity: light.ankleidezimmer
  input_select: input_select.state_of_day
  brightness_level:
      Morgen Wochentag: 30
      Tag Wochentag: 70
      Nacht Wochentag: 10
      Morgen Wochenende: 30
      Tag Wochenende: 70
      Nacht Wochenende: 10

As soon as the app detects motion, it starts but it does not turn the light on. The log file shows the following:

2018-07-20 17:04:00.013997 INFO State of Day: Changed input select to Tag Wochentag.
2018-07-20 17:04:18.729353 INFO Light Dress Room: Motion detected
2018-07-20 17:04:18.729900 INFO Light Dress Room: Turn on light and set timer
2018-07-20 17:05:00.013558 INFO State of Day: Changed input select to Tag Wochentag.

So it looks like the execution stops at “self.brightness_map = self.args[“brightness_level”]”.

I don’t understand what I did wrong, your help would be highly appreciated.

Thanks in advance and best regards

I solved it by myself. I noticed a missing “self.” before the get_state to get the value of the input_select. But this also didn’t work and returned the same log.

Strangely I had to reboot my machine completely for it to work, home assistant restart was not enough. Now everything works as intended.