IMAP - Several emails

Hello, I am a rookie in this world.

I have an external application that sends a mail every x minutes. I have created some sensors inside the configuration.yaml file to capture their values, all perfect.

Now I have another application that sends me an email every 3 hours. When reading this other email, the values of the first sensors are deleted and the value of the new sensor appears.

I’ve been trying to prevent this from happening but I can’t.

Any solution?

sensor:
  - platform: imap_email_content
    name: Reef Tunnel Raw
    server: xxxxxx
    port: 993
    username: xxxxxx
    password: xxxxxx
    senders:
      - [email protected]  
  - platform: template
    sensors:
      hora_reef_tunel:
        friendly_name: Hora Reef Tunnel
        value_template: "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([0-9].*h)', index=0))[:-1] }}"
        unit_of_measurement: h
      temp_reef_tunnel:
        friendly_name: Temperatura Reef Tunnel
        value_template: "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([0-9].*C)', index=0))[:-1] }}"
        unit_of_measurement: °C
      ph_reef_tunnel:
        friendly_name: pH Reef Tunnel
        value_template: "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([0-9].*pH)', index=0))[:-2] }}"
        unit_of_measurement: pH
      redox_reef_tunnel:
        friendly_name: Redox Reef Tunnel
        value_template: "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([+-]?[0-9].*mV)', index=0))[:-2] }}"
        unit_of_measurement: mV
      ec_reef_tunnel:
        friendly_name: Conductividad Reef Tunnel
        value_template: "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([0-9].*mS)', index=0))[:-2] }}"
        unit_of_measurement: mS

You have two options that I can think of…

  1. Set up multiple IMAP email content sensors, using the value_template configuration option to separate out each data point.

  2. Convert your legacy format template sensors to the contemporary format so you can take advantage of the ability to use triggers.

Hi,

It would be possible to include a conditional on the sensor value depending on the subject text of the email. I’ve seen some examples but they don’t work for me. I still don’t understand much about HA

value_template: >-
      {% if "Reef1" in subject %}
          "{{ (state_attr('sensor.reef_tunnel_raw','body') | regex_findall_index(find='([0-9].*h)', index=0))[:-1] }}"
      
      {% endif %}
1 Like

Switching to trigger-based template sensors will allow the values to remain stable even when a new email is received because they only update when the trigger template becomes true. (Although, it does make the name of your IMAP sensor a bit inaccurate)

sensor:
  - platform: imap_email_content
    name: Reef Tunnel Raw
    server: xxxxxx
    port: 993
    username: xxxxxx
    password: xxxxxx
    senders:
      - [email protected]

template:
  - trigger:
      - platform: template
        value_template: >
          {{ 'Reef1' in state_attr('sensor.reef_tunnel_raw', 'subject') }}
    sensor:
      - name: Hora Reef Tunnel
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([0-9].*h)', index=0))[:-1] }}
        unit_of_measurement: h
      - name: Temperatura Reef Tunnel
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([0-9].*C)', index=0))[:-1] }}
        unit_of_measurement: °C
      - name: pH Reef Tunnel
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([0-9].*pH)', index=0))[:-2] }}
        unit_of_measurement: pH
      - name: Redox Reef Tunnel
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([+-]?[0-9].*mV)', index=0))[:-2] }}
        unit_of_measurement: mV
      - name: Conductividad Reef Tunnel
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([0-9].*mS)', index=0))[:-2] }}
        unit_of_measurement: mS

  - trigger:
      - platform: template
        value_template: >
          {{ 'Application2' in state_attr('sensor.reef_tunnel_raw', 'subject') }}
    sensor:
      - name: Hora Application 2
        state: >
          {{ (state_attr('sensor.reef_tunnel_raw','body') 
          | regex_findall_index(find='([0-9].*h)', index=0))[:-1] }}
        unit_of_measurement: h
...

Thanks, I’ll try later

Hi,

I have tried your code and it captures nothing. I have modified mine and now it works almost fine.

It reads perfectly the mail that comes from one of the computers (Reef1) but the one that comes from the other does not (Reef2). I have created a shipment from the Reef1 team but putting Reef2 in the subject and it integrates perfectly.

If I send it from the other computer or even myself from a gmail account it doesn’t work either. I have also tried to send one from gmail with the subject Reef1 and it doesn’t read it either.

What can be? I see the emails perfectly in the inbox.

Fixed, I had not included the new sender in the list.

Thanks for everything

1 Like