Automation for Motion / Outside Lights not working

I have a pair of Hue lamps in the porch, along with a Hue Motion Sensor. I know the hardware works (under HomeBridge / Home.app, now disabled), but want to bring it over to HA.

This is my automations.yaml - but it doesn’t work & the lights don’t come on, even when pitch dark. Can anyone see an error? I want them to be active SS > SR, and come on at 50% brightness for 5 minutes when triggered (then off).

TIA

- id: '1603739924880'
  alias: Porch PIR Lights
  description: ''
  trigger:
  - platform: device
    domain: binary_sensor
    entity_id: binary_sensor.porch_pir_motion
    device_id: 77082c79153b11ebaa71777cd724b293
    type: motion
    for:
      hours: 0
      minutes: 5
      seconds: 0
  condition:
  - condition: sun
    before: sunrise
    after: sunset
  action:
  - type: turn_on
    device_id: 77220237153b11eb9a25c9dc83082952
    entity_id: light.porch_1
    domain: light
    brightness_pct: 50
  - domain: light
    entity_id: light.porch_2
    device_id: 77223d18153b11ebac15430aac7b86c5
    type: turn_on
    brightness_pct: 50
  mode: single

Yeah, your condition always returns false because it cannot possibly be after sunset and before sunrise at the same time.

Not to mention that your trigger requires 5 minutes of continuous motion before it fires the automation, which will never happen as the sensors don’t work like that.

1 Like

You need to put a delay in your action instead of the for in your trigger. Currently your automation reads like this:

When motion sensor is on for 5 minutes, turn the lights on. However, the motion sensor will only be on for a short period of time, then it will reset back to off, therefore your automation won’t trigger. It’ll only trigger if you are in front of the motion sensor for the whole 5 minutes so that it always detects motion.

Also what Marc pointed out already, you need two separate conditions for sunrise/sunset:

Another thing, why not put the two lights in a light group and turn on the group instead of the individual bulbs? Even better would be creating groups in the Hue app and import these groups into HA.

Thanks both; other than grouping the lights in to a “pair” which I will do later, to save me waiting until this evening to test it how does this read? Does swapping the order of after sunset / before sunset work, or will it still produce a conflict?

TIA

- id: '1603739924880'
  alias: Porch PIR Lights
  description: ''
  trigger:
  - platform: device
    domain: binary_sensor
    entity_id: binary_sensor.porch_pir_motion
    device_id: 77082c79153b11ebaa71777cd724b293
    type: motion
  condition:
  - condition: sun
    after: sunset
    before: sunrise
  action:
  - type: turn_on
    device_id: 77220237153b11eb9a25c9dc83082952
    entity_id: light.porch_1
    domain: light
    brightness_pct: 50
  - delay 00:05:00
  - domain: light
    entity_id: light.porch_2
    device_id: 77223d18153b11ebac15430aac7b86c5
    type: turn_on
    brightness_pct: 50
  - delay 00:05:00
  mode: single

Or should it be

 condition:
  - condition: sun
    after: sunset
 condition:
  - condition: sun
    before: sunrise

Neither of those will work.

You first example has the same problem as earlier in that it can never be after sunset and before sunrise at the same time.
From HA’s perspective after sunset is anytime between sunset and midnight, while before sunrise is anytime between midnight and sunrise.

Your second example is closer but the syntax is wrong and the default for multiple conditions is AND when you want OR.

Have a quicker read here.
You specifically want the OR condition

Remove the second delay, it does nothing.

Conditions should look like this:

condition:
  condition: or
  conditions:
    - platform: sun
      before: sunrise
    - platform: sun
      after: sunset

There’s even exactly this template in the docs for the sun condition.

Needs to be an OR condition, which looks like this…

condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise

Lol - ninja’d

I don’t think the delays are correct anyway - do you really want to turn on one light, wait 5 minutes and then turn on another?

I suspect you want both lights to come on at the same time, and then go off after 5 minutes?

OK, quick rethink of the logic based on my guess that what you’re actually trying to achieve is that when motion is detected the lights go on, and that they stay on for 5 minutes after the last motion is detected.

Then applying that logic to an automation using states and services, instead of those God awful device triggers and actions that the ui produces, so that we can template the on and off into one automation, here’s how I would do it…

- id: '1603739924880'
  alias: Porch PIR Lights
  trigger:
    - platform: state
      entity_id: binary_sensor.porch_pir_motion
      to: 'on' 
    - platform: state
      entity_id: binary_sensor.porch_pir_motion
      to: 'off'
      for:
        minutes: 5 
  condition:
    condition: state 
    entity_id: sun.sun
    state: 'below_horizon' 
  action:
    service: "light.turn_{{ trigger.to_state.state }}" 
    entity_id:
      - light.porch_1
      - light.porch_2
1 Like

Just to help me be clear and begin to understand the way the logic works, if (as you correctly said) I want the lights to be triggered by movement during SS>SR (or below horizon is fine), then stay on for 5 mins after the last triggering, then switch off …

Should the

for:
  minutes: 5

…not be between the two state sections, rather than after both as shown above?

In my warped logic, as shown the lights would come on, then stay off for 5 minutes :crazy_face:

The for: belongs to the second trigger.

There are two triggers, the first is whenever motion is detected (to: 'on'), the second is when no motion is detected for 5 minutes (to: 'off').

The template in the service: parts determines, whether the lights should be turned on or off.

Yeah, to expand on @Burningstone 's reply. The logic is thus:

  • Motion sensor turns to on - automation triggers immediately, checks (condition) that sun is below horizon and turns the lights on (state of the motion sensor that caused the trigger)

  • Motion sensor turns to off and remains off for 5 minutes, automation triggers at the end of the 5 minutes, checks (condition) that sun is below horizon and turns the lights off (state of the motion sensor that caused the trigger)

The only potential pitfall here is if the lights are on at the moment the sun goes above the horizon, as the condition will then prevent the turn off part, but I would expect that conditioning around this is overkill. Of course if you find this does cause a problem we can use a template condition that checks the trigger and always allows the ‘off’ to pass, and only applies the sun condition to the ‘on’ trigger.

Edit - for completeness…

- id: '1603739924880'
  alias: Porch PIR Lights
  trigger:
    - platform: state
      entity_id: binary_sensor.porch_pir_motion
      to: 'on' 
    - platform: state
      entity_id: binary_sensor.porch_pir_motion
      to: 'off'
      for:
        minutes: 5 
  condition:
    - "{{ trigger.to_state.state == 'off' or is_state('sun.sun', 'below_horizon') }}" 
  action:
    service: "light.turn_{{ trigger.to_state.state }}" 
    entity_id:
      - light.porch_1
      - light.porch_2
1 Like

Thank you both very much - really appreciated. I have made the changes and will test it tonight.