Converting MQTT android battery level to OpenHAB item


Firstly, thanks to all you guys for sharing your knowledge, I'm sure there are many lurkers like myself who benefit so much from this forum.

I've been working on an OH 2.5 system running on a RPi2 relying heavily on Node-RED as a rule engine for a while now, and have generally gotten by searching for similar "help me!!" posts, however this one has me stumped! I have no experience of coding, or even Linux other than what I have picked up along the way.

My latest aim is to integrate phone battery level into my presence detection flow, which currently relies solely on checking for a wifi connection, to reduce nuisance switching (and upsetting my wife) should a wifi connection drop due to both batteries running out.

I am sending battery percentage as part of a string (level, power connected, timestamp) through MQTT from each phone, which then updates the corresponding OpenHAB item. I then want to check this item during the presence detection flow so that it only continues if the last received number was higher than x.

The payload is:


Brace yourselves for a silly question... I am currently using:

msg.payload = msg.payload.split('level')[1].substr(2,2);
return msg;

Within a function node to extract the number I want from the payload, however from my understanding, this is will pass 2 numbers, and 2 numbers only, so should the battery be at 100% or below 10%, the flow breaks. Is there a way that I can extract either 1, 2 or 3 digit numbers?

Sorry for the long winded simple question, I feel context is important.

While I am appeasing the wife.....

Whenever I restart Node-RED, several of my flows trigger. Would this be a persistence issue or is there a setting somewhere that I've missed to stop that happening?

If that is in msg.payload you should be able to determine if it is an object. (sent it to a debug and check)

If so you should be able to address the level as msg.payload.level

When you say 'restarting Node-RED, do you mean shutting it down and starting it again with a 'node-red-start' or do you mean you do a deploy? If you are doing a deploy then check to see if you are doing a full deploy
Screen Shot 2020-02-10 at 5.35.29 PM
if so, change it to 'Modified Flows' and see how that works.

It is an object. How would I go about converting that level to a number? Sorry, my knowledge is woeful.

Regarding restarting, it's only when I restart, not on deploy.

Perhaps try:


As your payload is an object and not a string.
If it is a string send it through the json node to convert the json string into an object because your string looks like perfectly formatted stringified object..

It's already a number - notice there are no quotes around it, so what @Christian-Me says should work

As for restarting NR, do the flows that trigger have inject nodes you have the Inject once after [] seconds, then checked off?

@Christian - you are totally right, it is an object, however once it's sent / recieved by MQTT it becomes a string - I will try your method this evening to extract the relevant info on the Android side.

@zenofmud - I shall check this evening, from memory, it is only the flows with inject nodes that fire, so that could well be the issue.

Thanks both for your help on this, much appreciated.

In the MQTT In node, if set the Output dropdown to Parsed JSON then it will convert the JSON to an object for you, which avoids feeding it through a JSON node.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.