MQTT 2.5 convert string to number

OpenHab 2.0, MQTT 2.59
I’m trying to convert a simple incoming string payload (such as “23.6”) to a numeric item. (This is a single value mqtt payload message - not a json formatted payload).
I’ve been successful in receiving the value into a string item (configuring with paperUI), but have not been able to receive into a number type item (using paperUI)- all attempts result in NAN.
I’ve reviewed the binding documentation and the tried various forms of Incoming Value Transformations using REGEX, and also tried with a blank Incoming Value Transformation.I’m sure this is simple but I’m missing the solution.
I’d appreciate some guidance on this.

my little esp8266 running pubsub only can send a string so interested to see what comes of the thread but anyhow, I guess you could use a rule

Rule example:

		var String cbyte1 = dec.substring(24,26)
		var int int1 = Integer::parseInt(cbyte1,16)
1 Like

Is your incoming value




If the latter, just ensure your openHAB Thing Channel type is number.

Thanks for your response. This is definitely the right solution for my conditions: (When configured as a text channel the values show without the double quotes.) I have been able to consistently create number type channels from these single value string payloads by following theses steps in paperUI>
(1) Add channel to thing through config>Things>selectedThing (+)
(2) choose NumberValue when adding channel
(3) provide ChannelId, Label, MQTT State Topic, Absolute Min & Max,
NO TRANSFORMATIONS, units of measure (temperature)
(4) link to item (o) create new item - type should be Number, category temperature, Dimension Temperature, then (check)
(5) verify resuts in Control Tab

I did try this previously with failed results – not sure why it didnt work - initially UI responds with NAN until a new message is received - this may have mislead me.


1 Like

Hi Andrew. This is exactly what I’m doing - using an ESP8266 to read DS18B20 temp sensors and publish them one at a time using PubSubClient.h. The payloads are strings created with dtostrf():

See the solution below to receive with openhab2 … very simple

Thanks John I’ll give it a try, maybe the unit of measure magic will convert it from C to F

Thanks for taking time to respond – I’ve found a solution (below) that does not require rules.

I do not think so. The binding channel parameter is I reckon about adding text to / stripping text off of a payload.
The number type channel is still just a number channel.

Andrew, if you are creating the messages yourself it would be easy to convert the temperature values to degrees F before building the message. But I can appreciate the need where this is not the case. Also, while I havent tried it, the PubSubClient.h appears to support sending integer payloads:

boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);

1 Like

Hey guys,
I read this thread (and others) carefully, since I have a similar problem, but I don’t come forward.

I try to feed a number item in openhab2 with an MQTT payload, for synchronization reasons with a raspberry pi.
The entry in the items-file in:
Number num1 {mqtt=">[mybroker:number1:command::${command}],<[mybroker:number1:state::${command}]"}

and sitemap file:
Setpoint item=num1, label=“NUM1 [%.0f ]” minValue=5 maxValue=30 step=5

So the solution with a new item in paper UI of @John_Witherspoon is not working for me!

Sending and typecasting in C on the raspberry when the number is changed works but not the other way round, since I’m not able to realize a TYPECAST on openhabian

Any examples? Thanks a lot?

What is your specific problem? Your Item configuration is for the old v1 binding - is that what you have installed?

Yes, I’m working on the old binding!
The problem is:
Receiving a message via MQTT on the named channel (number1) has no impact, since the message has to be transformed to DECIMAL/ NUMBER. How can that be realized?

‘Transformed’ from what? Tell us the format of your incoming MQTT message.

I’m getting string via MQTT and want to convert into a number/decimal! (number item)
Just playing with the thougth to change to MQTT bindung 2.x
Would this help for this problem?

Golly, this is hard work. An example message would be really helpful.

Is there some reason you are not using
What are you expecting ${command} to do in this context?

Hey @rossko57 ,

no there is no reason (just a copy past error :man_facepalming:), but that solved my problem!
Thank you!