MQTT Discovery and Node-Red

I am trying to publish MQTT information from Node-RED to my Home Assistant setup but I am not getting it to work. Today I have a camera publishing MQTT to HA already and this works just fine.

Now I have created a flow in Node-RED that has exit to publish towards MQTT. My setup in Node-RED looks like this:

Debugging gives me updates just like I want (on the msg.payload), so the setup works. I have configured the various MQTT publish exits like follows:
image

image

I have tried some few changes like crossing ā€œclean sessionā€ and ā€œuse legacy 3.1 supportā€ but I get no changes. On my raspberry I find the following in my MQTT log:

1548140394: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140394: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140394: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140394: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140396: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140396: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140396: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140396: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140398: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140398: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140398: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140398: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140400: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140400: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/currentā€™, ā€¦ (5 bytes))
1548140400: Received PUBLISH from nodered (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140400: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜homeassistant/sensor/batteri/socā€™, ā€¦ (2 bytes))
1548140401: New connection from 192.168.1.100 on port 1883.
1548140401: New client connected from 192.168.1.100 as mosqpub|22608-sofia-sal (c1, k60, uā€™mqttuserā€™).
1548140401: Sending CONNACK to mosqpub|22608-sofia-sal (0, 0)
1548140401: Received PUBLISH from mosqpub|22608-sofia-sal (d0, q0, r1, m0, ā€˜sofia/salong/leds/blueā€™, ā€¦ (2 bytes))
1548140401: Sending PUBLISH to mosqsub|269-sofia-salon (d0, q0, r0, m0, ā€˜sofia/salong/leds/blueā€™, ā€¦ (2 bytes))
1548140401: Sending PUBLISH to 79f15147-3923-47ce-bdb1-ae98303d44f9 (d0, q0, r0, m0, ā€˜sofia/salong/leds/blueā€™, ā€¦ (2 bytes))
1548140401: Received DISCONNECT from mosqpub|22608-sofia-sal
1548140401: Client mosqpub|22608-sofia-sal disconnected.

The ā€œnoderedā€ lines does not work, but the ā€œsofia/salongā€ ones work fine. I have tried the following as-well:

pi@raspberrypi:/var/log/mosquitto $ mosquitto_sub -h localhost -p 1883 -t ā€œhomeassistant/sensor/batteri/socā€
47
47
47
47

So in my simple mind it seems to work! How do I get HA to discover these sensors? Is there some kind of templating I need to do? Do I have to create these ā€œobjectsā€ in MQTT? Sorry for my illiteracy around MQTT, this is not my competence at all! If someone could please point me in the right direction that would be lovely!

Things I have tried:

  • with and witout username
  • enable / disable ā€œallow_anonymousā€ in mosquitto.conf
  • delete integration in HA and recreate, also making sure discovery is checked
  • tried different topics, not sure how the topic part works totally, but every try fails the same way

Hi, I had a similar problem. Auto discover didnā€™t work. I solved it by manually adding my sensors in the HA config.
I used the Chrome MQTT lens extension to check if NR was publishing OK.
Maybe someone else has a better solution?

1 Like

Thanks @EricC, I got it working by manually creating the sensors. Sad that it does not work automatically, but I got it working at least!

Just to confirm, youā€™ve followed the docs for MQTT Discovery and have it enabled in your configuration?

MQTT Discovery relies on 2 pieces.

The config has to be sent over as a JSON object, and then the sensors need to be formatted in the same way you defined in the config.

Just setting the topic to homeassistant/sensor/whatever doesnā€™t automatically add the device using MQTT discovery

I have ā€œhand to godā€ tried to yes. But I suspect that any shortcomming is around this area:

The discovery topic need to follow a specific format:

<discovery_prefix>/<component>/[<node_id>/]<object_id>/<>
  • <component> : One of the supported components, eg. binary_sensor .
  • <node_id> ( Optional ): ID of the node providing the topic.
  • <object_id> : The ID of the device. This is only to allow for separate topics for each device and is not used for the entity_id .
  • <> : The topic config or state which defines the current action.

I have tried many setups of topic but I fail all the time. What I have today is this:

Configuration.yaml
# MQTT Sensors
- platform: mqtt
name: ā€œSpenningā€
state_topic: ā€œhomeassistant/volt/stateā€
unit_of_measurement: ā€˜Vā€™
- platform: mqtt
name: ā€œFyllingsgradā€
state_topic: ā€œhomeassistant/soc/stateā€
unit_of_measurement: ā€˜%ā€™
- platform: mqtt
name: ā€œStrĆømtrekkā€
state_topic: ā€œhomeassistant/current/stateā€
unit_of_measurement: ā€˜Aā€™

I am quite sure this is part of the problem, due to my limited understanding of MQTT and the guide linkedā€¦

If anyone has a way that my topic setup should be configured I am very happy for input! :slight_smile:

Follow the instructions on the doc page, and it will work.

Send a message with the config information on the config topic, and then your state topics will work.

1 Like

OK I will try again, but I still do not understand the doc quite wellā€¦

The first message you send is the CONFIG, and it will tell HA that you have some/a sensor(s) that you want to register.

The CONFIG tells the system what state/cmnd topics to listen for.

The state/cmnd topics will then be sent over like normal, with the appropriate topic configuration, as outlined in the doc.

Am I on the right track thinking I need to add config information here in Node-RED?

Does this mean that I need to create a new connection per config type of sensor?

No.

You need to SEND a CONFIG topic with a payload per the documentation. It has nothing to do with your home assistant connection node.

You only need to send the config topic ONCE. Create a flow, use an inject node, and send the appropriate JSON over the config topic. Youā€™re overcomplicating this.

No, you can package all your sensor messages in one payload, but you need to differentiate them per sensor.

The documentation covers this well, you just need to play with MQTT a bit

1 Like

Ok grand, I think I am starting to get it. I create the config topic for example on command-line one time ā€¦ perfect, let me go back to the drawing board. :slight_smile:

Or you can do it in Node-Red and inject it once. If you ever need it again, itā€™s there.

Ok that might be a good idea. Not quite sure how to do that either but I am gaining some understanding.

Does this look correct?

mosquitto_pub -h localhost -p 1883 -t "homeassistant/sensor/volt/config" -m '{"name": "Forbruksspenning", "device_class": "sensor", "unit_of_measurement": "V" }'

And then in Node-RED do this:
image

Not seeing anything just yet but I will give it some time and a quick boot just in case.

You canā€™t have a device_class of SENSOR.

and you want a device named ā€˜sensor.voltā€™?

You havenā€™t told it what state topic to listen to either.

Inject node sending JSON payload to the MQTT topic. SUPER SIMPLE.

1 Like

Yeah I thought to keep it simple, therefor sensor.volt, but I was planning on doing it more like below. I think I got the JSON payload to MQTT topic. Just add an input node with the JSON and send it to the correct topic.

image

In the MQTT publish node I use topic: homeassistant/sensor/batterymonitor/volt/config

JSON payload:

{
    "name": "Forbruksspenning",
    "state_topic": "homeassistant/sensor/batterymonitor/volt/state",
    "unit_of_measurement": "V"
}

Log file:

1548175428: Received PUBLISH from mqtt_32b8215f.fe2eae (d0, q0, r0, m0, ā€˜homeassistant/sensor/batterymonitor/volt/configā€™, ā€¦ (116 bytes))
1548175428: Sending PUBLISH to 0fdf0e20-d2ea-465c-9ccf-ea14c88b21ef (d0, q0, r0, m0, ā€˜homeassistant/sensor/batterymonitor/volt/configā€™, ā€¦ (116 bytes))
1548175429: Received PUBLISH from mqtt_32b8215f.fe2eae (d0, q0, r0, m0, ā€˜homeassistant/sensor/batterymonitor/volt/stateā€™, ā€¦ (5 bytes))
1548175429: Sending PUBLISH to 0fdf0e20-d2ea-465c-9ccf-ea14c88b21ef (d0, q0, r0, m0, ā€˜homeassistant/sensor/batterymonitor/volt/stateā€™, ā€¦ (5 bytes))

I still cannot seem to understand this fully. Device class cannot be sensor, ok grand. It can be battery it seems, but for this use it is not needed? I just want a number with two decimals.

I get a lot of these messages in my HA log:
2019-01-22 17:51:00 WARNING (MainThread) [homeassistant.components.cover.mqtt] Payload is not True or False: 0

Do I need to define that the payload is a number / integer? How do I do that?

Seems excessively long to me, but should work.

You donā€™t need a device class at all, if you donā€™t want it. If your value doesnā€™t fit a device class that already exists, donā€™t use one. What does your device output? a decimal as a payload? Then it should be good.

That is a ā€˜coverā€™ mqtt component, nothing to do with the sensors you have created using discovery.

1 Like

Yes it is a decimal. Typical 12.90 (volt number). It is the voltage of a battery. Still no discovery but let me dig some more in the logs.

It might actually be an issue with your excessively long topic.
Try:
homeassistant/sensor/battmonV/config

1 Like