Work with openHAB3 on a Raspberry 4. Now i tried to include MQTT in my setup.
Viewing items from a broker is ok but publish to the broker does´n work up to now:
Installed MQQT Bridge → Online
Defined MQTT Thing with 3 Channels → ONLINE
The first 2 topics are state topice and are transferred from the broker correct to openhab
Version
Ladestromvorgabe
the 3. Channel is defined as a command Topic:
MQTT Command Topic:
openHAB/lp/1/W which should be the topic on the MQTT Broker
First question: Have i to define this topic on the Broker before i can use it with openhab in a rule ???
My MQTT items:
String MQTT_Thing_Open_WB_Version "Version" {channel="mqtt:topic:openWB_Bridge:openWB_Thing:Version"}
Number MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1 "Sollwert Ladestrom Lp1" {channel="mqtt:topic:openWB_Bridge:openWB_Thing:Ladestromvorgabe_LP1"}
Number MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1 "Sollwert Ladeleistung Lp1" {channel="mqtt:topic:openWB_Bridge:openWB_Thing:Ladeleistungsvorgabe_LP1"}
Then i make a rulefor calculationg the power out of the current in a simple way :
val Spannung = 220
rule "Umrechnung Ladestrom auf Ladeleistung"
when
Item MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1 changed
then
var Number Ladeleistungsvorgabe = (MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1.state as DecimalType)*Spannung*3
logInfo("INFO: ","Ladestromsollwert {} Ladeleistung {} ", MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1, Ladeleistungsvorgabe)
val mqttActions = getActions("mqtt","mqtt:broker:openWB")
mqttActions.publishMQTT("MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1" , "Ladeleistungsvorgabe")
end
The rule triggers correct and in the log the calculated value is also ok but the rule terminates with:
Script execution of rule with UID"openWB-1" failed: null in openWB
Maybe somehwere can help my out and guide in the right direction
You have a Generic MQTT Thing. This Thing Has Channels. Presumably one of them has a command topic defined (you don’t show your MQTT Thing configs so ).
If that’s the case, then to publish to that MQTT topic you’d just sendCommand to the Item linked to that Channel.
You only use the MQTT Action when you are publishing to something that isn’t linked to an Item as a command topic.
Okay, you’ll need the UID of that to use the Action in a rule.
Irrelevant to using Action in a rule.
That would publish a fixed string payload “Ladeleistungsvorgabe” to a fixed topic “MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1”, which is a funny sort of topic but you can have that if you want.
No Items are involved here at all.
I wonder if the null error is failure to get Actions from the named broker Thing.
The normal way to “send stuff” is to command an Item that is linked to an MQTT channel with a commandTopic defined.
However, the one Channel that you have defined that has a command topic is a mqtt:number type Channel. That needs to be linked to a Number Item. Ladeleistungsvorgabe isn’t a number. It’s a String. You need to change the type of the Channel to mqtt:text and the type of the linked Item to String.
That is sending the String ‘Ladeleistungsvorgabe’ as a command to a Number Item linked to an mqtt:number Channel. That won’t work and you will get errors.
So you either need to change the types of the Channel and the Item to accept text like that or you need to send an actual number as the command
Cannot convert ‘Ladeleistung’ to a command type which item ‘MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1’ accepts: [DecimalType, QuantityType, RefreshType].
But, unless something has changed in the last few months, you cannot change the Channel type by simply modifying the YAML in the UI - you will have to delete that Channel, and make a new String type Channel.
Thanks
Important hint.
I fighted a little bit as i have items textual and therefore have first to comment out the channel part from the item.( to unlink it) Otherwise there is no possibility to delete the channel.
Then delete the channel in the thing and make a new one with text and afterwards change the item to string.
But now i am ready.
Will have a look on it tomorrow when sun shines and the value should increase step by step.
val Spannung = 220
rule "Umrechnung Ladestrom auf Ladeleistung simple"
when
Item MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1 changed
then
var Ladeleistung = (MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1.state as Number)*Spannung*3
logInfo("INFO: ","Ladestromsollwert {} Ladeleistung {} ", MQTT_Thing_Open_WB_Ladestromvorgabe_LP_1, Ladeleistung)
MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1.sendCommand(Ladeleistung)
end
Thanks again for your patience
Usage (and hopefully also understanding) of MQTT in both directions is a big step forward in my openhab knowledge and opens better understandig for the next steps.
Now implementation of shellys it next