Sending values out of a rule to an MQTT server on a openHAB3 setup

Dear All

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

Thanks in advance

Karl

No.

I think this doesn’t work as you think it does.

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 :person_shrugging: ).

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.

MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1.sendCommand('Ladeleistungsvorgabe')

The whole point about having Items is your rules (and other things) should not know nor should they care that they are connected to MQTT.

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.

As i defined the thing in Main UI i post the YAML

UID: mqtt:topic:openWB_Bridge:openWB_Thing
label: MQTT Thing openWB
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:openWB_Bridge
channels:
  - id: Version
    channelTypeUID: mqtt:string
    label: Version
    description: ""
    configuration:
      stateTopic: openWB/system/Version
  - id: Ladestromvorgabe_LP1
    channelTypeUID: mqtt:number
    label: Sollwert Ladestrom LP1
    description: ""
    configuration:
      stateTopic: openWB/lp/1/AConfigured
  - id: Ladeleistungsvorgabe_LP1
    channelTypeUID: mqtt:number
    label: Sollwert Ladeleistung LP1
    description: ""
    configuration:
      commandTopic: openHAB/lp/1/W
  - id: Spannung_L1_LP1
    channelTypeUID: mqtt:number
    label: Spannung L1 LP1
    description: ""
    configuration:
      stateTopic: openWB/lp/1/VPhase1
  - id: Spannung_L2_LP1
    channelTypeUID: mqtt:number
    label: Spannung L2 LP1
    description: ""
    configuration:
      stateTopic: openWB/lp/1/VPhase2
  - id: Spannung_L3_LP1
    channelTypeUID: mqtt:number
    label: Spannung L3 LP1
    description: ""
    configuration:
      stateTopic: openWB/lp/1/VPhase3

You can see i defined one channels as a command topic

Do i understand right that i don´t need the MQTT publish

and i can do instead use the following command:

MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1.sendCommand('Ladeleistungsvorgabe')

Correct.

That’s one of the whole purposes of Items.

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.

Sorry no it is a number

So i will try immidiatelly

sendCommand('Ladeleistungsvorgabe')

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

sendCommand(123.45)

So now rule workes without an error

but get another error.

Cannot convert ‘Ladeleistung’ to a command type which item ‘MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1’ accepts: [DecimalType, QuantityType, RefreshType].

That’s what I’ve been telling you. ‘Ladeleistung’ is not a number. That’s a string.

I would like to send a number.

So if i change the item and the thing channel to accept text it will send then as payload the value ?

Sorry for my silly questions but im not an IT specialist.

Then send a number.

Yes, if the Item and Channel accept text, sending a string as a command will cause that String to be published to the MQTT topic.

1 Like

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.

1 Like

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.

If this works i really learned a lot
so big thanks to @hafniumzinc and @rlkoshak

Karl

Hello

So now publishing to the MQTT server works but…

after changing Item to string and thing channel to text i now get on the MQTT server the string “Ladeleistung” as payload.

My intention was to send the value of this variable out of the rule so i changed my rule
to the following command:

No quotes!!!

MQTT_Thing_Open_WB_Ladeleistungsvorgabe_LP_1.sendCommand(Ladeleistung)

Here the full working rule: :heart_eyes:

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

Have a ncie day

Karl