Home Assistant is best described by the project themselves
Open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server.
As an avid self-hosting and chronic time waster, Hoem Assistant is a gold mine of little projects here an there that balloon into a massive and critical part of my home. I dread the day the batteries run out on my motion sensors and I have to manually turn the lights on. As such a critical part of my home infrastructure, I've come to have a thought nearly anytime I do a project. How does this integrate into Home Assistant?
For Christmas my lovely wife was nice enough to get me a subscription to a VPS so I could host a blog and run a few other things in the cloud. As the paranoid self-hoster I am, I wanted to get notifications for SSH Logins and loop those into Home Assistant for extra convenience. Solving this problem was WAY easier than I imagined. With a short script and a little data formatting, I was able to create a Logbook Card on my dashboard that showed recent activity.
In Home Assistant - Node-Red
We'll be using a webhook to receive data from the server on Home Assistant. To do this, there are two options. Using Node-Red, or using yaml. A long time ago I decided to shift all my automation from yaml to Node-Red so that is the path I recommend and will go over here. However you can accomplish the same using yaml with a combination of webhooks and Data Templating.
On The Server
On the server you'd like to recieve notifications for you need to create/edit
/etc/ssh/sshrc This file can be used to execute scripts on ssh login and is a built in functionality. Open in
sshrc and paste in a version of the code below, while replacing the relevent information and formatting the message how you prefer.
WARNING: If you use Ansible to work on the server, after doing this and running an Ansible script, you may experience a bombardment of notifications.
Setting up the Card
Thanks to the Logbook Card, setup is easy and can be done from the UI. Edit the Lovelace dashboard you'd like to add the card to. Select Add Card and choose the Logbook Card. There you can select the sensor you created in the previous step, how many hours to show, and some other configuration information.
type: logbook entities: - sensor.ssh_login hours_to_show: 24
Adding SSH logging to Home Assistant is super easy to do and can offer a tiny sliver of reassurance that your whole system isn't burning down by an unauthorized user.