How to add date and time to email notification

Hi everyone.

Total newbie here.

I managed to setup a DHT22 with a very simple email notification in the case of the temperature rising past a threshold.

  • I’d like the message in this email to read “The temperature of walking fridge 1 has gone over 5°C on (date and time)”

I’m not sure how to format this date and time information.

  • Also : can I change the subject of the email? Just says “Home Assistant” for now.

  • I’m sure I could find all these answers on the forum but… I can’t even find a search field for the forum. Am I missing the obvious?

Thanks so much for you help,

Ian

Show what you got.

I think so…

But where are you sending these emails from?
Directly from ESP or from Home Assistant?

You can use sensor.time to get the date and time.

I was indeed missing the obvious wasn’t I? Big fat magnifier icon not big enough for me.

Here is my configuration.yalm … I had to learn how to use secret.yalm first :stuck_out_tongue_closed_eyes:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

# Configure the Home Assistant Core to pick up the SSL certificates.
# This is done by setting the following configuration 
# for the HTTP integration configuration in your configuration.yaml
# DONT Forget to use https after this !!!
http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


#Sensor 1
sensor:
  platform: dht
  sensor: DHT22
  name: Temperature Salon
  pin: 4
  monitored_conditions:
    - temperature
    - humidity
    
# Pushbullet Notification service
notify:
  - platform: pushbullet
    api_key: !secret pushbullet_api
    name: pushbullet
  - platform: smtp
    name: email
    server: smtp.gmail.com
    port: 587
    timeout: 15
    sender: !secret email1
    username: !secret email1
    password: !secret email1_PW
    recipient:
      - !secret email2
    sender_name: Notification de Temperature

Is this the correct format to showcase the code?

It is, thanks. But there must be also an automation, which actually does the notification. Can we see it, too?

Sure. Thanks for helping

automations.yalm:

- id: '1614200060963'
  alias: Temperature Notification 1
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.temperature_salon_temperature
    above: '25'
  condition: []
  action:
  - service: notify.pushbullet
    data:
      message: Attention au sèche cheveux !!
  - service: notify.email
    data:
      message: The temperature has been too high on (date and time of alert)
  mode: single

Configuration.yaml:


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

# Configure the Home Assistant Core to pick up the SSL certificates.
# This is done by setting the following configuration 
# for the HTTP integration configuration in your configuration.yaml
# DONT Forget to use https after this !!!
http:
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


#Sensor 1
sensor:
  - platform: dht
    sensor: DHT22
    name: Temperature Salon
    pin: 4
    monitored_conditions:
      - temperature
      - humidity

  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'

    
# Pushbullet Notification service
notify:
  - platform: pushbullet
    api_key: !secret pushbullet_api
    name: pushbullet
  - platform: smtp
    name: email
    server: smtp.gmail.com
    port: 587
    timeout: 15
    sender: !secret email1
    username: !secret email1
    password: !secret email1_PW
    recipient:
      - !secret email2
    sender_name: Notification de Temperature

I only added three sensors. They are the once you generally need. If you want the other then just add them.
And I believe you had an indentation error and a missing - in the sensors.

automation.yaml:

- id: '1614200060963'
  alias: Temperature Notification 1
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.temperature_salon_temperature
    above: '25'
  condition: []
  action:
  - service: notify.pushbullet
    data:
      message: Attention au sèche cheveux !!
  - service: notify.email
    data:
      message: The temperature has been too high on {{states.sensor.date_time.state}}
  mode: single

And I believe you had an indentation error and a missing - in the sensors.

Yeah I guess it was working because I only had one entry ?

I’ll be trying your code. Is there any documentation on why

{{states.sensor.date_time.state}}

should be formatted like this? Why double brackets? Why states at the beginning and state at the end?

It is working ! Thanks so much.

Now onto trying to format the date and time to European standards, ill be following the second part here

And try to change the subject of the email to something else than Home Assistant, but that’s not a deal breaker.

So much to learn. Exciting :stuck_out_tongue_closed_eyes:

Double curly brackets mean that this is a template -> see the template docs here

And actually it would be better to use {{ states('sensor.time') }}, explanation why is in the linked docs.

"The temperature of {{ trigger.to_state.attributes.friendly_name }} has been too high on {{ now().strftime('%d.%m.%Y %H:%M') }}"

This will actually contain the time the notification was created, but it should be sufficient. Adjust format pattern according to your local standards.

Yes. title attribute in service call.

  - service: notify.email
    data:
      title: High Temperature!
      message: >-
        The temperature of {{ trigger.to_state.attributes.friendly_name }} has been too high 
        on {{ now().strftime('%d.%m.%Y %H:%M') }}