IMAP sensor not behaving as expected

My IMAP integration seems to be working properly in all aspects, except for the behavior of the sensor. I have configured the imap search command for “UnSeen UnDeleted Subject Notify”. I expected this the sensor count the number of messages that are: unread, and with a Subject equal to “Notify”. Instead, the sensor increments each time a target message is received, never returning to zero. Reading, deleting, and purging the trash have no effect on the count, which only increments with each new message. I’m wondering if I am mis-understanding the documentation, or if I am not using the search command properly.

It sounds like you have understood the docs and are using the search commands correctly. But, just to clarify, your search criteria should look like:

UnSeen UnDeleted Subject "Notify"

Have you tried disabling IMAP-Push? The default is to have it enabled to keep traffic at a minimum, but some providers require that IMAP clients ping them more often than HA does when push is enabled. If they don’t, the server disables the push from their side until they hear from the client again.

Thanks for helping. I changed the format of the IMAP search field, with no effect, it seems to work both ways.
Also I tried disabling and enabling Push_IMAP with no effect.
I’ve also completely deleted and reinstalled the IMAP integration, with no effect.

In a new experiment, I deleted all email from the INBOX.
Then enabled DEBUG logging.
At 6:45. I sent a single email with the subject “Notify”. My template was triggered and also my automation was triggered.

So there is only one email in the INBOX. Still the status of the entity is incrementing, now at “46”. (I have sent 46 messages to date).

I am not good at understanding the DEBUG log, but I don’t see anything that seems to give the current number of emails, so I don’t know how the integration keeps track of that.

Am I correct that that the sensor should be at “1”, instead of 46?
If the DEBUG file would help, I have it.
Thanks again.

Yes, if you deleted everything and then sent a single message, that would be the expectation.

What does your sensor show as it’s state_class in the States tool?

sensor.imap_webmaster_trinityvictoria_net_messages

State
47

State attributes (YAML, optional)
state_class: measurement
unit_of_measurement: messages
friendly_name: Webmaster_Message_Count

It sounds like you are trying to manage your IMAP in a client that does not fully support the standard, which means you are only marking messages for deletion, but not actually removing them.
Your client need to purge the IMAP directory for messages marked for deletion. This is not always the same as emptying the trash in your email client.

Thank you. That was exactly the issue. I was able to change the setting so that deleted messages were actually deleted instead of “marked for deletion”. Everything is now working as expected.

I have learned so much about HA during this. I really appreciate the help from everyone.