Switch on lights when motion sensor is on, motion sensor detected and luminosity is low

Hi

At the moment, i have the mechanism to switch on lights when motion is detected controlled with the hue app. This is based on the hour of the day, and it is a pain as sometimes it is very dark at 9am for example

I am looking to build an automation in home assistant to switch on the lights when:

  • Motion is detected
  • Sensor is on (There are some scenarios where i want to deactivate the motion sensor, as when having a bath)
  • Luminosity level is below 3

I have written something very simple, like this

- id: Bano_Javi_lights_on_low_ilumination
  alias: Bano_Javi_lights_on_low_ilumination
  initial_state: false
  mode: restart
  trigger:
    platform: state
    entity_id: binary_sensor.hue_motion_sensor_javi_motion
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.hue_motion_sensor_javi_motion
        state: 'on'
      - condition: numeric_state
        entity_id: sensor.hue_motion_sensor_javi_light_level
        below: 3
  action:
    - service: light.turn_on
      data:
        entity_id: group.bano_javi

But clearly there is something wrong there. I have the same code for checking if the motion sensor is on, and to detect motion,

entity_id: binary_sensor.hue_motion_sensor_javi_motion
        state: 'on'

I think this just checks if there is motion detected, and i need to add this to check if the sensor is on:

entity_id: switch.motion_sensor_javi
state: ā€˜onā€™

Is that correct?

Thanks!

Have you looked at the HACS integration Entity Controller? Itā€™s designed to do this sort of thing and can reduce quite complex automations to a few lines. For example, my yard light:

  - yard_light:                               
      sensor: 
      - binary_sensor.yard_motion_sensor_motion      # motion sensor
      entities: 
      - light.yard_1        # light
      delay: 180        # time light stays on
      overrides: 
      - input_boolean.yard_movement_detector      # switch to turn off sensor at particular light level
      - input_boolean.override_yard_motion_sensor    # switch to turn off sensor entirely

Yep, i saw that, it was my second option, but just for the sake of understanding how to do it manually, i want to build that script on my own :slight_smile:

Any ideas on how to do it?

Thanks

Itā€™s been a while since I switched to Entity Controller, but as I remember I was already using input_booleans to control the motion sensor, with those being turned on and off elsewhere.

Much easier to maintain than one large chunk of yaml (though I imagine purists would find it inelegant :roll_eyes:).

1 Like

does that entity exist already in your system?

Yes, thatā€™s the one that enables/disables the motion sensor

Probably I will end using this just to avoid reinventing the wheel but itā€™s personal now to make it work

Have you tried this?:

  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.hue_motion_sensor_javi_motion
        state: 'on'
      - condition: numeric_state
        entity_id: sensor.hue_motion_sensor_javi_light_level
        below: 3
      - condition: state
        entity_id: switch.motion_sensor_javi 
        state: 'on'

and since all of your conditions are ā€˜andā€™ you donā€™t need to specify it because ā€˜andā€™ is default:

  condition:
    - condition: state
      entity_id: binary_sensor.hue_motion_sensor_javi_motion
      state: 'on'
    - condition: numeric_state
      entity_id: sensor.hue_motion_sensor_javi_light_level
      below: 3
    - condition: state
      entity_id: switch.motion_sensor_javi 
      state: 'on'

Yep, the problem is the trigger, I have debugged it and the trigger is not actioned.

trigger:
platform: state
entity_id: binary_sensor.hue_motion_sensor_javi_motion
to: ā€˜onā€™

Thatā€™s why I donā€™t know if this code is meant to trigger the conditions when there is motion, as it is not doing it

the trigger doesnā€™t interact with the conditions at all so Iā€™m not sure what you mean.

once the automation trigger is satisfied then it checks that the conditions are all true and if yes then it runs the actions.

1 Like

Have you tried your trigger with a ā€œfromā€ state?

trigger:
  platform: state
  entity_id: binary_sensor.hue_motion_sensor_javi_motion
  from: 'off'
  to: 'on'

Hi - I am a newbie here and this forum was such a great help. I have a motion sensor automation related question that I could not figure out.

I wanted to set up two automations for each rooms: turn on the lights when motion is detected (and when it is dark outside) and turn off the lights when there is no motion. I thought this was pretty basic, butā€¦

Now while it seems easy, I found myself in a pretty funny situation. The lights turn on, but then they turn off and the funniest part is that then they get triggered and got stuck in a loop.

My guess is that my motion and/or occupancy sensors (Aqara P1 and FP2) do detect the motion, but if I stay stationary, like sitting at my desk or chilling on my couch, they stop detecting it, thus activate the turn off automation.

The problem is that:

  • A: it would be ideal for it to check if Iā€™m still in the room, and
  • B: I donā€™t understand that when I am not there and they do turn off, the motion sensor still might think that it was a motion so it triggers the turn on automation, which is not ideal :smiley:

Any help and suggestions would be a great help. Thanks in advance,
p