Arriving home , automations not working correctly

Hello

I’m trying to get my outside light on when I’m arriving to home and sun is below horizon.
This one code below works ok otherwise but for some reason mobile phone location isn’t so accurate that when I’m arriving home it shows few kilometres earlier that I’m at home and then away ( even my radius is 100m ). This makes my lights going on and off and normally those are off when I’m at my yard.
I read somewhere that I could set trigger e.g. for 15 minutes from first state change.
I tried second code but it’s not working, thought it’s not giving me any errors.
Could someone advise what I’m doing wrong ?

- alias: 'Talli_ulkovalo_saapuminen'
    trigger:
      platform: zone
      entity_id: device_tracker.teemu_teemus9
      zone: zone.home
      event: enter
    condition:
      condition: state
      entity_id: sun.sun
      state: below_horizon
    action:
      - service: switch.turn_on
        entity_id: switch.talli_ulkovalo
      - delay: '00:15:00'
      - service: switch.turn_off
        entity_id: switch.talli_ulkovalo
- alias: 'Talli_ulkovalo_saapuminen'
    trigger:
      platform: state
      entity_id: device_tracker.teemu_teemus9
      from: 'away'
      to: 'home'
      for:
        minutes: 15
    condition:
      condition: state
      entity_id: sun.sun
      state: below_horizon
    action:
      - service: switch.turn_on
        entity_id: switch.talli_ulkovalo
      - delay: '00:15:00'
      - service: switch.turn_off
        entity_id: switch.talli_ulkovalo

I think the issue may be that your device tracker is not in state “away” when you are not home, but rather showing the zone you are in as a state. Can you check the state of the device tracker under Developer Tools -> States when you are not home.

Wouldn’t be away anyway, would be not_home

For testing I changed my motion sensor for trigger.

Using this upper code, works just like should be.
But lower code doesn’t do anything…

- alias: 'Talli_ulkovalo_saapuminen'
    trigger:
      platform: state
      entity_id: binary_sensor.liike_kuisti
      from: 'off'
      to: 'on'
    condition:
      condition: state
      entity_id: sun.sun
      state: below_horizon
    action:
      - service: switch.turn_on
        entity_id: switch.talli_ulkovalo
      - delay: '00:06:00'
      - service: switch.turn_off
        entity_id: switch.talli_ulkovalo
- alias: 'Talli_ulkovalo_saapuminen'
    trigger:
      platform: state
      entity_id: binary_sensor.liike_kuisti
      from: 'off'
      to: 'on'
      for:
        minutes: 5
    condition:
      condition: state
      entity_id: sun.sun
      state: below_horizon
    action:
      - service: switch.turn_on
        entity_id: switch.talli_ulkovalo
      - delay: '00:06:00'
      - service: switch.turn_off
        entity_id: switch.talli_ulkovalo

Just realised that this is not what I’m searching. This will trigger my lights on AFTER specified minutes when I’m at home. My plan was get trigger immediately for 15min when I enter home zone. Even I would be out of home zone some minutes and arrive again ( when I’m really at home ).
Maybe timer is solution for my problem, I need to dig more for correct solution.

And here’s solution, needed to use template.

- alias: 'Talli_ulkovalo_saapuminen'
    trigger:
      platform: state
      entity_id: device_tracker.teemu_teemus9
      to: "home"
    condition:
      - condition: state
        entity_id: sun.sun
        state: below_horizon
      - condition: template
        value_template: '{{ (now()-states.automation.talli_ulkovalo_saapuminen.attributes.last_triggered).seconds > 960 }}'
    action:
      - service: switch.turn_on
        entity_id: switch.talli_ulkovalo
      - delay: '00:15:00'
      - service: switch.turn_off
        entity_id: switch.talli_ulkovalo

This makes run action only once in 960 seconds. Works very well!

Is there changes in HA because this stopped working ???
I’m nowadays using HA Core 2021.2.3.

It just not trigger even everything looks okay.