IMAP email contents - problem with template code

I have the following sensor defined:

- platform: imap_email_content
  name: gmail_carnet
  server: imap.gmail.com
  port: 993
  username: !secret heimenvaar_google_user
  password: !secret heimenvaar_google_password
  senders:
    - !secret marius_gmail_address
  scan_interval: 60
  value_template: >-
    {% if states.sensor.gmail_carnet.attributes.subject.find("inn i Lia") | int >= 0 %}
      'home'
    {% elif states.sensor.gmail_carnet.attributes.subject.find("ut av Lia") | int >= 0 %}
      'away'
    {% else %}
      '??'
    {% endif %}

But I get the the following in the log:

jinja2.exceptions.UndefinedError: ‘mappingproxy object’ has no attribute ‘subject’

The documentation states that from, subject, body and date should be possible to use in template code.

If I remove the value template the sensor is instatiated and loads the email.

I can then paste the template code in the Jinja-template test page and I do get the values that I am after

The following code yields 39 (as expected) in the template testing tool

{{ states.sensor.gmail_carnet.attributes.subject.find(“inn i Lia”) }}

So it looks like that the value template code is executed before the mail object/sensor is instantiated somehow, and gets a null/none reference.

I know this is old, and maybe solved by now, but I happened upon this subject while searching for examples of the value template. I solved it by not having a value template in this sensor and then making a template sensor with the stuff I want. But I think the problem is that you’re trying to get the value from the sensor in the regular template way, then it’s trying to evaluate it self while it’s getting the new value. That doesn’t work. I think you should’ve skipped “states.sensor.gmail_carnet.attributes” and go right to “subject”. Maybe someone else happens upon this and can be helped by this.