Category: Integrations

How to use Home Assistant to get an alert if your Tesla is not plugged in at night

I bought a Tesla Model 3 last year and it has been the best car I’ve ever owned, by far! Feel free to use my referral link if you want to purchase one also and get 1000 free Supercharger miles.

One of the best parts of owning it is that it never needs to be filled up for most daily driving. I don’t have to wonder if I have enough gas to go do daily chores. It charges up every night and is ready to go every morning.

This means that you have to remember to plug the car in every night. Normally, I plug it in when I get home from work (even though it is not scheduled to charge until later in the night). However, there are times when it doesn’t get plugged in for whatever reason (kids throwing a fit as soon as we get home, for instance).

I needed a way to make sure I always plugged the car in so it would charge during the night. Luckily, this is a simple automation in Home Assistant.

First, you need to configure the Tesla integration. This will give Home Assistant the ability to query your car to see if it is plugged in. It also gives you a lots of useful metrics about your car in the dashboard. Fun aside, my kids were so impressed with the speed of the car compared to my truck that they named my car Red Rocket! Therefore, lots of the entities have some reference to that name.

  • You can add the Tesla integration by clicking on Configuration then Integrations.
  • Click on the Add button in the lower right-hand side and search for Tesla.
  • Enter your Tesla.com username and password. Ensure the integration is able to successfully login to your Tesla account.
  • After the Tesla integration is finished configuring, you will see a large number of new entities. You can view them by searching for the Tesla integration in the Integrations page and clicking on Entities (I have renamed some of mine to make it more obvious what they were, so your entity names will vary).

The most important one for our purposes is the binary_sensor.red_charger_sensor as this one indicates whether or not the car is plugged into the charger.

Now that we have Home Assistant configured to communicate with your Tesla, the automation code is easy to write. In your automations.yaml file (or the GUI if you are so inclined), you will need to add the following code.

- alias: Notify if Tesla not plugged in at night
  initial_state: True
  trigger:
    - platform: time
      at: "19:30:00"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: binary_sensor.red_charger_sensor
        state: 'off'
      - condition: template
        value_template: "{{ is_state_attr('binary_sensor.red_parking_brake_sensor', 'shift_state', 'P')}}"
  action:
    service: notify.notify
    data:
      message: "Tesla Model 3 not plugged in!"
      title: "Alarm"

trigger – This automation will fire at 7:30 PM every night (if I am still at the office by this time, I am working too hard!).

condition – I need to both check both that the Tesla is not plugged in (by virtue of the binary_sensor.red_charger_sensor being in the off state) and that the car is in Park (as indicated by the binary_sensor.red_parking_brake_sensor.shift_state being equal to P). I don’t want this automation to go off if I am driving.

I could have added a check to make sure I am home, but I still wanted the reminder in case I am traveling and need to ensure the car is plugged in.

action – I use the default notify.notify since this will send a notification to both my phone as well as my wife’s phone.

As you can see, this is a simple automation to add, but it has saved me from not having a charged car many times.

How to monitor your home Windows PC with Home Assistant & Glances

A common request is to be able to monitor your home computers using Home Assistant. It is easy to monitor basic machine information on the machine that you have installed Home Assistant on (using System Monitor).

A much more powerful way is to use Glances. Glances is a cross-platform system monitoring tool written in Python.

There is already a pre-built addin to make it easy to monitor the machine Home Assistant is running on. However, this doesn’t let you monitor a remote machine (not the same machine your Home Assistant is running on).

I have a Windows PC on my home network (to play games do work on). I would like to monitor this machine from Home Assistant.

Here are the high level steps we will go through.

  • Install Glances on the remote Windows PC with pip
  • Test Glances locally
  • Create a Windows Scheduled Task to run Glances on login
  • Open the Windows Firewall port so Home Assistant can communicate with the local Glances service
  • Add a new Glances Integration in Home Assistant
  • Add new Glances sensors to UI

Install Glances on the remote Windows PC with pip

You will need to install the Glances Python program onto your Windows PC. The easiest way to do this is via the command line. You can easily install the Glances application using the Python package manager called pip.

Follow the instructions on the Glances GitHub page for your platform. Since I am running Windows on the machine I would like to monitor, I can run the following command. Obviously, you must have Python & pip installed on your Windows PC.

NOTE: You must install Glances as Administrator. Open the Windows Powershell command prompt as Administrator.

pip install glances

Test Glances locally

Now that you have installed Glances, we need to ensure it is working. In the same Administrator command prompt you just opened, run the following command to start up Glances manually.

glances -w

This will start the local instance of Glances so you can test it. You should see a window that looks like this.

You can now open a local browser and see the Glances GUI. Navigate to the following URL.

http://localhost:61208

Create a Windows Scheduled Task to run Glances on login

Now that you have installed Glances & tested it, you can create a Windows Scheduled Task to run it. This is needed because you want Glances to always be running in the background, no matter who is logged into the PC.

Open the “Task Scheduler” as Administrator by searching for it on the Start Menu.

In the Task Scheduler GUI, on the right-hand side, click on “Create Task…”

On the General tab, set the Name to “Glances” and set the Configure for to “Windows 10”

On the Trigger tab, click New. Set the Begin the task to “At log on” and set the Delay task for to “15 minutes” (customize as needed). Click OK.

On the Actions tab, click on New. Set the Action to “Start a Program”. The most likely location for Glances to be installed is “C:\Python27\Scripts\glances.exe” (your mileage may vary). Make sure to add the “-w” argument so it starts in “web server” mode. Click OK.

You can set any other settings you want to or your can just click OK to finish. You will now see the “Glances” task in the Task list.

Click on the Run button on the right-hand side of the window to force it to run now. Close the Task Scheduler.

Open the Windows Firewall port so Home Assistant can communicate with the local Glances service

Most likely, you will need to open a Windows Firewall port in order to allow Home Assistant running on another computer (a Raspberry Pi, for instance) to communicate with the locally running instance of Glances.

Open the “Windows Defender Firewall with Advanced Security” as Administrator by searching for it on the Start Menu.

Click on Inbound Rules on the left-hand side of the window. Click on New Rule… on the right-hand side of the window.

Set the Rule Type to “Port”. Click Next.

Set the Protocol to “TCP” and the Specific local ports to “61208”. This will allow incoming requests to be received by your local instance of Glances on this port only. Click Next.

Set the Action to “Allow the connection”. Click Next.

Make sure the Profile is set to only allow Domain and Private, not Public. This is because you don’t want Glances to respond to requests when you are not connected to your home network (since why would a public network (like a coffee shop WiFi) be trying to communicate with your computer over this port. This would most likely be a hacker scanning all available ports to see what responds. Click Next.

Ensure local network is Private (Optional, may not be needed for your network)

An issue I had was my home network was not set to Private for some reason. I was able to fix this by opening the Registry and setting the appropriate network to be Private instead of Public.

Note: there are ways to set this via the Control Panel Network settings, but these GUIs never worked for me. Therefore, I fell back to modifying the Registry. Use at your own risk!!!

Open “Regedit” as Adminstrator and navigate to the following key.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles

Under the Profiles key, click on each one in turn until you find the one that is your home connection (look at the ProfileName key). When you find it, double-click on the Category key and change the value to 1 (meaning Private). Close Regedit.

Give the new Inbound Firewall rule a Name “Glances” and click Finish. Close the Windows Firewall GUI.

Add a new Glances Integration in Home Assistant

Now that we have installed Glances, set it up to run automatically (and forced it to run now) and opened the Windows Firewall, we can set up Home Assistant.

Navigate to your Home Assistant home page and click on Configuration then Integrations.

Click on the orange New button at the bottom-right side of the page.

Search for Glances and click on the item.

Change the Host to the IP Address of your Windows PC (you can get this by running ipconfig from the Windows command line). My local address is blanked out in this screenshot. Click Submit.

Assuming a connection is made, you will see the new Glances integration in the list. Click on it. You will see all the new sensors that have been added to Home Assistant.

Add new Glances sensors to UI

Finally, we need to add these sensors to the Home Assistant UI so we can see the results. I am going to add them as simple badges.

Open up your lovelace YAML file (mine is broken up into different pages per tab) and add the following code.

badges:
  - sensor.glances_containers_active
  - sensor.glances_containers_ram_used
  - sensor.glances_disk_used_percent
  - sensor.glances_swap_used
  - sensor.glances_sleeping
  - sensor.glances_disk_free
  - sensor.glances_running
  - sensor.glances_cpu_load
  - sensor.glances_containers_cpu_used
  - sensor.glances_swap_used_percent
  - sensor.glances_ram_used
  - sensor.glances_total
  - sensor.glances_disk_used
  - sensor.glances_cpu_temp
  - sensor.glances_swap_free
  - sensor.glances_thread
  - sensor.glances_cpu_used
  - sensor.glances_ram_free
  - sensor.glances_ram_used_percent

Refresh your lovelace dashboard and see all your new sensors!