Templating Question

I’m running on 0.57.2. I’m experiencing an issue with templating that I can’t seem to figure out. I’m receiving the following error message when I start up HA:

homeassistant.exceptions.TemplateError: UndefinedError: 'dict object' has no attribute 'state'

automations/automations_lights_on_at_sunset.yaml

alias: "lights_on_at_sunset"
trigger:
  - platform: sun
    event: sunset
    offset: "-01:00:00"
condition:
  condition: template
  value_template: "{% if states('device_tracker.iphone1') == 'home' or states('device_tracker.iphone2') == 'home' %}true{% else %}false{% endif %}"
action:
  - service: notify.ios_iphone
    data_template:
      message: >
        {{ trigger.state.name }} is at home at sunset! Turning on interior lights!

What am I doing wrong? I’ve tried {{ trigger.state.name }}, {{ trigger.to_state.attributes.friendly_name }} and {{ trigger.state.attributes.friendly_name }} with no luck.

I ran into this too. for me I was using {{ trigger.state }} in an automation under a condition template, but one of the two triggers was the homeassistant start event which doesn’t have a “state” so it threw an error, but it still worked. I’m guessing the sun platform as the trigger is what is throwing the error.

That makes sense. Any idea on how I can work around this to get the template to show the name of the device, but without throwing the startup error? I’m working through cleaning up my automations so that they are nice and clean.