ShutDown PC after recieved email

Hello all.
I want my PC to shut down as soon as my backup is complete.
The only feedback I get from the backup is a mail. I have successfully created an imap_email_content sensor that works and gives the correct status (success/warning/failed).

But now I have a few questions about the implementation:

I can query the status of the sensor in the automation. But if I get a mail with status “successful” every day, the value of the sensor never changes and I can’t set it as a trigger.
I also can’t set a time trigger that queries a condition, because a) I don’t know when the backup is exactly finished and b) the sensor could read the mail from the previous day, because maybe none came today.

So I need a trigger that reads and deletes the mail as soon as a new mail arrives.

Can I realize this somehow or do I understand the sensor basically wrong?

Thanks for your help
Loki

What if you run automation like this:
if [email-check] = success then
set state [email-check] = ‘idle’
shutdown

You could do this possibly with the email sensor or create a input_boolean

That would be one possible solution. But something has to reset the input_boolean. For this I would probably need a trigger “New eMail”.

I tried it this way, but the input_boolean, once set, always stays “on” even when new mails come in.

alias: Test eMail
description: ''
trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: state
    entity_id: sensor.veeam_datensicherung
    state: fehlgeschlagen
  - condition: state
    entity_id: input_boolean.veeam_idle
    state: 'off'
action:
  - device_id: 61e96b842e39d1adccd044d4c118a3c2
    domain: mobile_app
    type: notify
    message: Status Veeam
    title: Veeam
  - service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.veeam_idle
mode: single

And I can’t change the value of sensor.veeam_datensicherung directly, can I?

You could use another helper for that with multiple values
Input Text - Home Assistant (home-assistant.io)

Check this for the inp.bool
How to set input_boolean to value of another input_boolean - Configuration - Home Assistant Community (home-assistant.io)
And there is the option to use a python script for changing states, I use below one but there are other examples.
Name it e.g. set_state.py, store in config/pythin_scripts (add this also to the configyaml) and then you can call that as a service

#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------

inputEntity = data.get('entity_id')
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()

newState = data.get('state')
if newState is not None:
    inputState = newState
    
newIcon = data.get('icon')
if newIcon is not None:
    inputAttributesObject['icon'] = newIcon

hass.states.set(inputEntity, inputState, inputAttributesObject)

Thank you for your detailed answer.
I will have a look at it. But is there really no easier way to check mails? It would be enough if HA would read the mail and then delete it. Or if HA marks the mail as read and he only processes unread mails.

Sorry but I have no knowledge in that area as I am not using HA with emails. So possibly it can be done easier.