<![CDATA[Category: MQTT | Home Assistant]]> 2016-03-21T19:41:07+00:00 https://home-assistant.io/ Octopress <![CDATA[Smarter SmartThings with MQTT and Home Assistant]]> 2016-02-09T07:44:00+00:00 https://home-assistant.io/blog/2016/02/09/Smarter-Smart-Things-with-MQTT-and-Home-Assistant This is a guest post by Home Assistant users Jeremiah Wuenschel and St. John Johnson.

So you own a SmartThings Hub. You probably bought it when you were looking to get into the whole Home Automation hobby because it worked with pretty much everything and offered you the ability to automate anything. After a week of ownership, you realized that building dashboards and automating required writing way more Groovy then you expected. Then one day you were browsing reddit and discovered the amazingness that is Home Assistant! A solution that offered dashboards, graphs, working support for Nest, and REAL EASY automation!

You spent your weekend getting everything set up, showing it off to your significant other, but in the end you got stumped when it came to integrating with all your existing SmartThings toys. What do I do now? Should I buy another hub? Should I just buy a Z-Wave stick?

That’s where we came in. We wanted a solution that can bridge the awesomeness of Home Assistant with the SmartThings hub that works with almost everything.

Glossary

This is going to be a pretty detailed tutorial on setting up our SmartThings bridge. However, there are a couple key terms that might be new to you:

  • MQTT: A lightweight message protocol for listening and publishing events that happen. Many home automation platforms have built in support for this (especially Home Assistant).
  • Docker: A tool for running applications that are self-contained. No need for installing any dependencies or worrying about conflicts. Installs easily on Linux and OSX.

Setting up the Bridge

MQTT

Assuming that you already have Home Assistant and Smart Things running, you will first want to get an MQTT broker running. There are a handful of MQTT brokers available in Open Source land. We chose Mosca for its simplicity.

There is very little you need to do to get Mosca running. The easiest approach is to use Docker, and run a command like the following:

$ docker run \
    -d \
    --name="mqtt" \
    -v /opt/mosca:/db \
    -p 1883:1883 \
    matteocollina/mosca

This will start Mosca up inside of a docker container, while keeping persistent storage for Mosca in /opt/mosca. The default configuration is the only thing we need to get things up and running.

If you don’t want to mess with Docker and can get node.js installed without trouble, the standalone instructions are all you need.

MQTT Bridge

This is the small piece of magic that bridges the gap between MQTT and SmartThings. It is a node.js app, and like Mosca it is probably easiest to install with Docker:

$ docker run \
    -d \
    --name="mqtt-bridge" \
    -v /opt/mqtt-bridge:/config \
    -p 8080:8080 \
    stjohnjohnson/smartthings-mqtt-bridge

The code for this bridge is on Github if you want to start it up independently.

The MQTT Bridge only needs to know where your MQTT broker lives. If you are using these docker commands as-is, edit /opt/mqtt-bridge/config.yml to look like this:

---
mqtt:
    host: <IP of the host>

Restart the bridge, and you are ready to go:

$ docker restart mqtt-bridge

SmartThings Device

The next step (and possibly the most confusing) is the device type. Go to the Smart Things Device IDE and Create New Device Handler. Choose From Code and paste in the MQTT Bridge Device Code. Click Save, Publish, and then For Me.

Now to install your new Device Handler. Go back to My Devices in the IDE, and click New Device. Enter a name, and pick any random set of characters for the Device Network Id (this will automatically update later). For Type, scroll to the bottom of the list and find your newly created MQTT Bridge. Fill in the other boxes however you like.

Go back to My Devices, and click on your new device in the list. This will bring up a page that allows you to edit your device’s Preferences. Click edit and fill in the 3 pieces of information it asks for.

  • MQTT Bridge IP Address: <IP address of the MQTT Bridge from the previous step>
  • MQTT Bridge Port: <8080 if you have changed nothing in the previous commands>
  • MQTT Bridge MAC Address: <Mac address of machine running the Bridge code>

This will create the link between SmartThings and the MQTT Bridge.

SmartThings App

The last step is to setup the SmartApp. After this, any registered devices will start sending their events to MQTT.

Go to the Smart App IDE. Click New SmartApp, followed by From Code. Paste in the MQTT Bridge SmartApp code and click Save. Click Publish and then For Me. In the SmartThings mobile app, add the new SmartApp and configure it with your devices and MQTT Bridge device. Clicking done will subscribe SmartThings to your MQTT broker and begin 2-way propagation of events.

Configure Home Assistant

To add SmartThings devices to Home Assistant over MQTT, first enable MQTT in Home Assistant:

mqtt:
  broker: localhost

Replace localhost with the location of the running MQTT Broker. Devices from the MQTT Bridge are published to the path smartthings/<Device Name>/<Atribute>

For example, my Dimmer Z-Wave Lamp is called “Fireplace Lights” in SmartThings. The following topics are published:

Topic Description
smartthings/Fireplace Lights/level Brightness (0-99)
smartthings/Fireplace Lights/switch Switch State (on/off)

Here is an example Home Assistant config:

switch:
  platform: mqtt
  name: "Fireplace Lights"
  state_topic: "smartthings/Fireplace Lights/switch"
  command_topic: "smartthings/Fireplace Lights/switch"
  brightness_state_topic: "smartthings/Fireplace Lights/level"
  brightness_command_topic: "smartthings/Fireplace Lights/level"
  payload_on: "on"
  payload_off: "off"
  retain: true

We recommend retain: true for every MQTT device in order to keep states in sync when things become disconnected.

Start digging through the MQTT Components in Home Assistant to find which components map to the new events being published to MQTT.

Configuring with Docker-Compose

Our personal preference for starting the whole suite of software is to use a single Docker-Compose file. Just create a file called docker-compose.yml like this:

mqtt:
    image: matteocollina/mosca
    ports:
        - 1883:1883

mqttbridge:
    image: stjohnjohnson/smartthings-mqtt-bridge
    volumes:
        - ./mqtt-bridge:/config
    ports:
        - 8080:8080
    links:
        - mqtt

homeassistant:
    image: balloob/home-assistant
    ports:
        - 80:80
    volumes:
        - ./home-assistant:/config
        - /etc/localtime:/etc/localtime:ro
    links:
        - mqtt

This will start home-assistant, MQTT, and the Bridge, in dependency order. All config can reference the name of the docker container instead of using IP addresses (e.g. mqtt for the broker host in Home Assistant).

How it works

HTTP Endpoint: There are really only 2 ways to communicate with the SmartThings hub that we could find. The easiest approach is to create a RESTful SmartApp authenticated with OAuth that provides state changes via HTTP directly. This approach is pretty straightforward to implement, but it requires communication with the SmartThings cloud service, and can’t be done entirely on your LAN. We hoped to keep all communication internal, and came up with a second approach.

Custom Device Type: SmartThings custom device types allow developers to define handlers for HTTP events received directly over the local network by the SmartThings hub. Messages received are authenticated by MAC address, and can contain arbitrary strings in their payload. Since a Device Type is only ever tied to a single device, we need to add a SmartApp to the mix in order to translate events between individual devices and our special Home Assistant Bridge device. Here is what we have so far:

Z-Wave Switch        |
Zigbee motion sensor |<---> Bridge App <---> Bridge Device Type <---> <Local network>
Z-Wave light bulb    |

On the Home Assistant side, there is a powerful platform available based on the MQTT lightweight message bus protocol. Everything from lights to switches to temperature sensors can be defined in Home Assistant as an MQTT component, so it makes for a convenient integration point. This requires an MQTT broker for handling the message bus, and one last piece to translate between the HTTP that SmartThings supports and MQTT.

Here is the final sequence of events:

SmartThings Bridge Sequence SmartThings Bridge Sequence

There are a lot of stops along the way for these events, but each piece is a simple translation layer to shuttle the events between systems.

Future Improvements

  • Raspberry pi: There is a lot of interest in getting this running on the Raspberry Pi. It only requires binaries compiled for ARM, so we plan to get ARM-compatible versions of the containers going at some point.
  • Authentication for MQTT: At the moment, the MQTT bridge doesn’t understand how to authenticate to MQTT, so only unauthenticated MQTT is supported. This is mitigated to some degree if you use our Docker Compose config, because MQTT’s port is not actually shared publicly.
  • Authentication for MQTT Bridge: Right now the bridge expects that anyone subscribing is the SmartThings hub. This could use proper authentication.
]]>
<![CDATA[Report the temperature with ESP8266 to MQTT]]> 2015-10-11T19:10:00+00:00 https://home-assistant.io/blog/2015/10/11/measure-temperature-with-esp8266-and-report-to-mqtt I recently learned about the ESP8266, a $5 chip that includes WiFi and is Arduino compatible. This means that all your DIY projects can now be done for a fraction of the price.

For this tutorial, I’ll walk through how to get going with ESP8266, get the temperature and humidity and report it to MQTT where Home Asssistant can pick it up.

Picture of the final setup (+ 2 LED for decoration)

Home Assistant will keep track of historical values and allow you to integrate it into automation.

Components

I’ve been using Adafruit for my shopping:

Besides this, you will need the usual hardware prototype equipment: a breadboard, some wires, soldering iron + wire, Serial USB cable.

Connections

On your breadboard, make the following connections from your ESP8266 to the HDC1008:

ESP8266 HDC1008
GND GND
3V Vin
14 SCL
#2 SDA

I picked #2 and 14 myself, you can configure them in the sketch.

Preparing your IDE

Follow these instructions on how to install and prepare the Arduino IDE for ESP8266 development.

After you’re done installing, open the Arduino IDE, in the menu click on sketch -> include library -> manage libraries and install the following libraries:

  • PubSubClient by Nick ‘O Leary
  • Adafruit HDC1000

Sketch

If you have followed the previous steps, you’re all set.

  • Open Arduino IDE and create a new sketch (File -> New)
  • Copy and paste the below sketch to the Arduino IDE
  • Adjust the values line 6 - 14 to match your setup
  • Optional: If you want to connect to an MQTT server without a username or password, adjust line 63.
  • To have the ESP8266 accept our new sketch, we have to put it in upload mode. On the ESP8266 device keep the GPIO0 button pressed while pressing the reset button. The red led will glow half bright to indicate it is in upload mode.
  • Press the upload button in Arduino IDE
  • Open the serial monitor (Tools -> Serial Monitor) to see the output from your device

This sketch will connect to your WiFi network and MQTT broker. It will read the temperature and humidity from the sensor every second. It will report it to the MQTT server if the difference is > 1 since last reported value. Reports to the MQTT broker are sent with retain set to True. This means that anyone connecting to the MQTT topic will automatically be notified of the last reported value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <Adafruit_HDC1000.h>

#define wifi_ssid "YOUR WIFI SSID"
#define wifi_password "WIFI PASSWORD"

#define mqtt_server "YOUR_MQTT_SERVER_HOST"
#define mqtt_user "your_username"
#define mqtt_password "your_password"

#define humidity_topic "sensor/humidity"
#define temperature_topic "sensor/temperature"

WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_HDC1000 hdc = Adafruit_HDC1000();

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);

  // Set SDA and SDL ports
  Wire.begin(2, 14);

  // Start sensor
  if (!hdc.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

bool checkBound(float newValue, float prevValue, float maxDiff) {
  return newValue < prevValue - maxDiff || newValue > prevValue + maxDiff;
}

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 1.0;

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  long now = millis();
  if (now - lastMsg > 1000) {
    lastMsg = now;

    float newTemp = hdc.readTemperature();
    float newHum = hdc.readHumidity();

    if (checkBound(newTemp, temp, diff)) {
      temp = newTemp;
      Serial.print("New temperature:");
      Serial.println(String(temp).c_str());
      client.publish(temperature_topic, String(temp).c_str(), true);
    }

    if (checkBound(newHum, hum, diff)) {
      hum = newHum;
      Serial.print("New humidity:");
      Serial.println(String(hum).c_str());
      client.publish(humidity_topic, String(hum).c_str(), true);
    }
  }
}

Configuring Home Assistant

The last step is to integrate the sensor values into Home Assistant. This can be done by setting up Home Assistant to connect to the MQTT broker and subscribe to the sensor topics.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mqtt:
  broker: YOUR_MQTT_SERVER_HOST
  username: your_username
  password: your_password

sensor:
  platform: mqtt
  name: "Temperature"
  state_topic: "sensor/temperature"
  qos: 0
  unit_of_measurement: "ºC"

sensor 2:
  platform: mqtt
  name: "Humidity"
  state_topic: "sensor/humidity"
  qos: 0
  unit_of_measurement: "%"
]]>
<![CDATA[Using MQTT with Home Assistant]]> 2015-09-11T09:19:38+00:00 https://home-assistant.io/blog/2015/09/11/different-ways-to-use-mqtt-with-home-assistant MQTT support was added to Home Assistant recently. The MQTT component will enable you to do all sort of things. Most likely you will use it to communicate with your devices. But Home Assistant doesn’t care where the data is coming from or is limited to real hardware as long as there is MQTT support. This means that it doesn’t matter if the data is coming from a human, a web service, or a device.

A great example is shown in a Laundry Automation post in this blog.

This post will give you a small overview of some other possibilities on how to use MQTT with Home Assistant.

Manual usage

The simplest but not the coolest way as a human to interact with a Home Assistant sensor is launching a command manually. Let’s create a “Mood” sensor. For simplicity Home Assistant and the MQTT broker are both running on the same host. The needed configuration snipplets to add to the configuration.yaml file consists of two parts: one for the broker and one for the sensor.

1
2
3
4
5
6
7
mqtt:
  broker: 127.0.0.1

sensor:
  - platform: mqtt
    name: "Fabian's Mood"
    state_topic: "home-assistant/fabian/mood"

After a restart of Home Assistant the “Mood” sensor will show up in the frontend. For more details about the configuration of MQTT itself and the sensor, please refer to the MQTT component or the MQTT sensor documentation.

Now we can set the mood. The commandline tool (mosquitto_pub) which is shipped with mosquitto is used to send an MQTT message.

1
$ mosquitto_pub  -h 127.0.0.1 -t "home-assistant/fabian/mood" -m "bad"

The Mood sensor

This is a really bad example. Don’t do this in the real world because you won’t be able to create diagrams of historical data. Better use a numerical value.

Python MQTT bindings

The last section was pretty boring, I know. Nobody wants to send MQTT messages by hand if there is a computer on the desk. If you are playing the lottery this section is for you. If not, read it anyway because the lottery is just an example :-).

This example is using the Paho MQTT Python binding because those binding should be available on the host where Home Assistant is running. If you want to use this example on another machine, please make sure that the bindings are installed (pip3 install paho-mqtt).

The first step is to add an additional MQTT sensor to the configuration.yaml file. The sensor will be called “Lottery” and the unit of measurement will be “No.”.

1
2
3
4
  - platform: mqtt
    name: "Lottery"
    state_topic: "home-assistant/lottery/number"
    unit_of_measurement: "No."

Don’t forget to restart Home Assistant to make the configuration active.

To play, we need numbers from 1 to 49 which can be marked on the ticket. Those numbers should be random and displayed in the Home Assistant frontend. The Python script below is another simple example on how to send MQTT messages from the commandline; this time in a loop. For further information and examples please check the Paho MQTT documentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python3
#
import time
import random
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

broker = '127.0.0.1'
state_topic = 'home-assistant/lottery/number'
delay = 5

# Send a single message to set the mood
publish.single('home-assistant/fabian/mood', 'good', hostname=broker)

# Send messages in a loop
client = mqtt.Client("ha-client")
client.connect(broker)
client.loop_start()

while True:
    client.publish(state_topic, random.randrange(0, 50, 1))
    time.sleep(delay)

Every 5 seconds a message with a new number is sent to the broker and picked up by Home Assistant. By the way, my mood is much better now.

The Lottery sensor

With only a few lines of Python and an MQTT broker you can create your own “smartdevice” or send information to Home Assistant which you haven’t think of. Of course this is not limited to Python. If there is an MQTT library available, the device can be used with Home Assistant now.

Arduino

To get started with real hardware that is capable to send MQTT messages, the Arduino platform is an inexpensive way to do it. In this section an Arduino UNO with an Ethernet shield and a photo resistor is used. The photo resistor is connected to analog pin 0 (A0) and has an output from 0 to 1024.

The Arduino UNO with Ethernet shield and photo resistor

The MQTT client for the Arduino needs to be available in your Arduino IDE. Below you will find a sketch which could act as a starting point. Please modify the IP addresses, the MAC address, and the pin as needed and upload the sketch to your Arduino.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
  This sketch is based on the basic MQTT example by
  http://knolleary.github.io/pubsubclient/
*/

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

#define DEBUG 1 // Debug output to serial console

// Device settings
IPAddress deviceIp(192, 168, 0, 43);
byte deviceMac[] = { 0xAB, 0xCD, 0xFE, 0xFE, 0xFE, 0xFE };
char* deviceId  = "sensor01"; // Name of the sensor
char* stateTopic = "home-assistant/sensor01/brightness"; // MQTT topic where values are published
int sensorPin = A0; // Pin to which the sensor is connected to
char buf[4]; // Buffer to store the sensor value
int updateInterval = 1000; // Interval in miliseconds

// MQTT server settings
IPAddress mqttServer(192, 168, 0, 12);
int mqttPort = 1883;

EthernetClient ethClient;
PubSubClient client(ethClient);

void reconnect() {
  while (!client.connected()) {
#if DEBUG
    Serial.print("Attempting MQTT connection...");
#endif
    if (client.connect(deviceId)) {
#if DEBUG
      Serial.println("connected");
#endif
    } else {
#if DEBUG
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
#endif
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(57600);
  client.setServer(mqttServer, mqttPort);
  Ethernet.begin(deviceMac, deviceIp);
  delay(1500);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  int sensorValue = analogRead(sensorPin);
#if DEBUG
  Serial.print("Sensor value: ");
  Serial.println(sensorValue);
#endif
  client.publish(stateTopic, itoa(sensorValue, buf, 10));
  delay(updateInterval);
}

The Arduino will send the value of the sensor every second. To use the data in Home Assistant, add an additional MQTT sensor to the configuration.yaml file.

1
2
3
4
  - platform: mqtt
    name: "Brightness"
    state_topic: "home-assistant/sensor01/brightness"
    unit_of_measurement: "cd"

After a restart of Home Assistant the values of your Arduino will be available.

The Brightness sensor

I hope that this post could give you some ideas about the usage Home Assistant and MQTT. If you are working on a cool project that includes Home Assistant, please let us now.

]]>