Package: python-openhab-eventbus
Python openHAB Event BusAn MQTT Event Bus for openHAB. This Event Bus mirrors theItem EventsItemStateEventandItemCommandEventvia MQTT. AnItemStateEventis received when the state of an item is updated. This can be apostUpdateby aRuleor when something is operated on the device and the state is changed via the correspondingBinding. AnItemCommandEventis received when a command is sent to an item via a channel. This can be asendCommandin aRuleor when serving an element in theSitemap.What is an Event BusGenerally speaking, an event bus is a design pattern for how devices can be operated. The state indicates the status of an item. This item can be, for example, a switch that is set to ON. If I switch off this device accordingly, the state will also change to OFF. The device therefore publishes the state of this switch item via a topic. To operate the device, however, a command is used. With the command OFF I give the device the command to change the state to OFF. If this device is ON, it will be switched off accordingly. With the command ON I would switch the device on again. For this the device must be able to subscribe via a topic.A command therefore requires the change of a state. However, it should be noted that a command is not the same as a state. Commands and states are often called differently. For example, I can imagine the commands louder or quieter at a loudspeaker and as a state I get a number that indicates how loud this loudspeaker is.The openHAB Event Bus allows to apply this design pattern to anyItemin openHAB. So I can operate an item via other programs or even other openHAB instances. But I can only configure that eitherStateis published andCommandis subscribed orCommandis published andStateis subscribed.InstallationYou can install it by using pip:python3 -m pip install python-openhab-eventbusUsageYou have to import theEventBusfromopenhab:from openhab import EventBusIn the next step you have to create anEventBusobject instance:# Main function.
if __name__ == "__main__":
    event_bus = EventBus("http://<openhab_ip>:8080", "<openhab_username>", "<openhab_password>", "<mqtt_ip", <mqtt_port>, "<mqtt_username>, "<mqtt_password>", "<statePublishTopic>", "<commandPublishTopic>", "<stateSubscribeTopic>", "<commandSubscribeTopic>")The program must be able to access theREST APIof openHAB as well as the MQTT broker to be used. Both openHAB and the MQTT broker do not have to run on the same computer as the program itself. Also, openHAB and the MQTT Broker may run on different computers. However, the computers must be in the same network. As MQTT broker I useMosquittomyself. However, the program should be compatible with all brokers.You have to replace<openhab_ip>with your openHAB IP address.<openhab_username>and<openhab_password>is needed to connect to theREST APIof openHAB. Depending on your configuration you may not need to use a username and password. Instead of onestreach for the username and password of your openHAB user, you have to passNoneaccordingly.To connect to the MQTT broker, an MQTT client is required. The program usespaho-mqttfor this purpose. The client therefore needs an IP address to this broker and the port under which this broker can be reached. For this you have to replace<mqtt_ip>and<mqtt_port>accordingly.<mqtt_ip>is astrand<mqtt_port>is anint.Mosquittoas example uses the port1883by default.It is possible for an MQTT broker to allowanonymousaccess. This means that one does not have to specify a user name and password. In this case<mqtt_username>and<mqtt_password>must be replaced by None. Otherwise, astrmust be used accordingly.TheREST APIusesserver-sent events(SSE) to check whether anItemStateEventor anItemCommandEventhas been triggered. If this is the case, theStateis queried for anItemStateEventvia aGETrequest for thisItemand this is published, or thecommandfor thisItemis published for anItemCommandEvent.WhetherItemStateEvent's orItemCommandEvent's are considered is determined when setting the topics for the MQTT client.The topicstatePublishTopicspecifies a topic to be used to publishItemStateEvent's. If it is set, nocommandPublishTopicmay be specified. AcommandPublishTopic, on the other hand, specifies a topic that publishesItemCommandEvent's. Both can be passed via astr. The other topic should be set toNone.The topicstateSubscribeTopicspecifies a topic to be used to subscribeState's of anItem. If it is set, nocommandSubscribeTopicmay be specified. AcommandSubscribeTopic, on the other hand, specifies a topic that subscribesCommand's. Both can be passed via astr. The other topic should be set toNone.So in the end you have the following two configuration options:from openhab import EventBus

# Main function.
if __name__ == "__main__":
    event_bus = EventBus("http://192.168.0.5:8080", None, None, "192.168.0.5", 1883, None, None, "openhab/${item}/state", None, None, "openhab/${item}/command")And:from openhab import EventBus

# Main function.
if __name__ == "__main__":
    event_bus = EventBus("http://192.168.0.11:8080", None, None, "192.168.0.5", 1883, None, None, None, "openhab/${item}/command", "openhab/${item}/state", None)What would happen in this example? The openHAB instance 192.168.0.5 publishes theState's of itsItem's. The openHAB instance 192.168.0.11 subscribes theState's for itsItem's. If the sameItem's are present, both instances have the sameState's.If you execute aCommandon 192.168.0.11, this is published and also triggers aCommandon 192.168.0.5, which means that theState's will be changed and would be published from there again.Now we have seen something else extremely important:${item}. This is a placeholder for the items. For each topic, this placeholder is replaced with the name of the item accordingly. It is important to pass this placeholder. Otherwise the program will not work.How the topics look like, you can define yourself:openhab/${item}/state
openhab/${item}/command
/openhab/${item}/state
/openhab/${item}/command
openhab/out/${item}/state
openhab/out/${item}/command
openhab/in/${item}/state
openhab/in/${item}/command
messages/states/${item}
messages/commands/${item}
/messages/states/${item}
/messages/commands/${item}
smarthome/${item}/state
smarthome/${item}/command
...You still have to give the program a name and save it as a Python file. Let's say you name iteventbus.py, then you can call it like this:python3 eventbus.pyA better suggestion is to run the program at system startup and start, stop or restart depending on openHAB. Aserviceis recommended for this. Let's assume that we name the service<event_bus.service>and put it in/etc/system/system/:[Unit]
Description=Python openHAB MQTT Event Bus
Requires=openhab.service
After=openhab.service
BindsTo=openhab.service
PartOf=openhab.service

[Service]
Type=simple
User=openhab
Group=openhab
UMask=002
ExecStart=/usr/bin/python3 </path/to/>eventbus.py
Restart=on-failure
RestartSec=30s

[Install]
WantedBy=openhab.servicePlease replace</path/to/>with the path to your file and if you does not name your fileeventbus.pyyou have to replace this too. As example it could be/home/ubuntu/eventbus.pyIf yourusernameisubuntuand you have placed the file in the home directory of yourubuntuuser.At least you have to run and enable your service:sudo systemctl start event_bus.service
sudo systemctl enable event_bus.service