Skip to content

User Guide for Setting Up MQTT Sensors in Home Assistant

telenkov88 edited this page May 31, 2024 · 1 revision

Overview

This guide explains how to set up MQTT sensors in Home Assistant for devices that use MQTT topics in the format /ReefRhythm/<unique_id>/pumpX, where X can range from 1 to 9 for devices with multiple pumps. Prerequisites

  • Home Assistant with MQTT integration configured.
  • Access to the Home Assistant configuration files.

Step 1: Configure MQTT Sensors

  • Open your configuration.yaml file. This file is usually located in your Home Assistant configuration directory.
  • Add the following MQTT sensor configurations:
mqtt:
  sensor:
    - name: "<sensor_name>"
      state_topic: "/ReefRhythm/<unique id>/pumpX"
      value_template: "{{ value_json.remain }}"
      unit_of_measurement: 'ml'
      icon: "mdi:cup"
      json_attributes_topic: "/ReefRhythm/<unique id>/pumpX"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}"

    - name: "<sensor_name> Free Memory"
      state_topic: "/ReefRhythm/<unique id>/free_mem"
      value_template: "{{ value_json.free_mem }}"
      unit_of_measurement: 'kB'
      icon: "mdi:memory-arrow-down"
      json_attributes_topic: "/ReefRhythm/<unique id>/free_mem"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}_free_mem"
  • Replace <sensor_name> with the actual device name.
  • Replace <unique_id> with the actual device ID and pumpX with the appropriate pump number.

Step 2: Create Template Sensors

  • Navigate to the sensors.yaml file (make sure this file is included in your configuration.yaml via sensor: !include sensors.yaml).
  • Add template sensors for attributes like dose and storage for each pump:
- platform: template
  sensors:
    <sensor_name>_pumpX_dose:
      friendly_name: "<sensor_name> PumpX Dose"
      unit_of_measurement: 'ml'
      value_template: "{{ state_attr('sensor.<sensor name>_pumpX', 'dose') }}"
    <sensor_name>_pumpX_storage:
      friendly_name: "<sensor_name> PumpX Storage"
      unit_of_measurement: 'ml'
      value_template: "{{ state_attr('sensor.<sensor name>_pumpX', 'storage') }}"

  • Replace sensor_name with the actual device name.
  • Replace pumpX with each pump number as needed.

Step 3: Validate and Restart Home Assistant

  • Validate your configuration in Home Assistant via Developer tools > Yaml > Check Configuration.
  • Restart Home Assistant to apply the changes via Developer tools > Yaml > Restart > Restart Home Assistant.

Step 4: Accessing Sensor Data

  • After restarting, you can access the sensor data through the Developer Tools > States in Home Assistant.
  • You can also add these sensors to your Lovelace dashboard for easy monitoring.

Notes

  • Ensure that the MQTT topics correctly match the topic structure provided by your devices.
  • Modify unit_of_measurement and other sensor properties according to the actual data provided by your device.

This setup allows you to maintain a structured approach to configuring multiple devices and pumps in Home Assistant, making your system scalable and easy to manage.

Configuration example

1-Head doser with unique ID /ReefRhythm/dcda0c190c18/ are dosin Limewater to aquarium. Screenshot 2024-05-16 134415

configuration.yaml:

sensor: !include sensors.yaml

mqtt:
  sensor:
    - name: "Limewater"
      state_topic: "/ReefRhythm/dcda0c190c18/pump1"
      value_template: "{{ value_json.remain }}"
      unit_of_measurement: 'ml'
      icon: "mdi:cup"
      json_attributes_topic: "/ReefRhythm/dcda0c190c18/pump1"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}"

    - name: "Limewater Free Memory"
      state_topic: "/ReefRhythm/dcda0c190c18/free_mem"
      value_template: "{{ value_json.free_mem }}"
      unit_of_measurement: 'kB'
      icon: "mdi:memory-arrow-down"
      json_attributes_topic: "/ReefRhythm/dcda0c190c18/free_mem"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}_free_mem"

sensors.yaml:

- platform: template
  sensors:
    limewater_dose:
      friendly_name: "Limewater Dose"
      unit_of_measurement: 'ml'
      icon_template: "{{ 'mdi:water-pump' }}"
      value_template: "{{ state_attr('sensor.limewater', 'dose') }}"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}_dose"
    limewater_storage:
      friendly_name: "Limewater Storage"
      unit_of_measurement: 'ml'
      icon_template: "{{ 'mdi:cup' }}"
      value_template: "{{ state_attr('sensor.limewater', 'storage') }}"
      unique_id: "doser_{{ value_json.name | replace(' ', '_') | regex_replace('[^a-z0-9_]', '', ignorecase=True) | lower }}_storage"

Overview in Home Assistant:

Screenshot 2024-05-16 134747 Screenshot 2024-05-16 134828 Screenshot 2024-05-16 134820 Screenshot 2024-05-16 134811