Help with a complex automation for an Aqara Motion Sensor

Hi there,

I’m using Hassio now for a few months and I got most of my automations up and running, but now I’m struggling with a bit more complex configuration.
What I want to achieve:

  • If the light meter of the Aqara motion sensor measures less then 10 Lux and motion is detected -> turn on the light at 100% brightness for 1 minute

  • If the light meter of the Aqara motion sensor measures less then 10 Lux and the time is later than 5 pm -> turn on light at 10% brightness untill 11:30 pm

  • If the light is on at 10% brightness and motion is detected, turn on the light to 100% brightness for 1 minute and fall back to the 10% brightness if the time is below 11:30 pm

Maybe it doesn’t seems to be so complex, and I am able to create scripts for separate parts, but I fail combining them into 1 single script.
Maybe the next flowchart explains it better:

I hope someone can help me :slightly_smiling_face:

I don’t think that’s going to work. if your flowchart is what you want to implement, there are a couple of flaws: If time is >17:00 and < 23:00 you’ll have 2 infinite loops that would technically bring your PC down to a grind:
image

  • While motion is not detected, you’ll keep switching your light to 10% (Loop A)
  • While motion is not detected, you’ll keep switching your light to 100% wait a minute and do it all over again (Loop B)

Also do you really want your light to come on if there is no motion? You might have your reasons but it sounds like a waste to me.

If you really wanted it like the above, it should probably look at 2 automations:

Automation #1

  • Trigger:
    • Light < 10 Lux
    • motion change (on or off)
  • conditions:
    • light < 10 lux
    • 17:00 > time > 23:30
  • action:
    • service template: if motion=on, set light brightness to 100%, else 10%
    • wait 1 min
    • set light brightness to 10%

Automation #2

  • trigger:
    • time = 23:30
  • action:
    • turn light off

You probably also want to set a time trigger of time = 17:00 to your first automation: if lux < 10 before 5 PM, your light will not turn to 10% until first motion detection

Thanks for your reply!
You are probably right about the loop, I think the flowchart doesn’t exactly shows how the yaml instructions work. Instead of “keep on switching the light” I meant “keep the light on”. But looking at your example, I think you understand the idea :slight_smile:

btw, yes I want the light on at 10% even if there is no motion, it is more to simulate that someone is at home. (probably it is nicer to do something with a presence detector, but I haven’t set that up yet)

Anyway I will try your suggestion
thanks!

1 Like

So as an outside observer to this conversation, it seems you think more in a flow chart type of way than code. I think you should look into node-red if you’re struggling with templates and the built in automations. I’ve never used it, but every time I see people talk about it, it’s just images of flow charts.