home-assistant.io/source/_posts/2017-03-28-http-to-mqtt-bridge.markdown
Ashton Campbell 9e6b9cb658 Fixed common spelling mistakes (#3544)
* fix spelling errors

* Update binary_sensor.xiaomi_aqara.markdown

Reverts to previous revision before spell check.

* Update tellstick.markdown

Reverts to previous revision before spell check.

* Update owntracks_two_mqtt_broker.markdown

Reverts to previous revision before spell check.

* Update cla_sign.html

Reverts to previous revision before spell check.

* Update credits.markdown

Reverts to previous revision before spell check.

* Update api.markdown

Fixed spell checker changing noone to no one.
2017-10-08 00:39:32 +02:00

4.3 KiB

layout, title, description, date, date_formatted, author, comments, categories, og_image
layout title description date date_formatted author comments categories og_image
post HTTP to MQTT bridge How to integrate IFTTT with HA using MQTT 2017-03-28 06:00:00 +0000 March 28, 2017 petkov true How-To /images/blog/2017-03-bridge/social.png

The idea of creating HTTP to MQTT bridge appeared when I was trying to integrate Google Assistant with my Home Assistant after watching BRUH Automation video. Right now there is no MQTT service available in IFTTT. Existing integration solution uses Maker Webhooks which requires that your Home Assistant instance is publicly accessible, which I think brings some security concerns or simply not always possible to set up.

The HTTP to MQTT bridge should fill that gap. The idea is to receive messages using HTTP requests and transfer them to your MQTT broker, which can be contacted by Home Assistant. The HTTP to MQTT bridge is written using Node.js with Express for the server part and MQTT.js for the client.

The app could be hosted on any Node.js hosting. I prefer Heroku: Cloud Application Platform for its simplicity.

{% linkable_title Bringing pieces together %}

  1. Configure the Home Assistant MQTT trigger.
  2. Configure CloudMQTT. Check this video tutorial for details.
  3. Deploy HTTP to MQTT bridge app.
  4. Add the Configuration Variables to your Heroku app mentioned here.
    • AUTH_KEY: Can be any string, eg. 912ec803b2ce49e4a541068d495ab570.
    • MQTT_HOST: The host of your MQTT broker, eg. mqtts://k99.cloudmqtt.com:21234.
    • MQTT_USER: MQTT username
    • MQTT_PASS: MQTT password
  5. Create an IFTTT applet the same way as described in BRUH Automation video.
  6. Configure Maker Webhooks service with below parameters.
    • URL: https://<app_name>.herokuapp.com/post/
    • Method: POST
    • Content Type: application/json
    • Body: {"topic":"<mqtt_topic>","message":"<mqtt_message>","key":"<AUTH_KEY>"}

{% linkable_title Subscribe to latest version %}

Additionally you can make Heroku to update the HTTP to MQTT bridge app to the latest available version from the GitHub repository automatically. To do this follow the instruction on the Heroku help page.

{% linkable_title Improve response time %}

After 30 minutes of inactivity Heroku will put your app into sleep mode. This will result in ~10 seconds response time. To prevent Heroku from putting your app into sleep mode, ping it every 10 minutes. You can do that by sending regular HTTP GET request to http://your_app/keep_alive/. But be careful. Heroku free quota is 550 hours per month. Without sleeping your app will be allowed to run only 22 days a month. Additionally the keep_alive method will send a simple MQTT message to prevent the broker from sleeping as well. The topic and message can be configured using Heroku environment variables KEEP_ALIVE_TOPIC and KEEP_ALIVE_MESSAGE and both are set to "keep_alive" by default.

You can even configure Home Assistant to ping HTTP to MQTT bridge every 10 minutes during daytime. Below is an example of how to do that:

rest_command:
  http_to_mqtt_keep_alive:
    url: https://<your_app_address>/keep_alive/
    method: get

automation:
  alias: HTTP to MQTT keep alive
  trigger:
    platform: time
    minutes: '/10'
    seconds: 00
  condition:
    condition: time
    after: '7:30:00'
    before: '23:59:59'
  action:
    service: rest_command.http_to_mqtt_keep_alive

{% linkable_title Thanks %}

Special thanks to Ben from BRUH Automation for awesome tutorials which inspired me to do this project.