MQTT Discovery and Node-Red

Thanks, I tried that now. Found this in my HA logs:

2019-01-22 17:14:37 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor batterymonitor volt
2019-01-22 18:08:12 INFO (MainThread) [homeassistant.components.mqtt.discovery] Found new component: sensor batteryV

Works!!

Just found that I needed to change the name in JSON. I had a custom sensor called sensor.forbruksspenning allready. This one was in conflict. Changed the “name” part of the JSON to Forbruksbank and POP it came!

Thanks a million! I learned a lot today :smiley: :smiley:

1 Like

Just another question, since I probably created a lot of mess in my MQTT config setup, should I clean it up? Remove the config or? Since in this example it created a sensor with _2, I am not sure why… I think I will try to create a few more sensors and see how it plays out (need a state of charge and a few others…)

Keep us updated @corvy as I’m watching this topic. I’m creating a nodeRED flow that monitors MQTT topics from other devices and then selectively auto creates HA sensors based on some criteria.

I am not sure how long discovered devices stay in HA - I’ve seen them appear and disappear in the UI - but that might just be because the device disappeared. flamingmOe mentions you maybe just need to create them once.

1 Like

I usually prefer to clean up devices/sensors I don’t have any more.

If you send an MQTT message with nothing in it, on the config topic for the extra device(s) it will delete them.

2 Likes

Perfect! I will do some few tests and move my config over to MQTT and then post my resulting config. This is so much better than my current setup with storing all the data in a JSON file and reading it back in. Not cool at all compared to this :stuck_out_tongue:

Hello again! Now it seems to be working just fine! I love it! :slight_smile:

If you want to look at my config: https://pastebin.com/9xAwL3hs (export of Node-RED)

I created the config as per suggested from @flamingm0e, thanks! I had to select retain=true to keep the config each reboot. I could have let the config run at an interval or after some time after reboot, but then I might get issues with startup sequence and stuff, so I rather set retain. Also a great idea to have the JSON setup in the flow.

Thanks for all the help! The sensors works perfect now, even after reboot so I am really happy.

2 Likes

Mine is working well too - including removing auto created devices. Your post helped to push me towards finally implementing this. The help topics are a little confusing but it does work fairly simply really.

1 Like

That’s great! My devices are very constant, only the value change. :sunglasses: But cool to get them autocreated with node-red. I think we could do a lot with node red and mqtt. Need to learn more about this!

This is the way I like to do it:

I credit the idea to @robconnolly from his blog here:
https://webworxshop.com/2018/05/08/home-assistant-mqtt-discovery-sensors-in-node-red

This particular flow is to turn a camera that allows sending motion detection images to an FTP server into a HA motion sensor. JSON code for the flow below. It centers around a lot of Javascript function nodes that I’ll explain below.

The critical trick here is to send the autodiscovery config message before the state message. This ensures the config message always gets sent before the sensor state. Your MQTT server shouldn’t care too much about this but HA will love it. And you can always change how things work down the road without having to manually re-send the config message.

payload to MQTT node:

msg.topic = "homeassistant/binary_sensor/modetftp_" + msg.payload.sensor_name + "/state";
msg.payload.image = null;
return msg;

Send HASS AutoDiscovery MQTT Config node:

var sensor_config = {
    payload: {
        name: "MoDetFTP: "+msg.payload.sensor_name,
        state_topic: "homeassistant/binary_sensor/modetftp_" + msg.payload.sensor_name + "/state",
        device_class: "motion",
        payload_on: 1,
        payload_off: 0,
        value_template: "{{ value_json.value }}",
        json_attributes: [
            "image_path",
            "path",
            "sensor_name"
        ]
    },
    topic: "homeassistant/binary_sensor/modetftp_" + msg.payload.sensor_name + "/config"
};
return sensor_config;

Full Flow (you’ll need to modify your MQTT server IP)

[
    {
        "id": "4507fdcc.9441d4",
        "type": "tab",
        "label": "MoDetFTP",
        "disabled": false,
        "info": "Motion Detection for IPCams over FTP"
    },
    {
        "id": "6efa5ae8.cffb74",
        "type": "ftp-server",
        "z": "4507fdcc.9441d4",
        "name": "MoDetFTP",
        "port": "7077",
        "x": 80,
        "y": 40,
        "wires": [
            [
                "79fa05a6.00b36c"
            ]
        ]
    },
    {
        "id": "7cb07afd.600704",
        "type": "mqtt out",
        "z": "4507fdcc.9441d4",
        "name": "MoDetMQTT",
        "topic": "",
        "qos": "2",
        "retain": "true",
        "broker": "1e4d139e.30fabc",
        "x": 1030,
        "y": 180,
        "wires": []
    },
    {
        "id": "79fa05a6.00b36c",
        "type": "function",
        "z": "4507fdcc.9441d4",
        "name": "path and filename to payload",
        "func": "parts = msg.topic.split('/');\nfiledata = msg.payload;\nmsg.payload = {};\nmsg.payload.path = msg.topic;\nmsg.payload.image = filedata;\nmsg.payload.sensor_name = null;\nif(parts.length > 1) {\n    msg.payload.sensor_name = parts[1];\n    msg.payload.image_path = parts;\n    msg.payload.value = 1;\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 160,
        "y": 100,
        "wires": [
            [
                "5db8e9d4.19ba38"
            ]
        ]
    },
    {
        "id": "4b7d1dd.fadabe4",
        "type": "function",
        "z": "4507fdcc.9441d4",
        "name": "Send HASS AutoDiscovery MQTT Config",
        "func": "var sensor_config = {\n    payload: {\n        name: \"MoDetFTP: \"+msg.payload.sensor_name,\n        state_topic: \"homeassistant/binary_sensor/modetftp_\" + msg.payload.sensor_name + \"/state\",\n        device_class: \"motion\",\n        payload_on: 1,\n        payload_off: 0,\n        value_template: \"{{ value_json.value }}\",\n        json_attributes: [\n            \"image_path\",\n            \"path\",\n            \"sensor_name\"\n        ]\n    },\n    topic: \"homeassistant/binary_sensor/modetftp_\" + msg.payload.sensor_name + \"/config\"\n};\nreturn sensor_config;",
        "outputs": 1,
        "noerr": 0,
        "x": 540,
        "y": 120,
        "wires": [
            [
                "fc2c8e70.23fa8"
            ]
        ]
    },
    {
        "id": "8e933788.a46688",
        "type": "delay",
        "z": "4507fdcc.9441d4",
        "name": "Send Sensor Payload after 500ms",
        "pauseType": "delay",
        "timeout": "500",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 520,
        "y": 180,
        "wires": [
            [
                "fc2c8e70.23fa8"
            ]
        ]
    },
    {
        "id": "5db8e9d4.19ba38",
        "type": "function",
        "z": "4507fdcc.9441d4",
        "name": "payload to MQTT",
        "func": "msg.topic = \"homeassistant/binary_sensor/modetftp_\" + msg.payload.sensor_name + \"/state\";\nmsg.payload.image = null;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 190,
        "y": 180,
        "wires": [
            [
                "4b7d1dd.fadabe4",
                "8e933788.a46688",
                "6da07b80.652404"
            ]
        ]
    },
    {
        "id": "ced9ebd1.240888",
        "type": "change",
        "z": "4507fdcc.9441d4",
        "name": "detection level to 0",
        "rules": [
            {
                "t": "set",
                "p": "payload.value",
                "pt": "msg",
                "to": "0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 570,
        "y": 280,
        "wires": [
            [
                "fc2c8e70.23fa8"
            ]
        ]
    },
    {
        "id": "fc2c8e70.23fa8",
        "type": "function",
        "z": "4507fdcc.9441d4",
        "name": "merge flows",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 850,
        "y": 180,
        "wires": [
            [
                "7cb07afd.600704"
            ]
        ]
    },
    {
        "id": "6da07b80.652404",
        "type": "trigger",
        "z": "4507fdcc.9441d4",
        "op1": "",
        "op2": "",
        "op1type": "nul",
        "op2type": "payl",
        "duration": "10",
        "extend": true,
        "units": "s",
        "reset": "",
        "bytopic": "topic",
        "name": "Clear Sensor after 10s of no motion",
        "x": 520,
        "y": 240,
        "wires": [
            [
                "ced9ebd1.240888"
            ]
        ]
    },
    {
        "id": "1e4d139e.30fabc",
        "type": "mqtt-broker",
        "z": "",
        "name": "twohunnid_mqtt",
        "broker": "192.168.1.19",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]
2 Likes

Ah, I get it, very clever :slight_smile: My sensor gives data in a constant flow more or less. So this would not be relevant in my use case, but I see that it is smart in you case example. Thanks for sharing! Probably will be useful in a future project!

Hi, so if I want a node red flow to send an mqtt value to a topic and be it auto discovered by home assistant, I have e.g. the following:

And I hoped that this would just be enough (set the topic right :-)) but it seems not to…

What do I need to add there?


There’s dummy auto discover config setting and another to inject the temperature

@cues I am a noob with MQTT and having issues getting discovery to work. I used all of the examples from the posts and ended up with yours and an example of the instruction page but still no luck.
When I listen with # I am seeing traffic and discovery is checked in the config off MQTT integration.

Bericht 4 ontvangen op homeassistant/switch/irrigation om 11:01:
true
QoS: 0 - Retain: false

Bericht 3 ontvangen op homeassistant/switch/irrigation/config om 11:01:
{
    "name": "garden",
    "command_topic": "homeassistant/switch/irrigation/set",
    "state_topic": "homeassistant/switch/irrigation/state"
}
QoS: 0 - Retain: false

I am pretty much clueless about my options now. Anybody any ideas.

Maybe worth mentioning that its a docker based install and mqtt eclipse running for the time being without user and pass

IMO homeassistant topic is for discovery feature only.

Actual topics are (or might be) located in different location.
use MQTT Explorer application. It will gives you overview of a tree of topics and values in mqtt.

That is really weird, I’m sure you’ve triple checked already that you have auto discovery enabled in mqtt integration on homeassistant ui :slight_smile:

These exact messages first create the sensor and then set temperature to 50C on my ha.


Viesti 157 vastaanotettu aiheessa mqttsensor/dummytemp 15.20:
50
QoS: 0 - Retain: false
Viesti 156 vastaanotettu aiheessa homeassistant/sensor/dummymqtttemp/config 15.20:
{
    "name": "Dummy MQTT temperature",
    "state_topic": "mqttsensor/dummytemp",
    "unit_of_measurement": "°C"
}
QoS: 0 - Retain: false

Thanks for the reply, and installed.

image

As you can see it isn’t in there but an older one with retain flag to true wich i can not get rid of is there. (Parked the getting rid off to somwhere later)

Going to recreate cues message I might made a typo somewhere, it must be something like that.
Only thing is the battVS doesn’t show up either.

Yes i needed to go to re-configure MQTT it was checked by default. Later I even unchecked and checked it to make sure it was really checked.

Will install the MQTT explorer first and then recreate your dummytemp.

Done butt still no luck

To make sure. If I can listen with the # then it cannot be a broker issue right???