27 December, 2018

Teaching Home Assistant to serve you coffee and work over MQTT

Configuring Home Assistant platform to trigger actions based on the location information received from the flespi devices.

Have you ever dreamt of a home not just cozy but functional to the point of nearly reading your thoughts and anticipating your wishes? Say, you come home on a frosty winter evening and treat yourself to a hot drink and a delicious dinner. Smart technologies are converting wishful thinking into a dream come true. Learn how to have the tea made and the dinner cooked right when you need it. No cookbook or wife required.

Preface: where smart homes are heading

Smart homes are getting smarter and more affordable every year. Experts in the field believe that to become successful players in this industry should build an open system supporting devices by numerous manufacturers.

One of the leaders offering a vast smart home infrastructure is Xiaomi.

The major Chinese brand has been releasing quite a few smart devices this year — smart locks, smart kettles, smart vacuums, and even smart toilets. What’s more, Xiaomi has recently announced its intention to partner with IKEA — a leading manufacturer of household goods producing various lighting modules for a couple of years. 

Along with the proprietary solutions, the market is full of the open-source alternatives. We have already published an article about using flespi MQTT Broker in Domoticz project.

Today we’ll be talking about Home Assistant — a smart home management system written in Python. The configuration files are represented in yaml.

The Home Assistant platform already supports 1239 components for automation. It also includes modules for integration of data from different services and smart home platforms, e.g.: 

  • IFTTT

  • Google Assistant

  • WeMo

  • NEST 

  • Xiaomi (MiJa and Aqara)

  • Apple HomeKit

  • Honeywell

  • IKEA

The platform also supports such DIY kits as Arduino and Raspberry Pi. Moreover, some cars can share data with the smart home — e.g. there are components for BMW and Tesla vehicles.

The home Assistant platform offers 52 modules for presence detection including the ones monitoring devices connected to a router via Wi-Fi (TP-Link, Huawei, Asus, Ubiquity, OpenWRT), tracking devices having the Bluetooth module on, and getting the location data from the GPS monitoring systems (e.g. OwnTracks, Traccar, Google Maps Location Sharing).

This is exactly what we are going to do now — add a component for getting user location data from flespi.

Task: triggering actions in Home Assistant based on device location

Configuring flespi

All messages coming from devices into the flespi platform get into the MQTT broker — a convenient message bus letting you get the data in the “passive” mode — waiting for the broker to deliver it.

You can pick one of the two ways to get the location data from flespi: 

  • First option:

    1. Connect your device to a flespi channel operating over the corresponding protocol

    2. Subscribe to the topic flespi/message/gw/channels/{channel_id}/{ident} with the target channel id and device ident

  •     Second option:

    1. Connect your device to a channel (like in a) above) 

    2. Create a virtual flespi device and specify its ident

    3. Subscribe to the topic flespi/message/gw/devices/{device_id} with the proper device id.

The difference is that a flespi device (second option) allows storing messages for up to ten years while a flespi channel is a pipeline for converting messages from diverse devices into the unified JSON and is not intended for long-term storage.

As an easy-to-reproduce example, we will show how to set up a Telegram bot to send messages to a flespi channel. However, you are free to use any software or hardware tracker of your liking supported by flespi.

Let’s create a flespi channel working via a Telegram bot.

flespi telegram channel

In the channel settings, we specify the token of the Telegram bot that you created via botfather.

flespi telegram bot token

If you did everything right, send the location to the chat with this Telegram bot and you will immediately see the received message in the channel log:

flespi telegram channel log

Now for the purposes of storing the message (location) history, we’ll create a virtual flespi device:

The key thing to be done at this step is correctly picking the device_type and ident. In our case it’s Telegram and Dexif respectively:

flespi device configuration

If you send another location message to the bot now, you’ll be able to see the current device location on the map:

flespi trackit

Besides, you can already see the messages by connecting to the MQTT broker using MQTT Board:

mqtt board messages

Configuring Home Assistant

In our case, Home Assistant is set up on Raspberry Pi Zero W (with built-in Wi-Fi). The project website offers a detailed configuration guide so I won’t duplicate it here.

Unfortunately, Home Assistant does not have a ready-made component to receive data from flespi. So we wrote a quick and dirty device_tracker component ourselves using Python.

Copy or symlink the entire mqtt_flespi_message folder from the repository into the config/custom_components folder. 

Then we have to configure the component to listen to particular topics. Open configuration.yaml and add the subscriptions to the topics into the device_tracker section in the following format:

device_tracker:
  — platform: mqtt_flespi_message
    devices:
      # flespi channel
      dexif: 'flespi/message/gw/channels/7730/Dexif'
      another: 'flespi/message/gw/channels/7730/another'
      # flespi device
      dexif2: 'flespi/message/gw/devices/173073'

You can also add the known devices into the known_devices.yaml file (otherwise they will appear there automatically).

dexif:
  hide_if_away: false
  icon:
  mac:
  name: dexif
  picture:
  track: true
  gravatar: blah@blah.blah

home assistant map flespi

Reaping the benefits

At the beginning of the article, I mentioned the idea about tea and dinner. Now you are just a few steps away from having a previously filled cooker (e.g. hassio component for Xiaomi rice cooker) start automatically once you leave the “work” geo-zone and having a kettle or coffee machine prepare an aromatic drink right before you get home. Once you enter the “home” geo-zone the garlands turn on and the light new-year music starts to play (it’s the holiday season after all). 

You come home, drink a perfect cup of coffee, put the freshly cooked meal on the plate, and… enjoy life ;)

Bonus

If you’ve chosen to get messages from the flespi device, we have a pleasant bonus.

Home Assistant allows adding any resources into its control panel as links. This way we will add TrackIt into Home Assistant and will be able to see the device tracks on the map.

panel_iframe:
  trackit:
    title: TrackIt
    icon: mdi:map-search
    url: https://trackit.flespi.io/#/login/{Your_FlespiToken}

home assistant trackit track

Make the advanced technology add a personal touch to your everyday life.