It`s a general question so the most system informations are unnecessary. What is more relevant is that we are still using the MQTT 1.14.0 binding and have not converted. However, not for the theoretical approach and the question of whether this is possible at all.
I use multiple Raspberry Pi’s with OpenHABian. One “server” and multiple “proxies”. The following is the configuration:
$OPENHAB_SERVER/$OPENHAB_PATH/services/mqtt.cfg
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
MQTTBroker.url=tcp://localhost:1883
# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a random default is generated.
#<broker>.clientId=<clientId>
# Optional. True or false. If set to true, allows the use of clientId values
# up to 65535 characters long. Defaults to false.
# NOTE: clientId values longer than 23 characters may not be supported by all
# MQTT servers. Check the server documentation.
#<broker>.allowLongerClientIds=false
# Optional. User id to authenticate with the broker.
#MQTTBroker.user=openhabvm
# Optional. Password to authenticate with the broker.
#MQTTBroker.pwd=Ghj%57fD
# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
MQTTBroker.qos=0
# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#<broker>.retain=<retain>
# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
MQTTBroker.async=false
# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>
$OPENHAB_SERVER/$OPENHAB_PATH/services/mqtt-eventbus.cfg
# Name of the broker as it is defined in the openhab.cfg. If this property is not available, no event bus MQTT binding will be created.
broker=MQTTBroker
# When available, all status updates which occur on the openHAB event bus are published to the provided topic. The message content will
# be the status. The variable ${item} will be replaced during publishing with the item name for which the state was received.
statePublishTopic=/messages/states/${item}
# When available, all commands which occur on the openHAB event bus are published to the provided topic. The message content will be the
# command. The variable ${item} will be replaced during publishing with the item name for which the command was received.
#commandPublishTopic=
# When available, all status updates received on this topic will be posted to the openHAB event bus. The message content is assumed to be
# a string representation of the status. The topic should include the variable ${item} to indicate which part of the topic contains the
# item name which can be used for posting the received value to the event bus.
#stateSubscribeTopic=
# When available, all commands received on this topic will be posted to the openHAB event bus. The message content is assumed to be a
# string representation of the command. The topic should include the variable ${item} to indicate which part of the topic contains the
# item name which can be used for posting the received value to the event bus.
commandSubscribeTopic=/messages/commands/${item}
$OPENHAB_PROXY/$OPENHAB_PATH/services/mqtt.cfg
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
MQTTBroker.url=tcp://<BROKER_IP>:1883
# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
#<broker>.clientId=<clientId>
# Optional. User id to authenticate with the broker.
#MQTTBroker.user=openhabvm
# Optional. Password to authenticate with the broker.
#MQTTBroker.pwd=Ghj%57fD
# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#<broker>.qos=<qos>
# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#MQTTBroker.retain=false
# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
MQTTBroker.async=false
# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#<broker>.lwt=<last will definition>
$OPENHAB_PROXY/$OPENHAB_PATH/services/mqtt-eventbus.cfg
# Name of the broker as it is defined in the openhab.cfg. If this property is not available, no event bus MQTT binding will be created.
broker=MQTTBroker
# When available, all status updates which occur on the openHAB event bus are published to the provided topic. The message content will
# be the status. The variable ${item} will be replaced during publishing with the item name for which the state was received.
#statePublishTopic=
# When available, all commands which occur on the openHAB event bus are published to the provided topic. The message content will be the
# command. The variable ${item} will be replaced during publishing with the item name for which the command was received.
commandPublishTopic=/messages/commands/${item}
# When available, all status updates received on this topic will be posted to the openHAB event bus. The message content is assumed to be
# a string representation of the status. The topic should include the variable ${item} to indicate which part of the topic contains the
# item name which can be used for posting the received value to the event bus.
stateSubscribeTopic=/messages/states/${item}
# When available, all commands received on this topic will be posted to the openHAB event bus. The message content is assumed to be a
# string representation of the command. The topic should include the variable ${item} to indicate which part of the topic contains the
# item name which can be used for posting the received value to the event bus.
#commandSubscribeTopic=
So to avoid an infinity loop publish and subscribe are logically not present in the same configuration. The server sends out only the status information, receives only the commands and vice versa for the proxies. Otherwise everyone receives what he would send.
Long introduction, but is not the point…
In a Rule, I would then like to think not only about
/messages/states/${item}
but want to use my own topics, like e.g.
/robot1/states/mycustomState1
/robot1/states/mycustomState2
/robot2/states/mycustomState1
For my sake also any calculator or other Raspberry Pi’s etc.
How could I approach this? For this I would not need the MQTT broker configured in mqtt.cfg.
Logically, this is only one approach. The second approach is that I of course send my own system information from each of my Raspberry Pi’s. And especially of course from my “server” and not the “proxies”.
As example:
/path/to/topic
instead of
/messages/states/${item}
So in short, that in addition to what OpenHAB exchanges with each other, I then have access to other devices or from other devices via MQTT. About the sense and nonsense I do not want to discuss, because we want to do a few different experiments here. I just do not know how to configure and apply this correctly.
In a Rule, I sort of tried the following:
rule "mqtt test rule"
when
Item <MyITEM> received update
then
val mqttActions = getActions("mqtt","MQTTBroker")
mqttActions.publishMQTT("/path/to/topic", "TESTED"
mqttActions.publishMQTT("path/to/topic", "TESTED2"
end
Sadly it does not work. For debugging I am using MQTT.fx. MQTT.fx is using the same Broker. I can subscribe all states and commands from OpenHAB and I can send changes to OpenHAB. So it is off course possible to use this Broker for my idea. (So at this point it would be helpful that you know which MQTT-Binding version I was using.)
What I want to create is a simple Python Code. Off course it works to subscribe states and commands or if I publish with MQTT.fx but I can not subscribe with MQTT.fx or my Python Code anything from my Rule. I mean that it is clear where I have a mistake. I just honestly have no idea what I have to do here and how.
For those who are interested, I still have the simple Python code here: (strictly speaking, it is not part of my problem).
import paho.mqtt.client as mqtt
import time
def on_message(client, userdata, message):
print("message received " ,str(message.payload.decode("utf-8")))
print("message topic=",message.topic)
print("message qos=",message.qos)
print("message retain flag=",message.retain)
broker_address="192.168.0.5"
broker_port=1883
print("creating new instance")
client = mqtt.Client("P1") #create new instance
client.on_message=on_message #attach function to callback
print("connecting to broker")
client.connect(broker_address) #connect to broker
client.loop_start() #start the loop
print("Subscribing to topic","/messages/states/iKonferenz_DanaLock_Tuerschloss_Batteriestatus")
#client.subscribe("/messages/states/iKonferenz_DanaLock_Tuerschloss_Batteriestatus")
client.subscribe("/messages/states/iKonferenz_DanaLock_Tuerschloss_Batteriestatus")
client.subscribe("/path/to/topic")
time.sleep(400) # wait
client.loop_stop() #stop the loop
From OpenHAB I can receive:
...
('message received ', '96')
('message topic=', u'/messages/states/iKonferenz_DanaLock_Tuerschloss_Batteries tatus')
('message qos=', 0)
('message retain flag=', 0)
...
and from MQTT.fx I can receive:
...
('message received ', 'TEST')
('message topic=', u'/path/to/topic')
('message qos=', 0)
('message retain flag=', 0)
...
So it could be possible if the binding would support it that the configuration and the topic path inside a rule could differ.
Thanks in advance and best regards
Michael