Jump to content

changing mqtt topics in mqtt bindings


CChris
 Share

Recommended Posts

Hi,

I want to connect my tinkerforge hardware with HomeAssistant over MQTT.
Therefore, I am now testing the MQTT Bindings - and they are working - so far.

I received the following in my MQTT Broker:

tinkerforge/response/barometer_bricklet/vNB/get_air_pressure {"air_pressure": 970609}
tinkerforge/response/barometer_bricklet/vNB/get_altitude {"altitude": 49438}

But - I did not yet get them into HomeAssistant, because I do need to configure the MQTT plugin still to listen to the topic "tinkerforge"...
Before I do this, I just wanted to know, if it would be possible, to change the topics.

Why?
Because the MQTT Plugin from HomeAssistant has an AutoDiscovery option...

It would automatically discover messages when a specific topic will be used.
The Documentation from HomeAssistant writes:

Zitat

The discovery topic needs to follow a specific format:

<discovery_prefix>/<component>/[<node_id>/]<object_id>/config

furthermore:
<component> : One of the supported MQTT Components, eg. binary_sensor, sensor, switch, etc...
<node_id> : (Optional) ID of the node providing the topic, this is not used by HomeAssistant but may be used to structure the MQTT Topic.
The ID of the node must only consist of characters from the character class [a-zA-Z0-9_-]
<object_id> : the ID of the device. This is only to allow for seperate topics for each device and is not used for the entity_id.
The ID of the device must only consist of characters from the character class [a-zA-Z0-9_-]

The node_id level can be used by clients to only subscribe to their own command topics by using one wildcard topic like <discovery_prefix>/+/<node_id>/+/set

Best practice for entities with a unique_id is to set <object_id> to unique_id and omit the <node_id>

by default, the auto-discovery prefix from Homeassistant is "homeassistant"

This would mean, that the tinkerforge Topic should probably changed to:

homeassistant/sensor/tinkerforge/UID/config {"air_pressure": 970609}
homeassistant/sensor/tinkerforge/UID/config {"altitude": 49438}

Another example, for which the Auto-Discovery has worked, is the sensor of my CPU usage from my computer:

homeassistant/sensor/WSCCA/WSCCA_cpulast/config

{"availability_topic":"homeassistant/sensor/WSCCA/availability","icon":"mdi:chart-areaspline","unique_id":"79647378-7aaa-43d9-bfcc-675684ea6ad9","unit_of_measurement":"%","device":{"identifiers":"hass.agent-WSCCA","manufacturer":"LAB02 Research","model":"Microsoft Windows NT 10.0.19044.0","name":"WSCCA","sw_version":"2022.13.0"},"name":"WSCCA_cpulast","state_topic":"homeassistant/sensor/WSCCA/WSCCA_cpulast/state"}

WSCC is the hostname of the computer
WSCC_cpulast is the name of the sensor.

I guess, when I want to use a similar format for the payload and topics,
I have to create my own script, which does get the sensor values - and then create the specific mqtt topicĀ  / payload within the script...?
That's probably not working with the default "tinkerforge_mqtt" bindings, right?

Edited by CChris
Link to comment
Share on other sites

If you only want to change the prefix (i.e. tinkerforge/) you can use the --global-topic-prefix command line argument:https://www.tinkerforge.com/en/doc/Software/API_Bindings_MQTT.html#topic-prefix

To also change the suffix to /config, you have to write your own script or change the bindings. (You can techically add a suffix to any tinkerforge-topic however home assistant does not allow node_ids with / in them so this doesn't work in your case)

Link to comment
Share on other sites

I am just thinking what the "best" format could be.
It would be great, if the auto discover option would be able to create entities out of the different sensors directly....
So I would asume, the best option would somehow to follow the example above from my cpu_usage....

where also other information are given in the payload...
In that case, it would require a own script for doing that - and including the different information into the payload...

But I do will tests with the --global-topic-prefix, too - to see how the outcome in homeassistant will be :)

- so, I have added the --global-topic-prefix to the command, but it seems that it was only applied to the "callback" part

image.png.71455ceb7b93db772d80c9452cc53595.png

I couldn't see any "response" within the global topic suffic... so, checking the txt file now, if I might change the topic there.

Edited by CChris
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...