[Feature Request] MQTT: custom base topic
Hello,
Is possible to set custom MQTT "base topic" ?
For now all MQTT is going under topic /<client-id>/... , but I want it to go under e.g.: /the/my_custom/base/topic , so assembled topics would looks like:
/the/my_custom/base/topic/<client-id>/connected
/the/my_custom/base/topic/<client-id>/IP/get
/the/my_custom/base/topic/<client-id>/0/get
/the/my_custom/base/topic/<client-id>/1/set
/the/my_custom/base/topic/<client-id>/1/get
...
To add this functionality:
- create a setting (under MQTT) "Base Topic", char[31] - should be sufficient, and store this with other settings,
- In line https://github.com/openshwprojects/OpenBK7231T_App/blob/c8074c00825911e6ff23a53cdd8302ccc5aa0aff/src/mqtt/new_mqtt.c#L584 , in place
CFG_GetMQTTClientId()first assemble the topic to string<base-topic>/<client-id>, then pass it to the function in 2nd argument, /or/ just create new function that read this base topic from configuration and return it e.g.:const char* CFG_GetMQTTBaseTopic(), and use it instead of CFG_GetMQTTClientId (and in places where topic is used)... - I also noticed, there is few other places too (e.g.
sprintf) where<client-id>is used as topic (seems like debug purposes?), and it is used "directly" inwill_topicin routineMQTT_do_connect.
This basically related to issue #48 but more simplified.
This is probably the only thing that keeps me away from anything that is non-ESP based - I just hate to have junkyard in my MQTT server(broker ; I have everything there organized) ...
Hey, we can do this, but since you already proposed a solution, maybe just create a pull request and I will integrate it?
This is a good feature.
We would want to make sure that the buffers involved are big enough to hold the additional base topic.
Hi,
I noticed it, I think I saw this with get topic - there is calculation of buffer done with some "hard" coded lengths. This is something that need attention too while writing changes/code.
For now, I'm not up to the task 😉, I have other things to take care of after work. Also setting up env for building will take me a bit of time, since I never touched this bkt chips.
@saper-2 EDIT: I looked at the code and adding a base topic field would be simpler Wait a sec
@saper-2 do you want cmnd/%s/ to be transformed to lights/cmnd/%s/ ?basically, prepend everything with a string?
or would you expect the cmnd/%s/ to be transformed to cmnd/lights/%s/ ?
EDIT: wouldn't just setting in that way in current build work?

I do not think this will work with OpenBK because the status and cmd are on different root topics. I think what was being asked wa something like this where status and command are based on the pre-defined root topic.

@saper-2 do you want cmnd/%s/ to be transformed to lights/cmnd/%s/ ?basically, prepend everything with a string?
Yes, exactly 😄 , so I could set a root-topic like @iprak named it.
or would you expect the cmnd/%s/ to be transformed to cmnd/lights/%s/ ?
I think it would be wiser in this case to go completely into "tasmota style" where we can define how the whole topic would we want to look like.
Here I will give a brief explanation about tasmota topics/prefixes and sub-topics 😉
This is just for "reference" , I know implementing this would require a lot of time, so I not even try asking for this 😅
Here is how tasmota config looks like:

And here is a bit of explanation:
%topic% is device name - in OpenBK client-id is used as "topic",
%prefix% "group" of subtopics in 3 categories:
telewhere would be "sub-topic" with all kind of information (sent every 300/600/900sec - can be set):LWT- no need to explain 😄 ,STATE- , format JSON: uptime, heap, wifi ap info (ssid, BSSID, rssi, link down count, channel, mode),SENSOR- format JSON, all kind info from sensors if connected/configured (BL0942/etc...),INFOx- format JSON, x=number,- 1 - Device basic info: fw version, type (aka module in tasmota), GroupTopic, ,
- 2 - Network info: device ip, hostname, web server mode,
- 3 - Last restart info: reason, boot counter,
cmndunder this topic goes all topic to "control" or query for status etc..., e.g.:POWER- without parameter will return understatprefix output state, if have value0,1,on,off,toggleoutput will change its state accordingly. note: if POWER have no nu,er following then it refer to output/relay number 1 , if is followed by number, then it refer to that number output,STATE- will return understatinRESULTcurrent info like intele\STATE,STATUS- value1to11request status info (response understat\STATUSx)- and all other commands that can be issued by console,
statunder sub-topics goes all other and:RESULT- format JSON , response for issued commands that do not have specific response topics (like forcmnd\STATE),POWER- report output status (ON/OFF), can be followed by number if there is more outputs (e.g.POWER6for 6th output)STATUSx,x- number, return json with information (and requested info by cmnd/status), more there: JSON-Status-Responses

I know, I wasted my time but.... 😝
EDIT: wouldn't just setting in that way in current build work?
hmmm it didn't worked last time... works but I'd call it "half-solution"

Why half? Because client-id is a "client id" not really a way to create topic (root topic) - this can be confusing. I'd leave client-id as client-id and wouldn't try to mix it with topic (base topic/root topic). Or maybe just change currently used client-id to root-topic ,and mqtt client-id create from FullName with appended last 4 or 6 digits of MAC (hex format)
Hi,
I just noticed: publishing to topic: home/bkt/NousA1/1/set , value 1 with client set to home/bkt/NousA1 - I can't control relay via MQTT . So Client can't be used to set topic.
I just installed this on a new device, and trying to move my setup I use for Tasmota to this. I see that mqtt config screen does have custom base topic support. But in Tasmota we can set this up via commands too, and I am not sure if that is possible yet.
Tasmote commands
- Topic
- FullTopic
- GroupTopic1