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).
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.
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?
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
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
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
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