I am trying to send a custom notification to my iOS phone dependent on the type of image detected by DOODs. I’ve tried many different variants of the below code and would love help on getting it to work.
Essentially, I am trying to read the attribute of an attribute to see if it is a person, else, identify as a car. Clearly, it is not working as it always skips over the “if” and returns “car”.
Thank you so much it worked, you are a life saver, I would have never come across that on my own. Does this syntax work with elif as well incase I want to add other objects to detect?
The example I posted uses an “inline if statement” simply because it’s compact but that’s not what corrects your original template’s error. What is important is how the attribute is accessed and it’s this part that is the correction:
message: >-
{% set s = state_attr('image_processing.doods_front_door', 'summary') %}
{% if s.person > 0 %} {% set x = 'Person' %}
{% elif s.car > 0 %} {% set x = 'Car' %}
{% elif s.whatever > 0 %} {% set x = 'Whatever' %}
{% else %} {% set x = 'Unknown' %}
{% endif%}
Front Door - {{ x }}
Please consider marking my first post with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. This helps others find answers to similar questions.
Ok well it looks like if it does not detect a person then it doesn’t insert “person” into the summary: thus giving me an “UndefinedError: ‘dict object’ has no attribute ‘person’”. Is there another way to go about this by looking to see if an attribute exists in summary?
You never mentioned that the attribute may not exist; the possibility of its absence makes things a bit messier.
Does summary always exist or is there also a possibility it may not? I’ll assume it always exists.
You must first test if the desired attribute is defined before you can attempt to acquire its value. Here’s a very basic example of the technique:
message: >-
{% if state_attr('image_processing.doods_front_door', 'summary').person is defined %}
Front Door - {{ 'Person' if state_attr('image_processing.doods_front_door', 'summary').person > 0 else 'Car' }}
{% endif %}
Sorry, I was not aware the attribute didn’t appear unless it was detected. The summary always exists.
When I get time this weekend I will give this a try and get back to you. Thanks again!
BTW, here’s the other example enhanced to check if the attribute is defined before using it:
message: >-
{% set s = state_attr('image_processing.doods_front_door', 'summary') %}
{% if s.person is defined and s.person > 0 %} {% set x = 'Person' %}
{% elif s.car is defined and s.car > 0 %} {% set x = 'Car' %}
{% elif s.whatever is defined and s.whatever > 0 %} {% set x = 'Whatever' %}
{% else %} {% set x = 'Unknown' %}
{% endif%}
Front Door - {{ x }}
This is what I ended up with thanks to your insight. Since, I only needed to know if the attribute is defined and you showed me how to do that I came up with the following code. Thanks a million again!
message: >-
Front Door - {% if state_attr('image_processing.doods_front_door', 'summary').person is defined %}Person
{% elif state_attr('image_processing.doods_front_door', 'summary').truck is defined %}Truck
{% elif state_attr('image_processing.doods_front_door', 'summary').bus is defined %}School Bus
{% else %}Car
{% endif %}
You should know that it’s the custom of this community to assign the Solution tag to the post that introduces the technique/concept/idea that leads to resolving the original problem/question. In this case you took the code already presented (with minor changes) and marked your own post as the Solution. If every user did that then it would appear that everyone ultimately “solves” their own problem.
I did have that debate in my head thinking if someone was looking for the same solution it would cut right to the chase. But on the other hand it was not in fact my own solution. Maybe in the future they will come up with a method in this forum to rectify this but in the meantime I did go back and give you credit. Again I can’t thank you enough for all your time and patience with me.
I understand the dilemma. The author of the topic wants an answer tailored for their exact environment. That usually means code containing entities and values for their system. It’s the precise solution for them and so they naturally want to assign it the Solution tag.
However, very often, the tailored post is devoid of explanation or context and is simply an example of the process but not the process itself. A sloppy analogy would be the answer to a math problem but without explaining how to calculate that answer. For users trying to solve similar problems, knowing the ‘how and why’ is important. Very often, users struggle to adapt someone else’s code because they have no idea how or why it works.
Anyway, this is all “meta” and the key thing is your original question has been answered and the method explained.