Creating a device with multiple sensor entities via MQTT Discovery

Hello,

My goal is to create a device with multiple sensor entities. For example, a barbecue thermometer with 2 temperature sensors. I’m using RTL_433 to generate MQTT messages.

I’ve attempted to follow the guide provided here:

I’ve followed the commands verbatim in the example, just to test out creating a device with multiple sensors. I get 2 entities which receive temperature updates, but there is no device which groups them together.

For reference, a previous poster had this issue and claims to have figured it out, but didn’t leave any specific information. See: How to implement a custom MQTT device (Home Theater Receiver)

I’ve scoured the internet but have been unable to find a good example. There are so many posts and videos using YAML configuration, which supposedly does not support multiple sensors per device.

Any help would be greatly appreciated!

For starters, can someone confirm that example in MQTT Configuration for multiple sensors actually creates a device with multiple sensor entities? I’m not sure what the expected behavior is, as the example doesn’t include results.

I was able to get a single sensor to have numerous state attributes, but I don’t think this is what I want.

Thank you!

1 Like

You can achieve multiple mqtt sensors via yaml without the use of scripts, recent conversation regarding the same, here:

FWIWI, 2 sensors linked to the same device.
The dev object is key.

{
  "uniq_id": "mqtt_sensor.temperature_humidity_sensor_49e0_battery",
  "name": "Temperature Humidity Sensor 49E0 Battery",
  "stat_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_battery/state",
  "json_attr_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_battery/attributes",
  "avty_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_battery/availability",
  "dev_cla": "battery",
  "unit_of_meas": "%",
  "stat_cla": "measurement",
  "dev": {
    "mf": "Xiaomi",
    "mdl": "LYWSD03MMC",
    "name": "Temperature/Humidity Sensor 49E0",
    "ids": [
      "A4:C1:38:EB:49:E0"
    ]
  }
}

{
  "uniq_id": "mqtt_sensor.temperature_humidity_sensor_49e0_voltage",
  "name": "Temperature Humidity Sensor 49E0 Voltage",
  "stat_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_voltage/state",
  "json_attr_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_voltage/attributes",
  "avty_t": "ha_stream/sensor/temperature_humidity_sensor_49e0_voltage/availability",
  "dev_cla": "voltage",
  "unit_of_meas": "V",
  "stat_cla": "measurement",
  "dev": {
    "mf": "Xiaomi",
    "mdl": "LYWSD03MMC",
    "name": "Temperature/Humidity Sensor 49E0",
    "ids": [
      "A4:C1:38:EB:49:E0"
    ]
  }
}

1 Like

Thanks everybody! I was able to modify the script @123 wrote to work for my needs! As @koying mentioned, I think the dev object is key. It would be great to get the MQTT Configuration documentation updated, as I’m sure this is a common desire.

Also, let me apologize for not finding these previous conversations. I searched youtube, google, and some here in this forum, but it was difficult finding relevant information applicable to modern home assistant.

Just to recap what I did:

I created a scripts.yaml configuration file according to this page.

In configuration.yaml I added:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
mqtt: !include mqtt.yaml

In this example, I’m configuring a Home Assistant to read sensor data from Honeywell Security Model 5816 Door/Window Sensor. The output from rtl_433 looks like this:

{
    "time": "2023-06-27 01:02:18",
    "model": "Honeywell-Security",
    "id": 181554,
    "channel": 8,
    "event": 160,
    "state": "open",
    "contact_open": 1,
    "reed_open": 1,
    "alarm": 0,
    "tamper": 0,
    "battery_ok": 1,
    "heartbeat": 0
}

In the new scripts.yaml file, I pasted the following code:

create_sensors:
  alias: "Create sensors via MQTT Discovery"
  sequence:
    - service: mqtt.publish
      data:
        topic: homeassistant/sensor/dw181554reed/config
        retain: true
        payload: >
          {
            "name": "DW181554 Reed",
            "unique_id": "dw181554_reed",
            "state_topic": "rtl_433/Honeywell-Security/181554",
            "value_template": {% raw %}"{{ value_json.reed_open }}"{% endraw %},
            "device": {
                "identifiers": ["dw181554"],
                "name": "DW181554",
                "model": "Model 5816",
                "manufacturer": "Honeywell Security"
            }
          }

    - service: mqtt.publish
      data:
        topic: homeassistant/sensor/dw181554tamper/config
        retain: true
        payload: >
          {
            "name": "DW181554 Tamper",
            "unique_id": "dw181554_tamper",
            "state_topic": "rtl_433/Honeywell-Security/181554",
            "value_template": {% raw %}"{{ value_json.tamper }}"{% endraw %},
            "device": {
                "identifiers": ["dw181554"],
                "name": "DW181554",
                "model": "Model 5816",
                "manufacturer": "Honeywell Security"
            }
          }

    - service: mqtt.publish
      data:
        topic: homeassistant/sensor/dw181554contact/config
        retain: true
        payload: >
          {
            "name": "DW181554 Contact",
            "unique_id": "dw181554_contact",
            "state_topic": "rtl_433/Honeywell-Security/181554",
            "value_template": {% raw %}"{{ value_json.contact_open }}"{% endraw %},
            "device": {
                "identifiers": ["dw181554"],
                "name": "DW181554",
                "model": "Model 5816",
                "manufacturer": "Honeywell Security"
            }
          }

    - service: mqtt.publish
      data:
        topic: homeassistant/sensor/dw181554battery/config
        retain: true
        payload: >
          {
            "name": "DW181554 Battery",
            "unique_id": "dw181554_battery",
            "state_topic": "rtl_433/Honeywell-Security/181554",
            "value_template": {% raw %}"{{ value_json.battery_ok }}"{% endraw %},
            "device": {
                "identifiers": ["dw181554"],
                "name": "DW181554",
                "model": "Model 5816",
                "manufacturer": "Honeywell Security"
            }
          }

    - service: mqtt.publish
      data:
        topic: homeassistant/sensor/dw181554heartbeat/config
        retain: true
        payload: >
          {
            "name": "DW181554 Heartbeat",
            "unique_id": "dw181554_heartbeat",
            "state_topic": "rtl_433/Honeywell-Security/181554",
            "value_template": {% raw %}"{{ value_json.heartbeat }}"{% endraw %},
            "device": {
                "identifiers": ["dw181554"],
                "name": "DW181554",
                "model": "Model 5816",
                "manufacturer": "Honeywell Security"
            }
          }

The script can then be found in Home Assistant Settings > Automations & Scenes > Scripts. Just run it.

And the resulting device looks like this, with an entity for each sensor:

1 Like

For future reference, it’s customary to assign the Solution tag to the post that answers/solves the original question/problem. In this situation, you used the supplied answer and example, then marked your own post with the tag. If everyone did that, all topics would end with the author “solving” their own question/problem.

FAQ guideline 21

Understood. There was an error in the post you linked that required adding the {% raw %} / {% endraw %} tags, which I included in the solution here. I’m just trying to make it easy for others to find the most concise solution. I :heartpulse: your post.

Here’s an issue to update the MQTT Configuration documentation:

(someone else beat me to it)

I just wanted to say thank you, this thread was super informative in helping me do something similar.

One small mention is that the PLC I was working with used a bunch of underscores, dashes and other strange things that made the reference in JSON invalid using the dot method.
For example, where you have:

{{ value_json.reed_open }}

I found it better (and solved all my problems) but using:

{{ value_json['reed_open'] }}

I also had some good results adding a few things to the payload:

            "unit_of_measurement" : "%",
            "icon" :"mdi:water-percent",

Thanks again, it all really helped!