uid: rules_tools:mqtt_eventbus label: MQTT Event Bus description: Implements the MQTT EventBus, handling both subscription, publication, and status. configDescriptions: - name: broker type: TEXT context: thing label: MQTT Broker Thing description: Select the MQTT Broker Thing used for the MQTT Event Bus required: true - name: topicRoot type: TEXT label: "openHAB Instance Name" description: Name of this openHAB instance, used as the root fo the topic structure. required: true - name: cmd type: TEXT context: item filterCriteria: - name: type value: Group label: Command Group description: Group Item whose member's commands are to be published. required: true - name: upd type: TEXT context: item filterCriteria: - name: type value: Group label: Update Group description: Group Item whose member's updates are to be published. required: true - name: channel type: TEXT context: channel filterCriteria: - name: kind value: TRIGGER label: MQTT Event Channel description: The MQTT trigger channel that subscribes to the MQTT Event Bus. required: true triggers: - id: "1" configuration: groupName: "{{cmd}}" type: core.GroupCommandTrigger - id: "2" configuration: groupName: "{{upd}}" type: core.GroupStateUpdateTrigger - id: "5" label: EB Subscription configuration: thingUID: "{{broker}}" channelUID: "{{channel}}" type: core.ChannelEventTrigger conditions: [] actions: - inputs: {} id: "9" configuration: type: application/javascript script: >- // Verison 1.3 var {helpers} = require('openhab_rules_tools'); console.loggerName = 'org.openhab.automation.rules_tools.MQTTEventBus'; // osgi.getService('org.apache.karaf.log.core.LogService').setLevel(console.loggerName, 'DEBUG'); helpers.validateLibraries('4.1.0', '2.0.1'); // These get populated below after we determine the type of the rule trigger var receivedEvent = null; var itemState = null; var itemCommand = null; var triggerType = null; var updateEvents = ['ItemStateEvent', 'ItemStateUpdatedEvent', 'ItemStateUpdateTrigger', 'update']; var commandEvents = ['ItemCommandEvent', 'ItemCommandTrigger', 'command']; /** * Parses the event received and extracts the Item, event type, and state/command * to update/command the Item. * * This expects the event to be topic#message where the Item's name is the third level * of the topic and the event type is the fourth. For example: * * openhab/in/MyLight/command#ON * openhab/in/MyLight/state#ON */ var procEvent = () => { console.debug('Processing an event bus event'); var parts = receivedEvent.split('#'); const topic = parts[0]; const state = parts[1]; parts = topic.split('/'); const itemName = parts[2]; const eventType = parts[3]; console.debug('Received MQTT EB message: type = ' + eventType + ' item = ' + itemName + ' state/cmd = ' + state); if(items[itemName] === null) { console.warn('Received an event bus message for ' + itemName + ' which does not exist!'); } else { (eventType == 'command') ? items[itemName].sendCommand(state) : items[itemName].postUpdate(state); } } /** * Publishes commands and updates to the configured topic. */ var pubEvent = () => { console.debug('Publishing an event bus event'); var retained = (updateEvents.includes(triggerType)); var topic = '{{topicRoot}}/out/' + event.itemName + ((retained) ? '/state' : '/command'); var msg = ((retained) ? itemState : itemCommand); console.debug('Publish - Topic: ' + topic + ' Message: ' + msg + ' Retained: ' + retained); actions.get('mqtt', '{{broker}}').publishMQTT(topic, msg, retained); } // Trigger processing if(this.event !== undefined) { triggerType = (event.triggerType !== undefined) ? event.triggerType : event.type.toString(); console.debug('Processing an MQTT Event Bus event: ' + triggerType); switch(triggerType) { // Process an incoming message on the MQTT Channel Trigger, updating/commanding the corresponding Item case 'ChannelEventTrigger': case 'triggered': receivedEvent = (event.receivedEvent !== undefined) ? event.receivedEvent : event.event.toString(); procEvent(); break; // Publish state updates and commands on local Items case 'ItemStateEvent': case 'ItemStateUpdatedEvent': case 'ItemStateUpdateTrigger': case 'update': itemState = (event.receivedState !== undefined) ? event.receivedState : event.itemState.toString(); case 'ItemCommandEvent': case 'ItemCommandTrigger': case 'command': if(event.receivedCommand !== undefined || event.itemCommand !== undefined) { itemCommand = (event.receivedCommand !== undefined) ? event.receivedCommand : event.itemCommand.toString(); } pubEvent(); break; // Warn if we received an unknown event. default: console.warn('MQTT Event Bus rule triggered without an event we can process: ' + triggerType + ', ignoring'); } } else { console.warn('MQTT Event Bus rule triggered without event, ignoring'); } type: script.ScriptAction