Automation problem lights on sunset

Hello all,

I have a problem with an automation which has 2 usecases:

  • Trigger light on when i’m home after sunset.
  • Trigger lights on when i’m coming home after sunset.

I have a template condition set that demands that the last_changes state is not changed for the last 15 minutes.
This is to prevent false triggers (i use netgear tracking and once in a while it says i’m not home when i actually am and otherwise).

Now the first usecase is working, but the second one is not.
The template condition is returning false and I think this is because i’m coming home and the person state changes, and then the template condition uses that as last_changed, so that’s well within the 15 minutes offcourse, so it doesn’t get triggered.

I think what i want is a template condition that says that my previous state was in that state for 15 minutes, but I don’t know how to do that.

Any help is really appreciated.
This is my automation code:

id: lightsOnSunset15
alias: Lights on 15 minutes before sunset
trigger:
  - platform: state
    entity_id: person.owner
    to: home
  - platform: sun
    event: sunset
    offset: '-00:15:00'
condition:
  - condition: state
    entity_id: person.owner
    state: home
  - condition: template
    value_template: >-
      {{ as_timestamp(states.person.owner.last_changed) + 900 < as_timestamp(now()) }}
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:15:00'
      - condition: sun
        before: sunrise
        before_offset: '+01:00:00'
action:
  - service: homeassistant.turn_on
    entity_id: light.light

Just to clear a doubt, the second use case is to turn the lights on after you have arrived home…right?

If that is the case, you can use the choose type action like the below

alias: Lights on 15 minutes before sunset
trigger:
  - platform: state
    entity_id: person.owner
    to: home
  - platform: sun
    event: sunset
    offset: '-00:15:00'
condition:
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '-00:15:00'
      - condition: sun
        before: sunrise
        before_offset: '+01:00:00'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: person.owner
            state: home
        sequence:
          - service: homeassistant.turn_on
            entity_id: light.light
      - conditions:
          - condition: state
            entity_id: person.owner
            state: not_home
        sequence:
          - wait_for_trigger:
              - platform: state
                entity_id: person.person
                id: home
                to: home
          - condition: or
            conditions:
              - condition: sun
                after: sunset
                after_offset: '-00:15:00'
              - condition: sun
                before: sunrise
                before_offset: '+01:00:00'
          - service: homeassistant.turn_on
            entity_id: light.light
    default: []

Hi sheminasalam,

Thanks for your reaction.

To be exact, the second usecase is when i’m arriving home and my netgear tracker “sees” me, and it is past sunset or before sunrise(with some offsets).

I will try and test your solution, thanks…