Template variable error: 'None' has no attribute 'attributes' when rendering

Hey guys, i have the following automation:

alias: Feuer erkannt
description: ''
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == ''smoke'' }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state == ''on'' }}'
action:
...

It works but sometimes throws the following error in the log:

2022-03-14 08:50:16 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{{ trigger.event.data.new_state.attributes.device_class == 'smoke' }}'

Could i solve this with a change on the first condition to:

value_template: '{{ (trigger.event.data.new_state is defined) and (trigger.event.data.new_state.attributes.device_class == ''smoke'') }}'

What do you think, how i can solve this?
thx

Mind the quotes and double quotes with your value_tempates. According to YAML Style Guide | Home Assistant Developer Docs says use " outside and ' inside.

condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == ''smoke'' }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state == ''on'' }}'

should read:

condition:
  - condition: template
    value_template: "{{ trigger.event.data.new_state.attributes.device_class == 'smoke' }}"
  - condition: template
    value_template: "{{ trigger.event.data.new_state.state == 'on' }}"

Thx for your answer.
I think that’s not the issue because it’s working with the style i wrote it and if i use your code or the GUI,
on save the yaml get’s changed to the style from OP:

condition:
  - condition: template
    value_template: '{{ trigger.event.data.new_state.attributes.device_class == ''smoke'' }}'
  - condition: template
    value_template: '{{ trigger.event.data.new_state.state == ''on'' }}'

Any other hints?

Regarding the error:

2022-03-14 08:50:16 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{{ trigger.event.data.new_state.attributes.device_class == 'smoke' }}'

means to me: …new_state… has no ‘attributes’
Please correct my if i am wrong.

So how can i check ‘new_state’ if it’s None (or in other words is “None” = non defined)?

thx

Every single state change will have a new_state. It’s part of the definition of the object, so is defined will not work. checking for is not none should do the trick.

2 Likes

Can you limit the trigger to a group or something.
This automation fires constantly in your HA.

thx, will try that.
If not solved i will remove solution and come back here.

I dont know any other solution to trigger an all “smoke” device_class devices.
That’s an solution i have here from the forum.
If i add any device which has the wanted device class, it’s automatically included in the automation.

Do you know an other solution to this?

Create a group with all the devices.
How often do you plan on adding new devices?

Sure that would work, but when i’ll add an device (ex.: replace smoke sensor → remove old, add new) and forgot to add them in the group, in the WORST case there could be an undetected fire.

Will my current trigger affect performance?
May @petro can bring light into it :slight_smile:

thx

You’re triggering on every state change in the system. It’ll have some sort of impact.

1 Like

When I add new sensors to my system I generally make sure they work.
In this case, I assume you have a button to test the smoke detector, which then fires an event/changes a state or something.
First I would make sure the state/event works, but I would also want to make sure the automations function.

I personally would not see this as an issue. Since I would check before I installed the device in it’s final position.

Is all your automations created this way?

EDIT:

Just to give some context, I listened to state_changed events for 10 seconds in a home where nothing really is used at the moment.
No doors opening or closing, no media players running, no intentional zigbee traffic.
Remember… 10 seconds.

That is how many times this automation would have been fired.

1 Like

Thanks for your thoughts and infos.
It’s just two automations which works this way → smoke detection & alarm triggering (opening device_class).

But when i think of it, you are absolutely right!
That’s also my workflow for an new or replaced device.

Just ported over my fhem system, where such an automation was easy depending on the device_name.

I will change those automations in this way and the issue is solved.

thx
pOpY