Home » Blog » How To send e-mail PERFECTLY using Gmail in Home Assistant

How To send e-mail PERFECTLY using Gmail in Home Assistant

Hi,
Today we are going to configure Home Assistant so that Home Assistant sends notification e-mails using Gmail automatically on certain events. Next to that, I will show you how you can toggle the out-of-office mode in Gmail using Home Assistant. In this video, you will learn about setting up Gmail, Helpers, Automations, and how to create a simple dashboard to set up your vacation settings.


⭐⭐⭐ NOTE: ⭐⭐⭐

This article accompanies a YouTube video. I wrote it for people who would rather read than watch a video. To keep doing this, I would like to ask you to also check out the video, leave a comment under the video, give the video a thumbs up, and subscribe to my YouTube channel. This means that the video is offered more often to new visitors so that they also stay informed of the latest Home Assistant tutorials.

Thank you for your support!

Ed

Click here to watch the video

There are multiple ways to let Home Assistant send notifications. I created videos about sending messages to your phone before, and this time I will explain how you can send an e-mail using Gmail in Home Assistant.

Configure Gmail to use it with Home Assistant

For this, you’ll need a Gmail account. Fortunately, you can create this for free and I assume that you have already created a Gmail account. The first thing that we need to do, is create a Client ID and Client Secret at Google.

  • Go to the Google Developers Console. You’ll find the link in the description below the video.
  • If you don’t have created a project yet, then create a new project by clicking on Create Project. Otherwise, select an existing project.
  • Give your project a project name, for instance, Smart Home Junkie GMail.
  • Then click Create.
  • Your project should be selected automatically, but you can click select project if it isn’t.
  • Now click “Enable” to enable the Gmail API.
  • After the API is enabled, click the Credentials link in the left menu.
  • You’ll see that no credentials have been created yet. Before you can do this, you need to configure the OAuth consent screen.
  • Click OAuth consent screen.
  • Select External and click “Create”.
  • Now enter a name for your App. For instance Home Assistant.
  • Select the user support email next. This is the email address of your account.
  • Scroll down and enter the Developer contact email address. This can be the same email address that you’ve selected before.
  • Click save and continue.
The Scopes Page
  • You will now see the Scopes page.
  • Just scroll down and click “Save and Continue”.
The Test Users Page
  • This next page is the Test users page. You do not have to enter any test users here, so click on Save and Continue again.
The OAUTH Consent Screen
  • Now click on the OAuth consent screen link in the left menu again.
  • Click on Publish App to push this app to production.
  • Click Confirm.
  • Now select the Credentials option in the left menu.
  • Click Create Credentials.
  • Select OAuth Client ID.
  • Select Web Application in the Application type dropdown.
  • Enter a name, for instance, Home Assistant Credentials.
  • Go to the Authorised Redirect URIs section and click Add URI.
  • Enter the following URL: https://my.home-assistant.io/redirect/oauth
  • Click Create.
  • Copy the Client ID and save it in a text document.
  • Copy the Client Secret and save it in a text document as well.
  • Click OK.
  • Double-check if the API is enabled and click on Library in the left menu.
  • Search for Gmail.
  • Click Gmail API.
  • Check if it is enabled. If not, click enable to enable the API.

Configure Home Assistant for use with Gmail.

The Client ID and Client Secret are now generated and you stored them in a text document. You can now start configuring Home Assistant.

  • Go to Home Assistant
  • Click Settings > Devices and Services.
  • Click Add Integration.
  • Search for Google.
  • Select Google Mail.
  • Enter a name and paste the OAuth Client ID and OAuth Client Secret.
  • Click Add.
  • The Google account page will open.
  • Select the account that you just used to set up the Client ID and Secret.
  • You might get a message that the app is not verified. In that case, click Advanced.
  • Now click on Go to home-assistant.io.
  • Toggle the select all checkbox to on.
  • and Click Continue.
  • Click Link Account.
  • Home Assistant will open again.
  • Click Finish.

Test Google Mail in Home Assistant

Google Mail is now configured in Home Assistant. Let’s test if it works.

  • Click on Developer Tools.
  • Open the Services tab.
  • Select notify.your e-mail address. In my case, it’s notify.smarthomejunkietest_gmail_com.
  • Enter a message.
  • Enter a title. This is the subject of the e-mail that you are sending.
  • Enter a target email address in the target field. I enter the same address here, but you can enter any email address that you like.
  • You can use the data field to enter cc, bcc, and From addresses, but I will leave it blank for this test.
  • Click Call Service.
  • When you see a green checkmark, the email is sent to the address that you specified.
  • Now go to your email inbox and check if the email arrived.
  • You should see the email in your inbox now.

Create an automation to send an e-mail

You can use this same mechanism in automations. For instance, to send an email when the alarm is triggered.

  • For that, go to Settings-> Automations and Scenes.
  • Click Create Automation.
  • Select Create New Automation.
  • In the triggers section, clcik add trigger and select the state trigger.
  • Select your alarm panel in the entity field. If you want to know how you can create your own alarm system in Home Assistant using your sensors and lights, then please watch this video where I explain this. The link is in the description below the video.
  • Select “Triggered” in the To field.
  • Now click Add Action in the Actions section.
  • Select Call Service.
  • and select the notify.your email address service.
  • Enter a message in the message field and enter a subject in the title field.
  • Fill in the recipient email address in the target field.
  • Click Save and give your automation a meaningful name.

You’ve now set up an automation that will send an email as soon as your alarm gets triggered. There are many more use cases for sending email and I am curious what use cases you can think of. Please let me know in the comments what your use case is!

I need your help!

You will be doing me a huge favor if you subscribe to my channel if you haven’t already. And, you help me a lot if you also give this video a thumbs up and leave a comment. This way, YouTube will present this video to new people and that will make the channel grow! In the description of the video, you will also find information about how you can sponsor me so that I can continue to make these tutorials for you.

Thank you!

Setting up a dashboard and automation to send emails from Home Assistant

Next to sending an email, you can also toggle the out-of-office notice in Gmail through Home Assistant. This way, you can toggle the out-of-office notice automatically when you leave your house, on the weekends, or during your vacations. For the sake of this tutorial, I created this dashboard plus an automation that you can use to turn vacation mode on or off manually. Let’s go over it to see how I created this.

The dashboard to toggle the vacation mode

As you can see, I created a toggle to switch vacation mode on and off. If I switch it to on, you will see that vacation mode in your Gmail settings will be set to on. The subject and message are filled in, and the start and end dates are filled in as well. When I switch the vacation mode to off, the vacation mode in the Gmail settings is switched to off as well.

Create the helpers

To create this dashboard, I needed to create some helpers.

Go to Settings->Devices and Services and click on the helpers tab to see all your helpers.

  • The first helper that I’ve created is a toggle helper which I called vacation mode. With this helper, I can turn the vacation mode on and off.
  • The second helper is a text helper for the subject. This helper will contain the subject text. I’ve set the maximum length to 255 characters for this helper.
  • The third helper is a text helper for the message. This helper contains the message of the email. I’ve set the maximum length to 1000 characters.
  • Then I created two datetime helpers for the first and last vacation days. These helpers are set to only store the date.
Create the dashboard

Now that the helpers are defined, we can create a dashboard.

  • Open one of your dashboards.
  • Click on the three dots in the upper right corner.
  • Click Edit Dashboard.
  • Click on the plus sign to create a new dashboard view.
  • Give your dashboard view a name like vacation.
  • Click Add card.
  • We are going to use a mushroom card for the title. If you haven’t got mushroom cards installed, you can install them using HACS. See this video on how to install HACS in Home Assistant. The title is not really needed, but I think it looks nice. If you don’t want to use Mushroom cards, you can also use the markdown card instead to show a title.
  • Search for “title”.
  • Click Custom: Mushroom Title Card.
  • Enter a title in the title field.
  • Click “Save”.
  • Click Add Card.
  • Search for Tile and click on the Tile card.
  • Search for “vacation mode” in the entity field and select the Vacation Mode helper that we’ve just created.
  • Click Save.
  • Click Add card again.
  • Search for Tile and click on the Tile card.
  • Now search for Subject in the Entity field and select the Subject helper that we’ve just created.
  • Click Save.
  • Click Add Card.
  • Search for Tile and click on the Tile card.
  • Now search for Message in the Entity field and select the Message helper that we’ve just created.
  • Click Save.
using a grid card
  • And again, click on add card, but this time search for Grid and select the grid card.
  • Now search for Tile in the search box and select the tile card.
  • Search for “First” in the entity field and select the First Vacation Day Helper.
  • Open the appearance section and set Vertical to on. This way, the date is shown a lot nicer on the card.
  • Now click on the Plus sign in the Grid Card configuration to add a new card to the grid.
  • Search for Tile in the search box and select the tile card.
  • Search for “Last” in the entity field and select the Last Vacation Day Helper.
  • Open the appearance section and set Vertical to on for this card too.
  • Go to the columns field and set the number of columns to 2.
  • And, toggle Render cards as squares off so that the cards within the grid look smaller.
  • Click Save.
  • Click Done.

Your dashboard is now ready and you can edit the subject, message, start date, and end date using this dashboard now.

The automation to toggle the vacation mode

The last thing that we have to do is create an automation that triggers if the vacation mode is toggled on or off.

  • Go to Settings-> Automations and Scenes.
  • Click Create Automation.
  • Click Create New Automation.
  • Click Add Trigger.
  • Select State to create a state trigger.
  • Select the Vacation Mode helper in the entity field.
  • Select “On” in the To field.
  • Click on the three dots in the trigger section and select Edit ID to create a Trigger ID. If you want to know how Trigger IDs work, then watch this video where I explain what Trigger IDs are and how they work.
  • Add a Trigger ID name, for instance, Vacation Mode On.
  • Now click Add Trigger.
  • Select State again and in the entity field, select the Vacation Mode helper.
  • Select “Off” in the To field.
Add the actions
  • Now let’s create the actions that should take place as soon as one of these two triggers is triggered.
  • Go to the Actions section and click Add Action.
  • Select the If-Then action.
The If statement
  • Within the If statement, click on the Add Condition button.
  • We want to check if the Trigger ID is triggered, so select the Triggered By condition.
  • Select the Trigger ID Vacation Mode On.
  • Now go to the Then statement and click the Add Action button to add an action to take when the Vacation Mode Helper is toggled to On.
  • Select Call Service.
  • Select Google Mail: Set Vacation as the service.
  • Click Choose entity and select the Gmail sensor entity for your account.
  • Make sure that Enabled is switched on.
  • We are going to point to our helpers in the Title, Message, Start, and End fields. For this, we need to add some YAML.
  • Enable the tile field and start by entering two accolades.
  • You’ll see that the edit field will change into a YAML edit field.
  • Now add the following code for the title. This code points to the Subject helper that we’ve just created.
  • Do the same for the message, start, and end date.
The Else statement
  • Now go to the Else statement and click on the Add Action Button.
  • Select Call Service.
  • Select Google Mail: Set Vacation as the service.
  • Click Choose Entity and select the Gmail sensor entity for your account.
  • Make sure that Enabled is switched off.
  • Type a dummy message in the message field. The message field should contain some text otherwise turning off the vacation mode will not work. I already asked the developer to change this, but at the time when I created this video, you do have to enter a message here.
  • Click save and give your automation a meaningful name.

Your automation is now ready!

Conclusion

So, this is how you can integrate Gmail into Home Assistant. I hope this helped. Let me know in the comments if you would use this or not. Thanks for watching and if my videos save you time, please consider supporting me through Patreon, Ko-Fi, or by becoming a member of my channel. These wonderful people are already sponsoring me and ensuring that I can continue making these tutorials. I cannot keep doing this without your help! You’ll find links to support me in the description of this video!
And please do not forget to give this video a thumbs up and subscribe to my channel to make the channel grow!

I will see you soon in my next video. Bye Bye!


,

Back to all articles