Light switching with motion sensor and sunset

I have an automation where a (group of) lights switch on on sunset.
Also, there is an automation when a motion sensor does not sense motion for 30 minutes, the light turns off.
Works fine.

However, if I am not at home, the light goes on at sunset, but the motion sensor never triggers. The lights stay on all night.

How can I add a condition to the sunset condition that the light only switches on if there has been a motiondetection in the past 30 minutes?

Wouldn’t it be easier to just add a condition to only turn on the lights at sunset if somone is home?

Example from the docs
but i agree with @Burningstone
person presence would be more logical. As your not relying on a motion sensor having been or not having been triggered. + there’s usually more than one of you in the person presence.

condition:
  condition: state
  entity_id: device_tracker.paulus
  state: 'not_home'
  # optional: trigger only if state was this for last X time.
  for:
    hours: 1
    minutes: 10
    seconds: 5

Ah, yes, sure. Used Unifi for presence detection (wifi) but it resulted in lights switching off and on when my phone connects from 2.4Ghz to 5Ghz and the other way around. Used Owntracks, but is does not work reliable and often too slow; already home for half an hour but Owntracks thinks I’m still 50km away.

I’ve been googling for a reliable presence detection but have not (yet) found it. The best way would be to see if a hwaddr/mac is available on the net regardsless the connection.

Therefore I restarted using motion detection

I use small BLE tags and multiple PI Zero W around the house for presence detection (and even room detection) and it worked flawless in the last 2 years.

I would really suggest to figure out reliable presence detection for your system, as it is one of the core things of home automation in my opinion.

If you still want to go the motion detection way, I would suggest adding template conditions to your sunset automation, which check if the motion sensors “last changed” attribute is < 30 minutes.

I use it so, that it triggers by motion, but has condition with sun set. Now it turns on when there is movement and it’s one hour to sunset.

Your before and after need to be swapped

Trigger by motion and sunset as condition may work.

@Burningstone, what BLE tags do you use?

NUT mini, need to change battery twice a year.

As a solution to your posted topic using the presence condtion would be more wise.
Regarding using presence for triggering automations. Well only if you have it configure and implemented correctly.