Wednesday, 31 August 2016

Using ESPEasy with Home Assistant via MQTT

Preface: I've just started playing around with Home Assistant on a Raspberry Pi and exploring the world of MQTT to control devices on the network. Learning curve is a bit steep but worth the effort as MQTT is very fast.

The hardware and software tools I'm using are as follows:
2 x Sonoff relay units
2 x NodeMCU Boards
ESPEasy firmware (must be version 121 or above as that contains the MQTT 'retain' flag option.
Home Assistant software on Raspberry Pi2
MQTT Test Software:
PC: MQTT.fx
Android: MQTT Dashboard



IMPORTANT: To ensure ESPEasy MQTT works with Home Assistant, make sure you set the MQTT protocol to OpenHAB MQTT under the config tab.

I have flashed the Sonoff units with ESPEasy (R121) and there is plenty of info online on how to do that. The code to use with the rules section for relay control via the switch is below. It will also report the relay state via the LED if remotely controlled via MQTT.
on relay#state do
  if [relay#state]=0
    gpio,13,1
  else
    gpio,13,0
  endif
endon
on button#state do
  if [button#state]=0
    gpio,12,0
    gpio,13,1
  else
    gpio,12,1
    gpio,13,0
  endif
endon
Note that the items highlighted refer to devices that have been configured in the devices list below and must match (case sensitive).




To control the relay via MQTT, send /{devicename}/gpio/12 with payload 0 or 1 so for ESP03 it would be:

/ESP03/gpio/12 with payload 1 for on. Note there is no trailing forward slash (/) after the gpio number but there is one preceeding the {devicename} if the ESPEasy defaults are left. Note: The sonoff has the relay connected to gpio 12 and the LED on 13.

If you subscribe to {devicename}/relay/state you get a response back from the sonoff whether the relay is flipped using MQTT or the button (either a 0 or 1).

If you loose access to any ESP8266 device with ESPEasy on it, you can connect to it’s local AP when it goes into AP mode which is normally ESP## and use the passphrase ‘configesp’. Once connected to that AP, you can reach the device on 192.168.4.1 where you can reconfigure the WLAN or MQTT broker information.

How to get feedback using MQTT on a ESP8266 device when you are using /{devicename}/gpio/{pin#}
You do not need to configure anything on ESPEasy for gpio control so if you want to turn a relay on that is configured on gpio 14, you can send either send HTTP or MQTT as shown below:

MQTT:
/{devicename}/gpio/{pin#} with payload of 1 for on or 0 for off.
HTTP:
http://<ESP IP address>/control?cmd=GPIO,<pin>,0
http://<ESP IP address>/control?cmd=GPIO,<pin>,1

If using http, the webpage will return the pin state. If you are using MQTT, you will need to subscribe to {devicename}/status and then by sending the following, you’ll get a json report on that subscription.

Sonoff via MQTT (two ways)
Publish to {devicename}/cmd with the payload gpio,12,0 to turn the relay off (and LED if you have rules set) or gpio,12,1 to turn the relay on. You’ll get the following back on {devicename}/status:


On:
{
"log": "GPIO 12 Set to 1",
"plugin": 1,
"pin": 12,
"mode": "output",
"state": 1
}

Off:
{
"log": "GPIO 12 Set to 0",
"plugin": 1,
"pin": 12,
"mode": "output",
"state": 0
}


You can also publish status,gpio,12 directly to query the gpio pin. This also works directly in the serial console. Just type status,gpio,12 to check the relay state and you’ll get Jason back like this:

>status,gpio,12
{
"log": "",
"plugin": 1,
"pin": 12,
"mode": "output",
"state": 1
}

Many thanks to this post.

I used Sonoff configuration mentioned here which provides feedback on /{devicename}/relay/state.

This was mirrored on my NodeMCU device and the GPIO pin that was used for my relay was configured as a switch under the devices tab. Simply choose the gpio pin you are using for the relay output under the 1st GPIO section as shown below.


If you then subscribe to /{devicename}/relay/state, when you publish a 0 or 1 to /{devicename}/gpio/{pin#}, you will get either a 0 or 1 back. If you have more than one relay, best to call them say relay12 and relay14 (corresponding to the GPIO #) so you know what relay fires if you monitor /{devicename}/+/state.

Note: The downside of configuring the GPIO as a switch input to allow MQTT status reports (at least during my testing) is that if you configure the pin using /{devicename}/pwm/14 and payload from 0 to 1000 for dimming the LED, this will confuse ESPEasy and you’ll get repeating state messages alternating from 0 to 1 and back again until you send pwm 0. Best to configure another GPIO if you want PWM control.

The full ESPEasy command reference is available here.

And lastly, below is an extract from my swiches.yaml file which shows how Home Assistant is configured with MQTT

# switch 1:
- platform: mqtt
  name: "GPIO14"
  state_topic: "/ESP01/relay14/state"
  command_topic: "/ESP01/gpio/14"
  payload_on: "1"
  payload_off: "0"
  qos: 1
  retain: true
 
# switch 2:
- platform: mqtt
  name: "Sonoff1"
  state_topic: "/sonoff1/relay/state"
  command_topic: "/sonoff1/gpio/12"
  payload_on: "1"
  payload_off: "0"
  qos: 1
  retain: true
 
# switch 3:
- platform: mqtt
  name: "Sonoff2"
  state_topic: "/sonoff2/relay/state"
  command_topic: "/sonoff2/gpio/12"
  payload_on: "1"
  payload_off: "0"
  qos: 1
  retain: true
 
# switch 4:
- platform: mqtt
  name: "Sonoff3"
  state_topic: "/sonoff3/relay/state"
  command_topic: "/sonoff3/gpio/12"
  payload_on: "1"
  payload_off: "0"
  qos: 1
  retain: true

I trust this has been helpful in some way as it took me many weeks to get here but so far it is working well and rock solid.

Oooh, and my advanced settings page from my ESPEasy R121 is shown below (user request)