AppDaemon motion detectionlights version 1.1

Ok, that cleared that one . But now below:

2016-11-23 21:19:24.010830 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.010291 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.010626 WARNING Unexpected error during loading of Motion Lights:
2016-11-23 21:40:15.010902 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.011621 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 665, in readApp
    importlib.reload(conf.modules[module_name])
KeyError: 'motionlights'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 685, in readApp
    init_object(name, class_name, module_name, config[name])
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 467, in init_object
    APPclass = getattr(module, class_name)
AttributeError: 'module' object has no attribute 'motionlights'

2016-11-23 21:40:15.011957 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.013196 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.013529 WARNING Unexpected error during loading of Motion Lights:
2016-11-23 21:40:15.013829 WARNING ------------------------------------------------------------
2016-11-23 21:40:15.014330 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 685, in readApp
    init_object(name, class_name, module_name, config[name])
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 467, in init_object
    APPclass = getattr(module, class_name)
AttributeError: 'module' object has no attribute 'motionlights'

2016-11-23 21:40:15.014634 WARNING ------------------------------------------------------------

Update: Solved; this needed to be MotionLights (capital M & L). Running ok!

p.s. Would it be possible to add an extra ‘parameter’ for elevation to the config? Actually only want the automation to work when below is true (in this case):

 entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 3.5

If I want this app to work in a different room; would I rename the app itself or would I create only multiple entries in the appdaemon.cnf?

you can use constraints in your cfg file.

like:

constrain_start_time = sunset - 00:45:00
constrain_end_time = sunrise + 00:45:00

if you realy want to use elevation you can make an input_boolean in HA which is on at the desired time and then use:

constrain_input_boolean = input_boolean.your_boolean

and yeah, you can reuse this app as often as you like. thats the big advantage from appdaemon.
so just create a new block in your cfg file with a different name and you can use different args, so different rooms.

Nice! Thanks!

Sorry, more Q’s: is the “delay” seconds or minutes? I now have delay = 300 but does not switch off (expecting 300 sec).

I have an scene that turns two lights on and a scene that turns these same lights off.

[MotionLights]
module = motionlights
class = MotionLights

constrain_start_time = sunset - 00:45:00
constrain_end_time = sunrise + 00:45:00 
sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
entity_on : scene.kitchen_timer
entity_off : scene.kitchen_timer_off
delay: 300

delay is seconds.
so it should turn off after 5 mins.
if it detects a second movement you should see that in your log or on screen.
and also after the timer ends you should get an entry in your log or on screen.

your config looks allright.

Oops, my bad! I did not reload HASS when I created the _off scene… All working as expected.

1 Like

i added an off function to your version of motion_lights.py to reset the timer, when at least one entity is turned off manually. if this functionalty is missing, an entity that is being turned off in HA, won’t turn on when motion is detected, as long as the timer is still running.

1 Like

i dont need it because my timer is set to 60 or 300 seconds, but nice.
it could even be better if the off switches would be defined seperatedly.

so that you could turn of the motion lights with any switch you set in the args.

A quick question

You have written:
def off(self, entity, attribute, old, new, kwargs):
if self.get_state(entity) == “off”:

wouldn’t it be more streamlined if you just do
if new == “off”:

And how do I make a post where I can paste code in a proper way? Bugs me that I can’t figure it out

/R

A question Rene:

Why do you specifically want new == on when setting up the timer? Wouldn’t it be better to allow for off to also trigger the timer? Then I think you could clean up your light_off to not need to look for motion still detected right?

/R

about your question to post code:

to post code correctly you need ` 3 times and then a new line. before and after the code.
not ’ and not " (sorry dont know the names from those symbols) the one you use to create è.

your question to diplix is right. that would be the way.

why do i want new= on?
i know that there are motion detectors which have there own timer. they keep on for the time they are set to, for instance between 5 an 30 seconds and you can set that with a potentiometer.
but my code has no way to detect that. so your delay would be affected by that.

the second thing you say is that you dont need to look after the motion detection is set to off.
and thats also not true. if you have an motion detector which goes to off after 5 seconds then you need to stay reel busy to make sure it still keeps detecting you.

suppose you use it for a bathroom. you want the light to stay on while you are on the toilet.
you dont move very much. lets say you move not at all for 60 seconds. allmost every motion detector would have gone to off by that time. but you still like the lights to stay on, untill your gone.

so thats why i think you need to keep checking untill the end off the delay. and only for movement.
thats also why the last line checks if the motion detector is still set to on at the end off the timer.

to use motion detection like this you want your motiondetector settings to be as short as possible.

Ok. Will try that out for posting code. Stay tuned! :wink:

Regarding what I meant with off is that new == off should also trigger the timer, not turn off the lights. Because then you will cover more more scenarios. E.g. When you move detection will go to on and timer will set but only at the beginning of movement. If the detection stays hi for longer than 60 seconds, the lights will turn off. But if you also care about the signal for new == off as in no motion detected and then also set the timer for countdown you will be less likely to actually need to check if sensors are still active.

Am I explaining it well enough or should I write a logical example of what I mean? :slight_smile:

/R

i think your logic is wrong.
actually the best motion detector you would have goes off diectly without any delay after the motion is detected.
but lets say you have 1 that stays high for 4 minutes.
the motion is detected and your timer is set to 5 mins. (bacause you like it to stay on for 5 mins after detection)
if your timer is influenced by your detector going to off, you lights would stay on for 9 minutes total.

going off is saying nothing about the motion, because of the fact that every motion detector is different.
it can say that there was no motion for some time (if you have a motion detector which stays high for repeated motion) but it can also say that just some time has passed since the first motion is detected.
and the some in some time is also unknown.

i set my detectors as low as possible. so it wont stay high for 60 seconds.
if you have a detector which stays high as long as it detects movement (and also has its own delay) you actually dont need an app like this. they you just would like your lights to go off when the detector goes to off.

so there is a hardware difference. some do have a decent program like this included in the hardware and some dont.
with this app you like the once that dont have any delay.

I see! I understand better now. Thanks for explaining.

It sounds like I need to investigate motion detection behaviour prior to buying any myself…

I’ll experiment with this when I get myself som motion detectors :slight_smile:

/R

i use cheap motion detectors that i can connect to my arduinos.
i could also use arduino programming to set my delay. but if it is in the hardware or the arduino is the same.
i dont want that because i like to be able to change settings from my pc and not on my hardware :wink:

good luck with finding out what is right for you.

@ReneTode

Haven’t changed this for a long time but now I want to differentiate the same light for evening use and night use. With this I mean that, when after 0:00 until sunrise, I want the motionlight still to work but with a dimmed light. What is the easiest way to achieve this? Was think of just duplicating the current one and then use times that are adjacent and refer to different scene. Code below. Is that the easiest way and would that work?

My apps.yaml would then contain:

MotionLights:
  module: motionlights
  class: MotionLights
  constrain_start_time: sunset - 00:45:00
  constrain_end_time: 0:00
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer
  entity_off: scene.kitchen_timer_off
  delay: 600

MotionLights:
  module: motionlights
  class: MotionLights
  constrain_start_time: 0:00
  constrain_end_time: sunrise + 00:45:00 
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer_night
  entity_off: scene.kitchen_timer_nigh_off
  delay: 200

yeah that should work
only you need to change the app name.

you can only have 1 time MotionLights.

so change it to:

MotionLights:
  module: motionlights
  class: MotionLights
  constrain_start_time: sunset - 00:45:00
  constrain_end_time: 0:00
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer
  entity_off: scene.kitchen_timer_off
  delay: 600

MotionLightsNight:
  module: motionlights
  class: MotionLights
  constrain_start_time: 0:00
  constrain_end_time: sunrise + 00:45:00 
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer_night
  entity_off: scene.kitchen_timer_nigh_off
  delay: 200

and you are good to go.

Thanks! Also had to change some stuff on time notation but all fine now. Now when someone get’s up in the middle of the night they don’t get blinded by the light. For completeness full code below:

MotionLights:
  module: motionlights
  class: MotionLights
  constrain_start_time: sunset - 00:45:00
  constrain_end_time: "00:00:00"
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer
  entity_off: scene.kitchen_timer_off
  delay: 600

MotionLights_2:
  module: motionlights
  class: MotionLights
  constrain_start_time: "00:00:00"
  constrain_end_time: "06:00:00"
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer_night
  entity_off: scene.kitchen_timer_night_off
  delay: 200

MotionLights_3:
  module: motionlights
  class: MotionLights
  constrain_start_time: "06:00:00"
  constrain_end_time: sunrise + 00:45:00 
  sensor: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor_10_0
  entity_on: scene.kitchen_timer
  entity_off: scene.kitchen_timer_off
  delay: 600

I am pretty new to appDaemon custom modules.

I put the script in the following folder: /conf/apps/motionLights.py

I get the following error when starting appDaemon

2023-08-03 17:12:36.672416 WARNING AppDaemon: No app description found for: /conf/apps/motionLights.py - ignoring

Is there more needed to get this working? or is the example outdated?