State Not Updating - IMAP

Hi all.

After spending ages trying to figure this out, I thought I would come here to see whether there’s anyone that can help.

I’m trying to avoid opening ports etc on my router to access my HA instance remotely, and the best option (for what I am trying) feels like emails from a secure account.

I have managed to get “Sending” emails sorted, but “receiving emails and then triggering” seems to be evading me. I have managed to get it to work intermittently, and for a short beautiful while I got it working, but then I somehow broke it!

So… Can anyone help a simpleton like me figure this out?

Here’s my Config sensor:


sensor:
  - platform: imap_email_content
    server: imap.gmail.com
    port: 993
    name: alarmmail
    username: redacted@redacted
    password: redacted
    scan_interval: 10
    senders:
      - redacted@redacted
    value_template: >-
      {% if "Alarm status" in subject %}
        'alarm_status'
      {% elif "Arm alarm" in subject %}
        'arm_alarm'
      {% else %}
        'no_new_emails'
      {% endif %}

And part of my automation


- id: 'nnn'
  alias: Via_Email_Alarm_Status
  trigger:
  - entity_id: sensor.alarmmail
    platform: state
    to: alarm_status
  action:
  - service: notify.send_an_email
    data: 
      title: Alarm Status - {{now().strftime("%Y-%m-%d %H:%M:%S")}}.
      message: 
               
               Alarm Status is currently {{states.alarm_control_panel.area_001.state}}.

If I jump into HA and check the state of alarmmail, it’s “unknown”.
If I send the email with the right word in the title, then the ‘via_email_alarm_status’ isn’t triggering.
If I jump into HA and manually update the state of alarmmail to “alarm_status” then the ‘via_email_alarm_status’ triggers and I get my email (the state then returns to “unknown”.

All the above makes me think that I am missing something really simple within the sensor in my config file, but I cannot seem to figure out what that is…

Any help at all would be hugely appreciated!