Send out status emails to dynamic recipients, depending on who request the status

Hello community,

I am just starting with Home Assistant and got my 2 Aqara sensors on my Conbee2 running to get a status about my garage doors.

At the moment I do not plan to configure an external access to my network, so I configured an imap sensor and a notification. So if I send an email to the monitored mailbox I got an email back with the status about these 2 sensors.

What I want to achieve is:

Depending on who requested the status mail (looking for the senders address) the notification’s recipient should be filled dynamically.
This would mean:

  • I send an email to the home assistant mailbox → I got the status back.
  • My wife send an email to the home assistant mailbox → ONLY my wife get the status back.

At the moment I use this config:

configuration.yaml

sensor:    
  - platform: imap_email_content
    name: imap_state_req
    server: server
    port: 993
    username: mail
    password: pw
    senders:
      - sender1
      - sender2

notify:
  - name: smtp_state_ans
    platform: smtp
    server: server
    port: 587
    timeout: 15
    sender: sender-address
    starttls: true
    username: mail
    password: pw
    recipient:
      - sender1
    sender_name: sender-name

automations.yaml

- id: statreq
  alias: Check Status by Mail
  trigger:
    platform: state
    entity_id: sensor.imap_state_req
  action:
    service: notify.smtp_state_ans
    data_template:
      title: Status from Home Assistant
      message: Status
      data:
        html: >
            STATUS GARAGE <br> 
            Door left: {{ states.binary_sensor.sensor_garage_left.state }} <br>
            Door right: {{ states.binary_sensor.sensor_garage_right.state }}

Do you have any idea how I get this working with dynamic recipients depending on the sender’s address?

Many thanks in advance!
toasti

I got it working!
Just one sensor, one notify and one automation for each email sender.

But do you have any idea how I got the sensor value changed from “off” to “closed” and “on” to “open”?
In the HO website I get the correct values, but not in the email message.
This seems to be normal for a binary_sensor - but how to change?

Thanks in advance!

The valid state values for a binary_sensor are on and off. Depending on the binary_sensor’s optional device_class, its state value may be displayed differently in the Lovelace UI (and only in the UI).

If you want to represent the two values differently, in an email message, you will need to convert them using a template.

{{ 'Open' if is_state('binary_sensor.sensor_garage_left', 'on') else 'Closed' }}

Many thanks!
But how to use it respectively where to add this in my code?

It’s a replacement for the template you currently use to report the left (and right) garage door’s state.

      data:
        html: >
            STATUS GARAGE <br> 
            Door left: {{ 'Open' if is_state('binary_sensor.sensor_garage_left', 'on') else 'Closed' }} <br>
            Door right: {{ 'Open' if is_state('binary_sensor.sensor_garage_right', 'on') else 'Closed' }}

I tried it and it was not working. Now it works, probably I made a mistake (copy & paste error).

THANK YOU VERY MUCH! Works perfectly now!

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions. For more information, refer to guideline 21 in the FAQ.

I did it :slight_smile:

I am also struggling with to include the last changed time of the sensor to the email.
Any idea?

I tried it this way:

{{ as_timestamp(states('binary_sensor.sensor_garage_left').last_updated) }}

Use the state_attr function instead.

Thanks!
I used this one

{{ as_local(states.binary_sensor.sensor_garage_left.last_updated) }}

and get a value, but it is not the last state the garage door was moving. It is probably the last time the state was written to the database.

You can’t use state_attr because last_changed and last_updated aren’t part of an entity’s attributes, they’re properties of the entity.

Toasti’s post above shows how to get the value of the last_updated property.

Try using last_changed.

It is giving me the same as last_updated.

last_changed represents the time when the binary_sensor’s state value changed. It is often the same as last_updated but not necessarily (the entity’s state can be updated without changing).

Thanks for the correction — my mistake.

Any idea how to get this working to see the last state the sensor was switched from open to close?