Automation condition error

Hello,

I have a trigger with a template condition which fails sometimes and I have no clue why.
The template condition is for preventing false triggers by demanding that the previous state was active for at least 25 minutes
Sometimes it works but sometimes I get the following error:
Error: In ‘template’ condition: UndefinedError: ‘dict object’ has no attribute ‘from_state’

This occurs mostly after a long time home and then the automation triggers at sunset.

My complete automation:

alias: Lights on
trigger:
  - platform: state
    entity_id: person.owner
    to: home
  - platform: sun
    event: sunset
    offset: '+00:05:00'
condition:
  - condition: state
    entity_id: person.owner
    state: home
  - condition: template
    value_template: >-
      {{ (now().timestamp() - trigger.from_state.last_changed.timestamp()) >
      1500 }}
  - condition: or
    conditions:
      - condition: sun
        after: sunset
        after_offset: '+00:05:00'
      - condition: sun
        before: sunrise
        before_offset: '+01:00:00'
action:
  - service: homeassistant.turn_on
    entity_id:
      - light.lamp
    data:
      transition: 10

Hope there’s a smart person around here somewhere😉.

Thank you!

trigger.from_state is only valid for a state trigger, and you also have a sun trigger. My guess is that you’re seeing the error when the sun trigger causes the automation to run. You can make a more complex condition or use a choose: to allow more flexibility, but I’d just make a separate automation for each trigger type.

I’m fairly sure (can’t check right now) that when sunset is your trigger, there is no from state. You can just put a for: on the person condition.

Thank you guys for replying.
No state when the sun triggers sounds like the issue indeed.
I tried the “for” option before but that did not work.
I use Netgear presence for detecting when I’m coming home. Now sometimes it happens that in the middle of the night my home/not home status changes every 5 a 25 minutes for a while and then my lights go on downstairs.
That’s why I was trying to prevent this with the template condition.
Any other ideas to prevent my issue is highly appreciated!

Use the sun elevation attribute as a state trigger rather than using the events with offset.
e.g.

  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -20