MQTT Auto Discovery and JSON payload

Hi,

I have two questions

I’m having some trouble understanding the MQTT Autodiscovery.
So as far as I understand, do correct me if I’m wrong, I have to tell Home Assistant to listen on some config topic. Where do I do this?

And then from some device I can publish something to this topic like:

{
  "~": "homeassistant/bell/",
  "name": "Bell",
  "unique_id": "bell_1",
  "cmd_t": "~/set",
  "stat_t": "~/state",
}

And then home assistant will create a device for me? How do I do this if I want some device combining a number and a binary value?

In general, If I want some device to publish multiple values, can I just do this in configuration.yaml:

sensor:
  - platform: mqtt
    name: "Temperature"
    state_topic: "office/sensor1"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.temperature }}"
  - platform: mqtt
    name: "Humidity"
    state_topic: "office/sensor1"
    unit_of_measurement: "%"
    value_template: "{{ value_json.humidity }}"

And then in the device publish this as payload to office/sensor1:

{
    "temperature": 23.20,
    "humidity": 43.70
  }
  1. You can do where you want it. On the device, in a script, on the mqtt integration page

Example of a script:

  scriptmyautodiscovery:
    sequence:
      - service: mqtt.publish
        data:
          retain: true
          topic: homeassistant/sensor/xiron_3201/temperature/config
          payload: '{"unit_of_measurement":"°C","device_class":"temperature","value_template":"{{ value_json.TEMP }}","state_class": "measurement","state_topic":"rflink/Xiron-3201","name":"eetkamer_temperature","unique_id":"eetkamer_temperature","device":{"identifiers":["xiron_3201"],"name":"xiron_3201","model":"Digoo temp & humidity sensor","manufacturer":"Digoo"}}'
      - service: mqtt.publish
        data:
          retain: true
          topic: homeassistant/sensor/xiron_3201/humidity/config
          payload: '{"unit_of_measurement":"%","device_class":"humidity","value_template":"{{ value_json.HUM }}","state_class": "measurement","state_topic":"rflink/Xiron-3201","name":"eetkamer_humidity","unique_id":"eetkamer_humidity","device":{"identifiers":["xiron_3201"],"name":"xiron_3201","model":"Digoo temp & humidity sensor","manufacturer":"Digoo"}}'

  1. Seems correct

Thanks for the script! I’ve implemented this on some local python client to test. So when I make the MQTT broker manually listen to “homeassistant/sensor/xiron_3201/temperature/config” it receives all the info (I’ve cut this short because it was a very long message):

So the next step is that some ‘card’ will appear on the integrations page, looking something like this:
image

But then of course related to some other device. How does this happen?

It shows in mqtt

You need device {}

You can use short names. See docs.

Just as example:

$mqttpub -t "homeassistant/sensor/${id}/${devx}_${1}/config" \
-m '{
 "unit_of_measurement":"ms",
 "state_class":"measurement",
 "expire_after":"120",
 "icon":"'$icon'",
 "name":"'"$name $dev $3"'",
 "state_topic":"'"$topic/${id}/${devx}_${1}"'",
 "availability_topic":"'$topic/${id}/status'",
 "unique_id":"'"${id}_${devx}_$1"'",
 "device":{
   "identifiers":"'${id}'",
   "name":"'"$name"'",
   "model":"'"$model"'"}
 }'
$mqttpub -t "homeassistant/binary_sensor/${id}/${id}_status/config" \
-m '{
 "device_class":"connectivity",
 "payload_on":"online",
 "payload_off":"offline",
 "expire_after":"120",
 "name":"'"$name Status"'",
 "state_topic":"'"$topic/${id}/status"'",
 "availability_topic":"'$topic/${id}/status'",
 "unique_id":"'"${id}_status"'",
 "device":{
   "identifiers":"'${id}'",
   "name":"'"$name"'",
   "model":"'"$model"'"}
}'

I forgot to encode my Python dict as JSON, it works now. Thanks a lot, this saves me lot of time adding the devices!

I’m new to this - -how do I set up generic MQTT to publish updates for all devices for various values but especially state? Can I do it using a topic and payload, or do I need to write code? A simple version that says “changed” might be enough – I can then query the HA server.

I’m not sure if this is what you mean, but ‘various values’ are usually different entities with their own topics to which device subscribe. No need to query or something.
Maybe this is helpfull:

Thanks.

I have no problem writing MQTT code. I have my own app that listens for messages and updates the status locally. This works fine for Tasmota and Shelly.

My problem is that I want to get such notifications whenever a HomeAssistant devices changes. When I look at MQTT Publish service - Home Assistant (home-assistant.io) I see


Looking again, it does seem like the template facility is fairly powerful but the examples are a mixture of general and specific like the use of “.paulus”. It presumes I want put a lot of smarts into the HA side. I just want “tell me everything” so I can process it in my own client on my PC.

topic: /home-assistant/{{everything}}
payload: "{{attribute}:{{attribute-value}}"

One problem with a general-purpose tool like HA is that there is a lot of implicit knowledge I need to get up to speed on, but I learn best by starting with simple working examples.

Or use home assistant api

I agree with you on that one. Seems like a shitload of work to publish state changes. The API is a more easy way if you don’t mind querying the server.

Since I’m unfamiliar with the API - a pointer or example?

Oh - you mean the http API by polling. Or can I use WebSockets to get events? If so, that would be acceptable for this purpose.

UPDATE: Been looking at the WebSocket support stand example. It seems to be for the browser rather than node.