Error rendering data template: UndefinedError: 'dict object' has no attribute 'from_state'

Any ideas why this template automation is throwing the error:

Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘from_state’

trigger:
  - platform: zone
    entity_id: person.wife
    zone: zone.home
    event: leave
  - platform: zone
    entity_id: person.wife
    zone: zone.home
    event: enter
condition: []
action:
  - service: notify.husband
    data:
      message: |
        {% if trigger.event == "leave" %}
          {{ trigger.from_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.from_state.attributes.friendly_name }} arrived {{ trigger.zone.attributes.friendly_name }}
        {% endif %}
mode: single

An event has no state.
Trigger on the person state (home / not_home)

You also don’t need this part as the triggers are both for person.wife, so just write the name of the wife.

Understood but I’m trying to build a scalable automation where I can add other members of the household. The plan was to add other members of the household to the zone leave/enter list however I can now see that isn’t going to work as I’ll need to trigger off the person state, not zone event.

Many thanks for the pointer, my understanding isn’t quite there as I’m still getting the same error with my revised code as follows:

trigger:
  - platform: zone
    entity_id: person.wife
    zone: zone.home
    event: leave
  - platform: zone
    entity_id: person.wife
    zone: zone.home
    event: enter
  - platform: state
    entity_id: person.wife
    from: home
    to: not_home
condition: []
action:
  - service: notify.husband
    data:
      message: |
        {% if trigger.event == "not_home" %}
          {{ trigger.from_state.attributes.friendly_name }} left {{ trigger.zone.attributes.friendly_name }}
        {% else %}
          {{ trigger.from_state.attributes.friendly_name }} arrived {{ trigger.zone.attributes.friendly_name }}
        {% endif %}
mode: single
max: 10

Remove the zone triggers, not needed.
trigger.event is to be replaced by trigger.to_state

Error still persisting:

Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’

Current code:

trigger:
  - platform: state
    entity_id: person.wife
    from: home
    to: not_home
condition: []
action:
  - service: notify.husband
    data:
      message: |
        {% if trigger.to_state == "not_home" %}
          {{ trigger.to_state.attributes.friendly_name }} left home
        {% endif %}
mode: single
max: 10

Your code is more complicated than it should.

Try just:

trigger:
  - platform: state
    entity_id: person.wife
    to: not_home
condition: []
action:
  - service: notify.husband
    data:
      message: "{{ trigger.to_state.attributes.friendly_name }} left home"
mode: single

If you want to combine arrive at and left home, you can try- (have not tried it myself)

trigger:
  - platform: state
    entity_id: person.wife
condition: []
action:
  - service: notify.husband
    data:
      message: >-
        {% if is_state('person.wife', 'not_home') %}
          Wife left home
        {% elif is_state('person.wife', 'home') %}
          Wife arrived at home
        {% else %}
          Wife is located at {{ states('person.wife') }}
        {% endif %}
mode: single

The above code assumes-
1. If person.wife goes to undefined zone, the states will be not_home
2. If person.wife goes to defined zone, the states will be defined zone - such as Work

A flaw exist in the logic. Please read Burningstone’s post below.

1 Like

There’s a flaw in your logic.
If his wife leaves a zone other than home (e.g. work), your automation will show “Wife left home”, which is of course not true.

It should be

trigger.to_state.state == "not_home"

Also the friendly name part can be simplified to

trigger.to_state.name

This will get the friendly name and in case the friendly name doesn’t exist it falls back to the entity_id, see here for more details about the state object → State Objects - Home Assistant

Try this:

trigger:
  - platform: state
    entity_id: person.wife
action:
  - variables:
      old: "{{ trigger.from_state.state }}"
      new: "{{ trigger.to_state.state }}"
      person: "{{ trigger.to_state.name }}"
  - service: notify.husband
    data:
      message: >
        {% if new == "not_home" and old == "home"%}
          {{ person }} left home
        {% elif new == "home" %}
          {{ person }} arrived at home
        {% elif new == "not_home" %}
          {{ person }} left {{ old }}
        {% else %}
          {{ person }} arrived at {{ new }}
        {% endif %}
mode: single

This can be extended with as many persons as you like, by adding additional triggers. This should also cover all the possible cases, arriving at home, arriving at a zone other than home, leaving home and leaving a zone other than home.

Thanks for pointing out the logic flaw and showing us how to do it properly :slight_smile:

@Cergon, if you need to setup a zone entering/leaving notification, you can take a look at this automation by Burningstone.

1 Like

Thanks everyone for your help on this, I’ve learnt a lot. @Burningstone thanks for the revised code which I tested yesterday. The code fits my use case perfectly. One issue I have found is after wife leaves ‘work’ and prior to arriving ‘home’ I am receiving continuous messages stating ‘wife arrived at not_home’. These recur circa twice a minute thus by the time wife was home I had about 30 message notifications on the mobile app.

Any ideas?

That’s probably the sensor getting a new GPS measurement, that is not home or in any other zone, the state gets set again to not_home, even though it is already in this state. You can add a condition to overcome this issue:

condition:
  - "{{ trigger.to_state.state != trigger.from_state.state }}"
action:
.....

Thanks, that works a treat.

For those interested my initial problem was also caused by user error. I was using ‘run actions’ from the automations window to test the automation. This of course was leading to the error as there was no state change on the person entity. The correct way to test this automation is via ‘Developer Tools > States’ and manually changing the person entity state. Hopeful that helps others.

2 Likes

I registered to THANK YOU for this comment!

Thought I was driving crazy as it worked out before and then suddenly didn’t anymore. Just because I used the „run action“ instead of physically triggering a sensor.

Hi, can you share your full code? thanks

hi,

I have this automation and work perfectly until the last update…

- alias: 'Notify if door/window opened for 4 minutes'
    trigger:
      - platform: state
        entity_id: group.sensori_porte_finestre
        to: "on"
        for:
         hours: 0
         minutes: 4
         seconds: 0
    action:
      - repeat:
          count: 6
          sequence:
            - service: notify.alexa_media
              data_template:
                data:
                  type: tts
                target: 
                  - media_player.echo_dot_sala
                message: >-
                    {% for entity in trigger.to_state.attributes.entity_id %}
                      {% if states(entity) == 'on' %}
                        {{ state_attr(entity, 'friendly_name') }} è aperta
                      {% endif %}
                    {% endfor %}
            # Give it time to complete
            - delay:
                hours: 0
                minutes: 4
                seconds: 0
    mode: single

now receive this error : Error rendering
data template: UndefinedError: ‘dict object’ has no attribute ‘to_state’.
Anyone know why?

How to debug my automation? It is possible to know which methods or properties has “trigger” or any other object present in my automation ?