Forward group status to MQTT

I use OH3.4 on a RPI4.
I have defined a SUM group for my Solar power inverters.
The group state is the SUM of the two members which are the power output of each inverter.

I need this SUM to be published on MQTT.

I found groups can have a channel, however publishing to MQTT requires a command topic and a group change is not a command.

I ended up defining a Number with a command topic channel and a rule that triggers when a member of the group changes. I then post an update to that number.
Is there a more elegant way to do this ?

You still need the rule but you don’t need the second Number Item and Channel. You can use the publishMQTT Action in the rule to publish the new value straight from the rule.

A more general approach for forwarding items to MQTT…

  1. make sure MQTT Actions are installed in the GUI.
  2. create a group and make all items of interest a member of this group.

(items file) :

Group gPushToMQTT
Number Fronius_Inverter1_Power            (gFroniusInverter, gPushToMQTT)                              { channel="fronius:powerinverter:mybridge:myinverter:powerflowinverter1power" }
  1. Set up MQTT broker. My broker goes by the name mosquitto (.things file)
Bridge mqtt:broker:mosquitto "MQTT Broker" 

4.Then there is a rule to do the actual push.

rule "PushMemberToMQTT"
when Member of gPushToMQTT received update
    val mqttActions = getActions("mqtt", "mqtt:broker:mosquitto")
    var name =
    var value = Math.round ( (triggeringItem.state as Number).floatValue ) // rounding is optional
    mqttActions.publishMQTT ("OpenHabItems/"+ name, value.toString )