[SOLVED] Send avgTemp to broker

From sensors scattered around the house, I get the average temperature from:
Group: Number: AVG avgTemps
Is it possible to send this value to the MQTT broker?
I would like the client to receive this value, which is a thermostat that I am building with an ESP8266 module
Thanks for your help
David

Yes

rule "average temp changed"
when
    Item avgTemps changed
then
    avgTempsMQTT.sendCommand(avgTemps.state.toString)
end

Create an item called avgTempsMQTT link it to an MQTT channel

As an alternative, you can use the publishMQTT Action. Replace the sendCommand with

    val mqttActions = getActions("mqtt","mqtt:systemBroker:embedded-mqtt-broker")
    mqttActions.publishMQTT("mytopic",avgTempsMQTT, true) // the "true" sets the retained flag to on

That is an extra line of code but doesn’t require creation of a new Item.

A third possibility which might work though I don’t know it it’s been tested is to create a Generic Thing with a publishing Channel like you would need to for Vincent’s approach. Then link that Channel to the Group. There’s a chance that will work.

work, thanks!
another small brick for my thermostat project !!

Please tick the solution post, thanks

Hi everybody,

i am using exactly the same code to publish a topic within a rule. That’s works fine.
But now i want to set the retain flag and tried to set the third parameter to true.
It does not work in my environment.

I am using the actual version from openhabin 2.5.1

Any ideas or suggestions ?
Is there a further setting to change?

Thanks in advance
Christian

In what way?

I would excpect that the published message is flagged as retained.
But it isn’t a retained message in the queue.

The feature is new for version 2.5
Any further info you can provide would be helpful.
The message gets to your broker?

Oh, it’s new in version 2.5.
Because the thread was from October 2019, i thought it was already a feature.
But never mind.

I installed the mqtt binding via Paper UI and configured the mqttbroker thing to connect to a MQTT Mosquitto Broker.
Its installed on the same machine and its not the embedded Broker.

I setup a rule in the openhabian rules folder.

val mqttActions=getActions(“mqtt”,“mqtt:broker:MQTTBroker”)

mqttActions.publishMQTT(“Stromzaehler/Strom/Zaehlerstand”,(tmp_zaehlerZaehlerstand + 1).toString, true);

The message will be published and appears in the broker, but the retained flag is not set.

The last time I looked at it I was getting retained messages from the Action. How are you verifying that the flag is/is not set? There might need to be an issue filed.

I verify it with the tools MQTT-Explorer and MQTT.fx.
Are there other ways to verify it ?

Currently it seems to switch between work and does not work.
But i can’t see a pattern, or perhaps i have not the right understanding to check it.

No, just gathering information.

Is this call to publishMQTT the only thing that is publishing to the topic?

Can a subscriber tell whether a message it gets is a normal immediate “pass through”, or an older retained message?

In general, yes. There is a flash on the incoming message that indicates whether it was restrained or not I’m pretty sure. In OH, there is no way to tell though.

Freudian slip?

1 Like