Tracking our home location with Home Assistant

In this post we are going to see something very interesting, And it's nothing more than the ability to track or track certain Bluetooth devices (Smart Bracelets, Mobile phones…) Per house, with the aim of locating the position at all times to carry out automations.

What I said, We all have a mobile phone that follows us everywhere, or we wear a smart bracelet or smartband, All of these devices have Bluetooth, and we can use the MAC address of each device to locate it inside our home. ¿For what? Well, first by statistics, We can visualize how much time we spend and where, It's very curious. Obviously we can do it to put automations, For example, if it is night and you are in the room and the front door to the house is not locked, Well, let it close. When you get to the kitchen in the morning I say good morning, I put the news on you… Or what do I know, to paint us directly on the Floorplan o Animated floor plan of our house, Sitting on the sofa, eating, sleeping… 😉 Eye, that we can also locate our animals, If we put a tracker of these bluetooths, and that if the cat/dog enters a room it should not, Let the speakers at home tell us.

How do we do this? We have to think that certain devices have to be placed in each room of the house, in each room, bath… where we want to locate. And the solution obviously has to speak between each device, to know which one we are closest to.

The best solution personally if you don't have these devices is to rely on ESP32-mqtt-room, that by means of boards based on the ESP32 chip and above all very cheap you can easily distribute and hide around the house. I have to tell you that I used this solution for about a year, In the end I migrated to the solution that we will see today in this post, since I have several Raspberry Pi scattered around the house, It didn't cost me anything to add this functionality. One day if I get bored I make a post for the ESP32-mqtt-room, which is also very cool.

What I said, Today we will see room-assistant, A great solution too, and that we can install and run on our beloved Raspberry Pi, and of course like the previous option, fully integrable into our Home Assistant, so stay tuned!

Installation on Raspbery Pi,

We start! We will follow these steps in each Raspberry Pi that we have and want to install room-assistant, First things first, install NodeJS:

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

Instalamos todas las dependencias que necesitaremos:

Sudo apt-get update && sudo apt-get install -y build-essential libavahi-compat-libdnssd-dev bluetooth libbluetooth-dev libudev-dev libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev

Instalamos room-assistant, class-transformer y class-validator:

sudo npm i --global --unsafe-perm room-assistant sudo npm install i --global class-transformer sudo npm install i --global class-validator

Añadimos los siguientes permisos:

sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
sudo setcap cap_net_raw+eip $(eval readlink -f `which hcitool`)
sudo setcap cap_net_admin+eip $(eval readlink -f `which hciconfig`)

Sinceramente no recuerdo muy bien porqué hice lo siguiente, pero lo tengo documentado 🙂

sudo nano /etc/avahi/avahi-daemon.conf enable-dbus=yes

We create the directory and the configuration file for room-assistant (Be careful, we then fill in the configuration file, Just so you know where it is):

mkdir -p ~/room-assistant/config touch ~/room-assistant/config/local.yml

Before you start with the configuration file, Let's create the service to boot room-assistant:

sudo nano /etc/systemd/system/room-assistant.service

And we introduce the following configuration to the service:

[Unit]
Description=room-assistant service

[Service]
ExecStart=/usr/bin/room-assistant WorkingDirectory=/home/pi/room-assistant Restart=always RestartSec=10 User=pi

[Install]
WantedBy=multi-user.target

We enable the service, We started it (Well, before starting it you must have the configuration file for your room-assistant that we will see below) and we verify that it is torn off:

sudo systemctl enable room-assistant.service sudo systemctl start room-assistant.service sudo systemctl status room-assistant

Room-assistant configuration file, of individual example, that is, a Raspberry Pi working individually, without talking to the rest of the Raspberrys, which we will give a name in 'instanceName', we will add the MAC addresses of the bluetooth devices to be tracked or tracked in 'addresses', and important, we will indicate our MQTT broker server, to send you the data:

global:
  instanceName: Garage integrations:
    - homeAssistant
    - bluetoothClassic homeAssistant:
  mqttUrl: MQTT://DIRECCIÓN_IP_BROKER_MQTT:1883
  mqttOptions:
    username: USERNAME password: bluetoothClassic PASSWORD:
  minRssi: -20
  addresses:
    - 'FD:72:03:6B:BC:XX'
    - 'EA:4B:B2:2To:56:XX'
    - '7C:2F:80:EC:61:XX'
    - 'B4:C4:FC:64:19:XX'

And here is a Example in cluster mode of the room-assistant configuration file, which will be the one we use when we have more than one Raspberry Pi with room-assistant. Unlike the previous one, in this one we will have to add the IP address of each Raspberry Pi in peerAddresses:

global:
   cluster:
     autoDiscovery: False port: 6425
     peerAddresses:
       - DIRECCION_RASPBERRY_PI_1:6425
       - DIRECCION_RASPBERRY_PI_2:6425
       - DIRECCION_RASPBERRY_PI_3:6425
       - DIRECCION_RASPBERRY_PI_4:6425
       - DIRECCION_RASPBERRY_PI_5:6425
   instanceName: Kitchen integrations:
     - homeAssistant
     - bluetoothClassic homeAssistant:
   mqttUrl: MQTT://DIRECCIÓN_IP_BROKER_MQTT:1883
   mqttOptions:
     username: USERNAME password: bluetoothClassic PASSWORD:
   minRssi: -20
   hciDeviceId: 0
   addresses:
    - 'FD:72:03:6B:BC:XX'
    - 'EA:4B:B2:2To:56:XX'
    - '7C:2F:80:EC:61:XX'
    - 'B4:C4:FC:64:19:XX'

Well, Now yes, after recording and having your configuration file, Remember to restart the service (Or demon) by room-assistant.

Home Assistant Settings,

¡Well, we already have it! Now all that remains is to add in our dear. loved and desired Home Assistant the devices we want to visualize, This is, Mobile phones, Bracelets… so let's go to the Home Assistant configuration file, configuration.yaml and in the 'Sensor' section’ We add the following block, indicating the MAC and the description of the device:

sensor:
...
  - Platform: mqtt_room device_id: "b4c4fc6419XX"
    Name: 'Teléfono móvil de Héctor'
    state_topic: 'room_presence'
    timeout: 5
    away_timeout: 0
...

And as always, save the file and restart Home Assistant to load the configuration. We will be able to add each Entity that we have registered easily with the mouse filtering by 'room_presence' type devices.

And nothing, Let the magic run, we will see how depending on where we are, we will see in this history the state of the Entity and its positioning.

If you go too far and you have a Grafana lying around, You can visualize, for example, with a view of the cheese in a very simple way the times, And of course, This has a history… To the gossips, I've taken away the time I spend in the bathroom, hehe, And with this kind of query we would have it:

SELECT state as metric, TIMESTAMPDIFF(SECOND, min(last_updated), Max(last_updated)) as value, last_changed as time_sec FROM states WHERE $__timeFilter(created) and state = 'Cocina' and entity_id = 'sensor.maixu_dordoka_room_presence' GROUP BY last_changed ORDER BY created DESC ;

And well, It's time to look at an example of automation, In the images you will see it easy, in this case it is, at night, When my txabala or I are in bed, if the door has not been locked, Well, close it, I also notified it through the speakers at home, Send a Telegram, blah, blah…

As always a pleasure, I hope you enjoy it, A hug and thank you very much for those likes on social networks that cost nothing!! 🙂

Recommended Posts

Author

nheobug@bujarra.com
Autor del blog Bujarra.com Cualquier necesidad que tengas, Do not hesitate to contact me, I will try to help you whenever I can, Sharing is living ;) . Enjoy documents!!!