MQTT Integration Guide

Publish sensor data from Just Sensors to Home Assistant, Grafana, and more

Overview

Just Sensors can publish sensor data to any MQTT broker, enabling integration with home automation systems, data visualization platforms, and custom applications. MQTT (Message Queuing Telemetry Transport) is a lightweight messaging protocol ideal for IoT and sensor data.

๐Ÿ”Œ
Compatible Platforms
  • Home Assistant โ€” Native MQTT integration with auto-discovery
  • Grafana โ€” Via Telegraf + InfluxDB for time-series visualization
  • Node-RED โ€” Visual flow-based automation
  • openHAB โ€” Open-source home automation
  • AWS IoT Core / Azure IoT Hub โ€” Cloud IoT platforms
  • Custom applications โ€” Any MQTT client library

Topic Structure

Sensor data is published to hierarchical topics that make it easy to subscribe to specific sensors or all data from a device.

{base_topic}/{device_id}/{sensor_type}
Component Description Example
base_topic Configurable prefix (default: "justsensors") justsensors
device_id First 8 characters of device vendor ID a1b2c3d4
sensor_type Sensor name in snake_case accelerometer

Example Topics

justsensors/a1b2c3d4/accelerometer
justsensors/a1b2c3d4/battery
justsensors/a1b2c3d4/location
justsensors/a1b2c3d4/sound_level
justsensors/a1b2c3d4/session  # Session metadata

Wildcard Subscriptions

Use MQTT wildcards to subscribe to multiple topics:

# All sensors from a specific device
justsensors/a1b2c3d4/#

# Specific sensor from all devices
justsensors/+/battery

# All data
justsensors/#

Payload Format

All payloads are JSON-formatted with an ISO8601 timestamp and sensor-specific fields.

Accelerometer Example

{
  "timestamp": "2025-02-05T10:30:00Z",
  "sensor_type": "accelerometer",
  "x": 0.012,
  "y": -0.984,
  "z": 0.021
}

Battery Example

{
  "timestamp": "2025-02-05T10:30:00Z",
  "sensor_type": "battery",
  "level": 0.85,
  "state": "charging"
}

Location Example

{
  "timestamp": "2025-02-05T10:30:00Z",
  "sensor_type": "location",
  "latitude": 37.7749,
  "longitude": -122.4194,
  "altitude": 10.5,
  "speed": 2.3,
  "horizontalAccuracy": 5.0
}

Quality of Service (QoS)

MQTT supports three levels of delivery guarantee. Choose based on your reliability needs and network conditions.

QoS 0

At Most Once Fast

Fire and forget. Message sent once with no acknowledgment. Best for high-frequency sensor streams where occasional loss is acceptable.

QoS 1

At Least Once Recommended

Acknowledged delivery. Publisher retries until acknowledged. May deliver duplicates. Best balance of reliability and performance.

QoS 2

Exactly Once Reliable

Four-step handshake ensures single delivery. Higher latency. Best for critical messages where duplicates would cause issues.

Recommended QoS by Sensor Type

Sensor Type Recommended QoS Reason
Accelerometer, Gyroscope QoS 0 High frequency, loss acceptable
Battery, Brightness QoS 1 Periodic updates, should be reliable
Location QoS 1 Important for tracking
Session metadata QoS 1 Should not be lost

Home Assistant Setup

Home Assistant has native MQTT integration that makes it easy to receive sensor data from Just Sensors.

1

Install an MQTT Broker

Install Mosquitto as a Home Assistant add-on, or use an external broker.

# In Home Assistant, go to:
Settings โ†’ Add-ons โ†’ Add-on Store โ†’ Mosquitto broker โ†’ Install
2

Enable MQTT Integration

Add the MQTT integration in Home Assistant.

# In Home Assistant, go to:
Settings โ†’ Devices & Services โ†’ Add Integration โ†’ MQTT
3

Configure Sensor Entities

Add MQTT sensors to your configuration.yaml:

mqtt:
  sensor:
    # iPhone Battery Level
    - name: "iPhone Battery"
      state_topic: "justsensors/+/battery"
      value_template: "{{ (value_json.level * 100) | round(0) }}"
      unit_of_measurement: "%"
      device_class: battery
      icon: mdi:cellphone

    # Sound Level
    - name: "iPhone Sound Level"
      state_topic: "justsensors/+/sound_level"
      value_template: "{{ value_json.decibels | round(1) }}"
      unit_of_measurement: "dB"
      icon: mdi:microphone

    # Brightness
    - name: "iPhone Brightness"
      state_topic: "justsensors/+/brightness"
      value_template: "{{ (value_json.level * 100) | round(0) }}"
      unit_of_measurement: "%"
      icon: mdi:brightness-6

    # Barometer (Pressure)
    - name: "iPhone Pressure"
      state_topic: "justsensors/+/barometer"
      value_template: "{{ value_json.pressure | round(2) }}"
      unit_of_measurement: "kPa"
      device_class: pressure

  device_tracker:
    # Location Tracking
    - name: "iPhone Location"
      state_topic: "justsensors/+/location"
      json_attributes_topic: "justsensors/+/location"
      source_type: gps
4

Create Automations

Use the sensor data to trigger automations:

automation:
  - alias: "Low iPhone Battery Alert"
    trigger:
      - platform: numeric_state
        entity_id: sensor.iphone_battery
        below: 20
    action:
      - service: notify.notify
        data:
          message: "iPhone battery is low!"

Grafana Setup

Visualize sensor data over time with Grafana, using Telegraf to collect MQTT data and InfluxDB for storage.

1

Install Required Components

Install Telegraf, InfluxDB, and Grafana. Using Docker:

# docker-compose.yml
version: '3'
services:
  influxdb:
    image: influxdb:2.7
    ports:
      - "8086:8086"
    volumes:
      - influxdb-data:/var/lib/influxdb2
  
  telegraf:
    image: telegraf:latest
    volumes:
      - ./telegraf.conf:/etc/telegraf/telegraf.conf:ro
    depends_on:
      - influxdb
  
  grafana:
    image: grafana/grafana:latest
    ports:
      - "3000:3000"
    depends_on:
      - influxdb

volumes:
  influxdb-data:
2

Configure Telegraf

Create telegraf.conf to subscribe to MQTT topics:

# telegraf.conf

# MQTT Consumer Input
[[inputs.mqtt_consumer]]
  servers = ["tcp://your-broker:1883"]
  topics = ["justsensors/#"]
  data_format = "json"
  topic_tag = "topic"
  
  # Parse device_id and sensor from topic
  [[inputs.mqtt_consumer.topic_parsing]]
    topic = "justsensors/+/+"
    tags = "_/device_id/sensor"

# InfluxDB Output
[[outputs.influxdb_v2]]
  urls = ["http://influxdb:8086"]
  token = "your-token"
  organization = "your-org"
  bucket = "sensors"
3

Create Grafana Dashboard

Add InfluxDB as a data source in Grafana, then create panels with Flux queries:

// Battery level over time
from(bucket: "sensors")
  |> range(start: -24h)
  |> filter(fn: (r) => r.sensor == "battery")
  |> filter(fn: (r) => r._field == "level")
  |> map(fn: (r) => ({ r with _value: r._value * 100.0 }))

// Accelerometer magnitude
from(bucket: "sensors")
  |> range(start: -1h)
  |> filter(fn: (r) => r.sensor == "accelerometer")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({
      r with magnitude: math.sqrt(r.x*r.x + r.y*r.y + r.z*r.z)
    }))

Available Sensors

Just Sensors can publish data from the following sensors via MQTT:

Sensor MQTT Topic Key Fields
Accelerometer accelerometer x, y, z (g-force)
Gyroscope gyroscope x, y, z (rad/s)
Magnetometer magnetometer x, y, z (ยตT), heading
Barometer barometer pressure (kPa), relativeAltitude (m)
Device Motion device_motion pitch, roll, yaw, quaternion
Pedometer pedometer steps, distance, floorsAscended
Location location latitude, longitude, altitude, speed
Battery battery level (0-1), state
Sound Level sound_level decibels, peakDecibels
Light Meter light_meter estimatedLux, iso, exposureDuration
Brightness brightness level (0-1)
Proximity proximity isNear (boolean)
Orientation orientation orientation (portrait, landscape, etc.)

Troubleshooting

Connection Failed

  • Verify your broker host and port are correct
  • Check if TLS is required (try port 8883 with TLS enabled)
  • Ensure your device has network connectivity
  • Check firewall rules on your broker server

Authentication Failed

  • Double-check username and password
  • Verify the user has publish permissions
  • Check broker ACL configuration

No Data Appearing

  • Use an MQTT client (e.g., MQTT Explorer) to verify messages are being published
  • Check your subscription topic patterns include the correct device ID
  • Use wildcard subscriptions (justsensors/#) to see all messages
  • Verify the sensor is active and recording

Debugging with MQTT Explorer

Download MQTT Explorer to visualize your topic structure and inspect payloads in real-time.