Number item won't update with new MQTT value

Hello!

Looking for some assistance.

A number item is receiving updates through incoming MQTT messages, the item is defined as follows:

Number esic_temp1 "ESIC 1 Temperatur [%.1f] °C" <temperature> (esic_Chart,Temperature) {mqtt="<[mqttPi:ESIC/1/1/temp:state:default]"}

The item is updated correctly for values >= 10.0 deg C resulting in the updated value and the following log message:

2016-07-09 23:54:01 - esic_temp1 state updated to 12.56

For temperatures < 10.0 deg C the item update fails resulting in the following log message:

[WARN ] [.c.i.events.EventPublisherImpl:80 ] - given new state is NULL, couldn’t post update for ‘esic_temp1’

Of course I’ve checked that the MQTT messages are delivered correctly by using a parallell MQTT.fx client subscribing to the topic.

I noticed this behaviour as both of my ESIC 433 MHz sensors which are presented in my Openhab client clearly limits the value. See attached picture showing one of the sensors readings.

For some reason one value (7.19) got through just 2 hours ago. This makes it even more strange to me.

Is the problem that the sent value only has one digit to the left of the decimal point and thus somehow making it difficult for the update process? In that case I guess one way around this could be for me to multiply the value at the Arduino and the divide it again at the Openhab server in some way.

Thanks! /Niklas

The number in the MQTT message may have formatting where there are leading or trailing spaces, and that would not be acceptable for constructing a DecimalType. Instead of default in the binding string, you could have JS(number.js) which removes any whitespace on either side.

transform/number.js:

result = input.trim();

Then see if the other values get through.

That’s some strange magic @watou but it worked. I understand the cause but I’m quite lost regarding Java.

Should I have managed my message at the Arduino in a different way before sending it? This fix at Openhab seems to me as the wrong way of handling this.

But this is a different topic, my C+±fu is not that evolved…, yet.

My C++ code:

temp = float(decodedTemp)/16.0-50;
    
//Temperature
String topString = "ESIC/" + String(decodedData[0]) + "/" + String(decodedData[1]) + "/temp";
topString.toCharArray(top_buff, topString.length()+1);
dtostrf(temp, 5, 2, message_buff);
client.publish(top_buff, message_buff);

Thank you for this!

Glad it worked. If there is a way to use dtostrf so it doesn’t add leading or trailing spaces, then every float your Arduino code publishes can be handled by the MQTT binding without needing to be transformed first by that JavaScript one-liner. But it’s not really a problem now, either!

Wow, this is a new avenue for me in adapting openhab to my needs… can anyone just add a few more words of explanation? i’m trying to understand where to put these parts… i can see that i need to add the JS(number.js) part in the binding as mentioned, but i’m not sure where to put the rest of it…

transform/number.js:

result = input.trim();

If anyone could just explain this a little further it would be greatly appreciated

The MQTT binding deals with MQTT messages as text strings, but if it is bound to a Number item, the binding tries to convert the text string message into a valid DecimalType state. So if the string is a proper representation of a number (no leading or trailing spaces, decimal digits, no thousands separator, a period to separate the whole number part from the fractional part), then the conversion works. But if the MQTT message is formatted otherwise, the conversion fails, unless you first transform the message into a valid string representation of a number. Ideally, the remote code that publishes the message would be corrected so that openHAB doesn’t have to transform it in order to make it a proper string representation of a number, but is instead published in the correct format in the first place.

Since I don’t know specifically what you’re asking, that’s all I have for the moment!

Yes, i’ve tried to attack from the sending side of it but not easy… i really just want to know where the second part or parts are to be put

result = input.trim(); ````

Is this a script i have to put somewhere ? i assume not in the Items page..?

is the first line the location and the second a 1-line script? i just have no idea where to start with understanding it..

Sorry for the lack of clarity on my part

No worries.

You would create a file under the existing transform directory (on Linux using apt-get install of openHAB 1.8, the directory is /etc/openhab/configurations/transform). The file name would be number.js, which is a text file containing the JavaScript line

result = input.trim();

The in your .items file, you would use JS(number.js) in the MQTT binding config string, like:

Number MyNumber { mqtt="<[broker:/topic:state:JS(number.js)]" }

Is if your MQTT publisher sends the message " 12.45 " (without the quotes), then the JavaScript transform would trim off the spaces, and then try to use it (successfully) as a decimal number.

12:08:39.437 [DEBUG] [avaScriptTransformationService:61 ] - about to transform ’ 1329512’ by the Java Script ‘number.js’

Perfect! easy and effective!

Thanks

1 Like

I had the same issue and changed the arduino code:

Before: dtostrf(celsius, 4, 1, charMsg);
After: dtostrf(celsius, 0, 1, charMsg);

Now the module is sending data w/o leading space.