Do automation if "stuff" changes

Hi

Im wondering if i can make a automation for my PIR sensor.

Today i have a automation that "if motion = on " and “Sun = sunset” = turn on lamp
If no motion in 20 min = turn off lamp

But if im in the room before sunset, then the PIR is “on”, and when sunset happend the lamp does not turn on. Can i get the lamp to turn on if motion in my room and sunet is happening?

I think you can put below horizon or maybe check the angle, that way it should trigger anyway

How would i do that?

   condition:
     - condition: state
       entity_id: sun.sun
       state: 'below_horizon'

I use this

automation front_motion_lights:
  alias: Front motion lights in dark
  trigger:
    platform: state
    entity_id: binary_sensor.outside_motion
    state: 'on'
  condition:
    condition: or
    conditions:
      - condition: sun
        before: sunrise
      - condition: sun
        after: sunset
  action:
    service: switch.turn_on
    entity_id: switch.outside_lights

In your situation, the “trigger” would be PIR sensor turning to ‘on’ and the condition would be after sunset. Below is an example of the automation:

- alias: Turn on Lights with Motion after sunset
  trigger:
    platform: state
    entity_id: sensor.front_porch_sensor
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
  action:
    - service: switch.turn_on
      entity_id: switch.front_porch_light_switch_33_0

To turn it off, you would have a second automation that checks for no motion after 20 minutes:

- alias: Turn off Lights with No Motion after 20 minutes after Sunset
  trigger:
    platform: state
    entity_id: sensor.front_porch_sensor
    to: 'off'
    for:
      minutes: 20
  condition:
    condition: and
    conditions:
      - condition: sun
        after: sunset
  action:
    - service: light.turn_off
      entity_id: switch.front_porch_light_switch_33_0

I have preferred using a template condition for sun angle. Sometimes it is dark in my house at sunset and I really want lights on before the sun is actually below the horizon.

- condition: template
  value_template: "{{ states.sun.sun.attributes.elevation  | int < 10 }}"

The above works well and if your trigger is your PIR sensor once the sun is below an angle of 10 degrees (or whatever you choose) it will kick on the light.

Yea, i have a automation for that, but would it work if the PIR is allready at “On” and then “sunset” happends?

Isn’t the pir momentarily? Anyway, if not, then you could do a second automation with sunset being the trigger, and pirstate=on the condition.

Well i dont know, it does not turn on the light if there is motion and was before sunset.
I guess i have to make another automation for that