Entity Controller Mega Thread (Motion Lighting Controller)

Tags: #<Tag:0x00007f73afd22650>

thats the problem when you want to make a HA component from it.

the flux switch could also have a state active and inactive, but it only has on or off.
you could talk with the devs how they think about it.

or just leave it as a custom component and create 2 switches from it. or 1 input_select.

I feel bad I suggested this :wink:

Perhaps would it be possible to use the HA status to indicate the current status of the LightingSM controlled entity? So if the switch.motion state was on it meant the controlled entities were on, if off they are off.

To get current LightingSM platform state-machine state could be an attribute? Perhaps we need to friendly name the states as well so they make sense to the typical user?

This way from a GUI perspective the on/off toggle would show the controlled entity state which makes sense and logical to the user; and we can use the secondary field to specify the internal state machine state friendly name so the user knows why the controlled entity is on or off.

Hello @danny do you have some instructions on how to deploy this for a current HA system? I already have AD running for HADashboard.

Isn’t that documented in the readme on GitHub?

I subclassed directly off Entity, meaning this component does not belong to a domain, which is an option as well.

Hi Danny,

I noticed you had a 2.0.0 on your repo so I had some time today to give it a early test. I found something when multiple entities are configured.

It seems the control entity from the first LightingSM instance is assumed as the state_entity for all following instances.

If a state_entity is explicitly set for each LightingSM instance that is correctly used.

I think the config parser just references the first instance if it doesn’t see a state_entity.

Below you can see the configuration I used and then some logging to show that for mudroom the light.office_lamp_level was used.

      - binary_sensor.office_auto_lights
      - light.office_lamp_level               
   delay: 600     

      - binary_sensor.garage_man_door_sensor
      - binary_sensor.mudroom_pir_sensor
      - binary_sensor.mudroom_door_sensor
      - light.mudroom
   delay: 300

      - binary_sensor.garage_man_door_sensor
      - binary_sensor.garage_pir_sensor
      - binary_sensor.single_garage_sensor
      - binary_sensor.dual_garage_sensor        
      - light.garage
   delay: 600

2018-12-29 18:32:56 INFO (MainThread) [custom_components.lightingsm] Config Item motion_mudroom: OrderedDict([('sensors', ['binary_sensor.garage_man_door_sensor', 'binary_sensor.mudroom_pir_sensor', 'binary_sensor.mudroom_door_sensor']), ('entities', ['light.mudroom']), ('delay', 300)])
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Init LightingSM with: OrderedDict([('sensors', ['binary_sensor.garage_man_door_sensor', 'binary_sensor.mudroom_pir_sensor', 'binary_sensor.mudroom_door_sensor']), ('entities', ['light.mudroom']), ('delay', 300), ('name', 'motion_mudroom')])
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Name: motion_mudroom
2018-12-29 18:32:56 INFO (MainThread) [custom_components.lightingsm.motion_mudroom] Setting up state entities
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Setting up control entities
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Registering control: light.mudroom
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Using existing state entities: ['light.office_lamp_level']
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Control Entities: ['light.mudroom']
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Sensor Entities: ['binary_sensor.garage_man_door_sensor', 'binary_sensor.mudroom_pir_sensor', 'binary_sensor.mudroom_door_sensor']
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Registering sensor: binary_sensor.garage_man_door_sensor
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Registering sensor: binary_sensor.mudroom_pir_sensor
2018-12-29 18:32:56 DEBUG (MainThread) [custom_components.lightingsm.motion_mudroom] Registering sensor: binary_sensor.mudroom_door_sensor
2018-12-29 18:32:56 INFO (MainThread) [custom_components.lightingsm.motion_mudroom] serivce data set up: OrderedDict([('sensors', ['binary_sensor.garage_man_door_sensor', 'binary_sensor.mudroom_pir_sensor', 'binary_sensor.mudroom_door_sensor']), ('entities', ['light.mudroom']), ('delay', 300), ('name', 'motion_mudroom')])    


2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] Sensor state change: on
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] state: idle
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] matches on
    2018-12-29 18:46:55 INFO (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] <state light.office_lamp_level=off; node_id=70, value_index=0, value_instance=1, value_id=72057595217149953, friendly_name=Office Lamp, supported_features=1, icon=mdi:lamp @ 2018-12-29T18:32:59.290525-05:00>
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom]  * State of light.office_lamp_level is <state light.office_lamp_level=off; node_id=70, value_index=0, value_instance=1, value_id=72057595217149953, friendly_name=Office Lamp, supported_features=1, icon=mdi:lamp @ 2018-12-29T18:32:59.290525-05:00>
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] State entities are OFF.
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] Exiting idle
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] (night mode disabled): None
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] Using DAY MODE parameters: {'delay': 300, 'service_data': None}
    2018-12-29 18:46:55 INFO (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] {'delay': 300, 'service_data': None}
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] Delay: 300
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] light params before turning on: {'delay': 300, 'service_data': None}
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] Turning on light.mudroom (no parameters passed to service call)
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] state: active_timer
    2018-12-29 18:46:55 DEBUG (SyncWorker_0) [custom_components.lightingsm.motion_mudroom] state: active_timer
    2018-12-29 18:46:56 DEBUG (SyncWorker_18) [custom_components.lightingsm.motion_mudroom] False

Hi, yes the same thing happened before. For some reason when a new object is created in python, it contains some values assigned to the previous object… need to explicitly overwrite all values.
Will push a fix tonight.

@jwelter Can you please download and test this version.
The only change is this line

I am really at a loss here because I do not understand how one Python object can contain the values of a previously initialised object. If someone can take a look atthe code, I would appreciate it. As soon as multiple entites are created, it all breaks. It’s very odd.
Entity objects are initialised on this line of code

Seems related to this question on StackOverflow. This helped me solve @jwelter defect for state entities.

Does not explain why they all get the same entity name though.

Solved Apparently… if values are not reassigned in constructor they are shared among object instances. crazy stuff… did my head in.

Thanks! I have started to comment in the GitHub instead if you are ok with that?

This problem seems solved in 2.2.1 and I am testing that now on some “wife critical” lights :wink:

I hope you have custom_updater setup. just fixed major defect in v2.2.2 related to constrain times

1 Like

I have an app based on stock motion_light app which I have been testing for last few months. Works for the most part.


What doesn’t work?

The way app works is:
Outer motion detected > door opened > internal motion detected = light on for 10 min
if door closed and another internal motion detected > keep 10 min else timer reset to 60 sec.
There is a bug in the logic somewhere so that the timer is not reset some of the time, I am too lazy to go through the app to debug.

Can you raise an issue for this and include your configuration?

I think we need to have a class to describe the room in a house with properties like doors, lights, fans and occupancy to properly control lights, fans etc. I tried to make one but failed due to my poor programming skills.
Anyway my reasoning for the app goes like this:

If the room has only one door(Toilet does):
    >> If motion detected inside
               >> Room must be occupied( Unless the previous state was unoccupied and the door has not opened recently: false positive)    
                     >> If the room is occupied light must be on(With conditions) until the door opened again(to let the occupant out)

I will raise an issue if you like to further discuss.

I meant raise an issue for the bug you found.

This is completely unrelated functionality…

This would only apply to toilets. Any other room you could have multiple people entering

i think you look at it from the wrong direction. without creating special apps (and quite some programming) your way should be this:

create a timer

and 3 simple automations like

  door is opened 
  lights are off 
  start a timer

  door is opened 
  lights are on 
  turn lights off

  motion detected 
  timer is on and lights are off
  turn lights on
reset timer

now the lights will only go on if the door has been opened and motion is detected within the timer time
and the lights will only go off if the door is opened again


this is GREAT @danny

Maybe I’m misunderstanding something, but when I add the example from the doc in my configuration file, I get this error in my log:

Failed config
  General Errors: 
    - Component not found: lightingsm

is there something I need to do to add this component to Home Assistant? From reading the doc it sounded like the component should be available.