Automation - imap mark message as seen - uid from actual message

hi, I am trying to include in the automation that the UID for the message triggers that the message received is “marked as seen”.

I get the UID from imap_content (template already in YAML taken from the example here : IMAP - Home Assistant and including UID when “imap_content” when event is triggered).

the best I am getting is eg “(303)” when i include in the YAML : “UID: ({{state_attr(“sensor.imap_content”, “UID”)}})” … any ideas how to possibly get this working?

I found this but honestly I am not really understanding what has to be included and not to get it to work and would appreciate some help.

here is the automation script redacted here and there

id: 'redacted0'
alias: imap_control
description: ''
triggers:
  - type: value
    device_id: redacted1
    entity_id: redacted2
    domain: sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 0
    above: 0
conditions:
  - condition: template
    value_template: >-
      {% if  "test ha" in state_attr("sensor.imap_content", "Subject") and
      "[email protected]" in state_attr("sensor.imap_content", "Sender") %}
        True
      {% else %}
        False
      {% endif %}
actions:
  - action: imap.seen
    metadata: {}
    data:
      entry: redacted4
      uid: '303'
    enabled: true
  - action: notify.email_notification
    data:
      message: redacted5
      title: redacted6
mode: single

any ideas?

I would suggest you use the same imap_content event trigger you are using for your sensor instead of a Device trigger. Truthfully, I don’t see how that would ever work since trigger-based template sensors cannot currently be part of a device.

Using the event trigger will give you direct access to the uid and other values stored in the trigger variable:

id: 'redacted0'
alias: imap_control
description: ''
triggers:
  - trigger: event
    event_type: "imap_content"
    event_data:
      initial: true
      sender: [email protected]
conditions:
  - condition: template
    value_template: >-
      {{ trigger.event.data.subject is search("test ha") }}
actions:
  - delay: 2 
  - action: imap.seen
    metadata: {}
    data:
      entry: redacted4
      uid: "{{ trigger.event.data.uid }}"
    enabled: true
  - action: notify.email_notification
    data:
      message: redacted5
      title: redacted6
mode: queued 

maybe to help explain, what I included was a snippet of the code i found in the trace in the automation done in the visual editor. The condition section seems to work perfectly and I figured if it worked there then maybe it could work for the UID. There are other parts to the automation i have removed to try to just keep what was needed here. I hope it was clear but i have a template in my configuation.yaml to get these values (removing what i did not think i will ever need) :

template:
  - trigger:
      - trigger: event
        event_type: "imap_content"
        id: "custom_event"
    sensor:
      - name: imap_content
        state: "{{ trigger.event.data['subject'] }}"
        attributes:
          UID: "{{ trigger.event.data['uid'] }}"
          Message: "{{ trigger.event.data['text'] }}"
          Sender: "{{ trigger.event.data['sender'] }}"
          Date: "{{ trigger.event.data['date'] }}"
          Subject: "{{ trigger.event.data['subject'] }}"
          Initial: "{{ trigger.event.data['initial'] }}"

can you help me understand / advise me on how to use this new code - would you advise I disable the visual editor automation and put this into the configuation.yaml then as eg

automation this_automation:
  #code here?

followed with the code you suggest and fixed redactions (in a specific place in the configuration.yaml? ) or how should one do this best?

I am really thankful for your help but I am truthfully struggling with the coding as I feel it is a bit javascript like with what seem to be lots of ways to rome (visual, yaml editing, hybrid) and quite a few loosy goosy rules /guidelines / best practices (what to include, what not and when) - to be honest I probably have not spent enough time or found the right docs that have clicked for me yet but it seems the step from visual editor usage to moving it all to configuation.yaml I find really complex as for example when I put in the snippet autogenerated from the visual editor automation into the yaml I seem to get errors or it just does not work (just now my original code autogenerated just does nothing if put into configuration.yaml with header "automation this_test: " as i wondered above). Any tips in general how to better understand when straggling between these worlds (visual and yaml editing)?

finally it takes sooo much time with imap to figure out if what i did made any difference, or making changes to configuation.yaml (as it needs to be reloaded) … but for imap sometimes it takes 10min to get it to refresh (other times it is near instantaneous)… is there a trick to get imap to trigger and check directly for unseen messages?

There is no need for you to edit your configuration.yaml file for automations, just edit the automation in the Automation Editor. You can do it manually, “transcribing” what I have posted or copy/pasting by selecting “Edit in YAML” from the 3-dot expansion menu at the top right in the automation editor.

If you need one, there is a slightly dated, but still informative video primer on transcribing between YAML and the UI Automation editor available on the ResinChem Tech Youtube channel .

Because of the way those files are merged and stored there are slight differences. Until you are comfortable with YAML, I would suggest you not bother to move automations. There’s no need to do so.

It really comes down to personal preference. Even though I’m capable of writing automations directly in YAML, I mostly start my automations and scripts in the visual editors to build out the overall structure quickly and then switch to editing it in YAML for templates or options that aren’t supported in the visual editor. Doing it that way guarantees that the automation is assigned a valid unique ID, that the syntax is valid, and that I can make small changes through the UI if I want to in the future… while still allowing more sweeping changes to be made using YAML either through “Edit in YAML” or inside automations.yaml.

For all other non-UI configuration I use Packages because I prefer its flexibility over the other methods of splitting the config.

You may also want to take a look at YAML for Non-Programmers.

You can reload the base sensors by selecting the IMAP integration in the Integrations dashboard then clicking “Reload” from the 3-dot expansion menu for the sensor you want to update. However, depending on how their triggers and conditions are configured, reloading the integration won’t always update related trigger-based template sensors.

thanks so much for the advice and links I really appreciate it. Really interesting and nice to get an idea of how others are working. It makes a huge difference and hopefully this will be helpful for others as well!

I will in the meantime keep trying and will work through all the links!