@David_Graeff i am running 2.5 M1 and I am seeing this bug. I am trying to link up homeassistant and openhab via mqtt using rule files and the thing action.
rules file
// MQTT naming convention i use:
//
//commandPublishTopic = /openHAB/out/${item}/command
//stateSubscribeTopic = /openHAB/in/${item}/state
// MASTER
var boolean commandSubscribeTopic = true
var boolean stateSubscribeTopic = false
var boolean statePublishTopic = true
var boolean commandPublishTopic = false
val actions = getActions("mqtt","mqtt:broker:a35fc928")
// SLAVE
// var boolean commandSubscribeTopic = true
// var boolean stateSubscribeTopic = false
// var boolean statePublishTopic = true
// var boolean commandPublishTopic = false
// We need a group with all items as a Member of trigger.
// Creating one dynamicly. (Group dg_AllItems)
// ScriptServiceUtil.getItemRegistry.getItems() can not be used as a Member of trigger (I've tried :-)
rule "Publish command to broker"
when
Member of HomeAssistant_Switch received command
then
if (commandPublishTopic){
HomeAssistant.members.forEach[hassGroup|
if (hassGroup instanceof GroupItem)
{
hassGroup.members.forEach[hassMember|
if (hassMember.name == triggeringItem.name)
{
val mqttGroupLabel = hassGroup.label.toString().split(":")
val mqttSubPath = mqttGroupLabel.get(0)
val mqttPath = "homeassistant/"+mqttSubPath+"/"+triggeringItem.name+"/set"
val mqttPayload = triggeringItem.state.toString
logDebug("HomeAssistant.rules", "Sending to mqtt path: " + mqttPath+ " the following payload: "+ mqttPayload)
HomeAssitant_MQTT.sendCommand(mqttPath + "%Payload%" + mqttPayload)
//actions.publishMQTT(mqttPath,mqttPayload)
}
]
}]
}
end
rule "Publish state to broker"
when
Member of HomeAssistant_Sensor received update or
Member of HomeAssistant_Switch received update or
Member of HomeAssistant_Door received update or
Member of HomeAssistant_Motion received update or
Member of HomeAssistant_Presence received update 8
then
HomeAssistant.members.forEach[hassGroup|
if (hassGroup instanceof GroupItem)
{
hassGroup.members.forEach[hassMember|
if (hassMember.name == triggeringItem.name)
{
val mqttGroupLabel = hassGroup.label.toString().split(":")
val mqttSubPath = mqttGroupLabel.get(0)
val mqttPath = "homeassistant/"+mqttSubPath+"/"+triggeringItem.name+"/state"
val mqttPayload = triggeringItem.state.toString
logDebug("HomeAssistant.rules", "Sending to mqtt path: " + mqttPath+ " the following payload: "+ mqttPayload)
HomeAssitant_MQTT.sendCommand(mqttPath + "%Payload%" + mqttPayload)
//actions.publishMQTT(mqttPath,mqttPayload)
}
]
}]
end
rule "Subscribe to broker"
when
//Channel 'mqtt:broker:a35fc928:publish' triggered
then
logDebug("HomeAssistant.rules-mqtt-publish", "mqttevent payload received: " +receivedEvent.getEvent.toString())
var evnt_payload = receivedEvent.getEvent.toString.split("#")
// logDebug("HomeAssistant.rules", "mqttevent payload received: + " + event_payload.toString())
var mqtttopic = evnt_payload.get(0).toString.split("/")
var mqttcommand = evnt_payload.get(1)
//logDebug("HomeAssistant.rules-mqtt-publish", "the mqtttopic is : " +mqtttopic.toString() + "the mqttcommand is:" +mqttcommand.toString())
var mqttitem = mqtttopic.get(mqtttopic.length-2)
//logDebug("HomeAssistant.rules-mqtt-publish", "the mqttitem is : " +mqttitem.toString())
var mqttcommandorstate = mqtttopic.get(mqtttopic.length-1)
//logDebug("HomeAssistant.rules-mqtt-publish", "The members to search are: " + HomeAssistant.allMembers)
var ItemExists = HomeAssistant.allMembers.filter[item | item.name.contains(mqttitem)].length
//logDebug("HomeAssistant.rules-mqtt-publish", "the mqttitem is : " + ItemExists.toString())
if (ItemExists > 0 || mqttitem.contains("HomeAssistant_Config")){
if (commandSubscribeTopic && mqttcommandorstate == "set") {
sendCommand(mqttitem,mqttcommand)
}
else {
if (stateSubscribeTopic && mqttcommandorstate == "state") {
postUpdate(mqttitem,mqttcommand)
}
}
}
end
rule "Send Autodiscovery Switches"
when
System started or
Item HomeAssistant_Config changed
then
HomeAssistant.members.forEach[hassGroup|
if (hassGroup instanceof GroupItem)
{
logDebug("HomeAssistant.rules", "HASSgroup name = " + hassGroup.name)
hassGroup.members.forEach[hassMember|
var mqttPayload = ""
var hassGroupname = hassGroup.name
val mqttGroupLabel = hassGroup.label.toString().split(":")
val mqttSubPath = mqttGroupLabel.get(0)
val mqttBasePath = "homeassistant/"+ mqttSubPath +"/"+ hassMember.name
var mqttPath = mqttBasePath+ "/config"
logDebug("HomeAssistant.rules-config", "The device class is: " + mqttSubPath.toString())
var mqttdevicejson = ""
if (mqttGroupLabel.get(1).contains("None")){}else{
mqttdevicejson = '"device_class":"' + mqttGroupLabel.get(1) + '",'
}
switch (hassGroupname){
case mqttSubPath.contains("switch"):
mqttPayload = '{"name": "'+ hassMember.label +'","state_topic": "'+ mqttBasePath + '/state", "command_topic":"'+mqttBasePath+'/set"}'
case mqttSubPath.contains("binary_sensor"):
mqttPayload = '{"name": "'+ hassMember.label +'",' +mqttdevicejson+'"state_topic": "'+mqttBasePath+'/state"}'
}
logDebug("HomeAssistant.rules", "Sending to mqtt path: " + mqttPath+ " the following payload: "+ mqttPayload)
//actions.publishMQTT(mqttPath,mqttPayload)
HomeAssitant_MQTT.sendCommand(mqttPath + "%Payload%" + mqttPayload)
]
}]
end
rule "Send HomeAssistant Config"
when
Item HomeAssistant_Config received update
then
val mqttPath = "homeassistant/switch/"+triggeringItem.name+"/state"
val mqttPayload = triggeringItem.state.toString()
logDebug("HomeAssistant.rules", "Sending to mqtt path: " + mqttPath+ " the following payload: "+ mqttPayload)
HomeAssitant_MQTT.sendCommand(mqttPath + "%Payload%" + mqttPayload)
//actions.publishMQTT(mqttPath,mqttPayload)
end
jsr223 file
load(Java.type("java.lang.System").getenv("OPENHAB_CONF")+'/automation/jsr223/jslib/JSRule.js');
JSRule({
name: "Send MQTT Payload",
description: "Send an MQTT payload from the string",
triggers: [
CommandEventTrigger("HomeAssitant_MQTT")
],
execute: function( module, input){
//logInfo("About to Trigger MQTT Rule: ");
var receivedCommand = getTriggeredData(input).receivedCommand;
var splitCommand = receivedCommand.split("%Payload%");
var mqttPayload = splitCommand[1];
var mqttPath = splitCommand[0];
logInfo("The MQTT rule was triggered with a path of : " + mqttPath + " and a payload of: " + mqttPayload);
actions.get("mqtt", "mqtt:broker:a35fc928").publishMQTT(mqttPath, mqttPayload);
}
});
.items file
Group HomeAssistant
Group HomeAssistant_Switch "switch:None" (HomeAssistant)
Group HomeAssistant_Sensor "binary_sensor:None" (HomeAssistant)
Group HomeAssistant_Door "binary_sensor:door" (HomeAssistant)
Group HomeAssistant_Motion "binary_sensor:motion" (HomeAssistant)
Group HomeAssistant_Presence "binary_sensor:presence" (HomeAssistant)
Switch HomeAssistant_Config
String HomeAssitant_MQTT "[%s]"
Switch Light1 "Kitchen Light" (Lights, Away_Lights,Bedroom_Lights,HomeAssistant_Switch)["Lighting"] {channel="zwave:device:512:node9:switch_binary", channel="mqtt:topic:3851041d:Light1"}
Switch Front_Door_Sensor "Front Door" <kfrontdoor> (HomeAssistant_Door) {channel="konnected:module:1673491:Zone_6"}
Switch Konnected2_Zone_1 "Door Bell" (HomeAssistant_Sensor) {channel="konnected:module:1673491:Zone_1"}
Switch System_State "Home/Away" (Persist, HomeAssistant_Presence)
Switch Front_Motion_Control "Front Motion Control" (gPresent,HomeAssistant_Motion) {expire="5m,command=OFF"}
I was original using the val actions and publishing directly from the DSL rules but i read somewhere that the jsr223 rules only run one per thread so i moved the publish to the jsr223 script to see if that would make it more stable.
Alas it has not. My setup can run for awhile but then the action starts failing. A restart is only temporary fix.