# Converting MQTT android battery level to OpenHAB item

**URL:** <https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525>\
**Category:** General\
**Created:** [10 February 2020 22:16 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525 "2020-02-10T22:16:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheReverendJT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thereverendjt/32/16935_2.png) [@TheReverendJT](https://discourse.nodered.org/u/TheReverendJT)\
**Post date:** [10 February 2020 22:16 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/1 "2020-02-10T22:16:29Z")

</div>

Hi!

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:

> `{"level":6,"isPlugged":false,"timestamp":1581370521414}`

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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 February 2020 22:33 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/2 "2020-02-10T22:33:45Z")

</div>

> [@TheReverendJT](#):
>
> {"level":6,"isPlugged":false,"timestamp":1581370521414}

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1e84f9175947a02b874d4ee340e9681ddee60c3b.png)  
if so, change it to 'Modified Flows' and see how that works.

---

<div class="post-metadata">

**Author:** ![TheReverendJT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thereverendjt/32/16935_2.png) [@TheReverendJT](https://discourse.nodered.org/u/TheReverendJT)\
**Post date:** [10 February 2020 23:02 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/3 "2020-02-10T23:02:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [10 February 2020 23:27 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/4 "2020-02-10T23:27:51Z")

</div>

Perhaps try:

msg.payload=msg.payload.level;

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..

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 February 2020 23:31 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/5 "2020-02-10T23:31:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TheReverendJT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thereverendjt/32/16935_2.png) [@TheReverendJT](https://discourse.nodered.org/u/TheReverendJT)\
**Post date:** [11 February 2020 14:42 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/6 "2020-02-11T14:42:25Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 February 2020 15:24 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/7 "2020-02-11T15:24:13Z")

</div>

> [@TheReverendJT](#):
>
> once it's sent / recieved by MQTT it becomes a string -

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [11 April 2020 15:24 UTC](https://discourse.nodered.org/t/converting-mqtt-android-battery-level-to-openhab-item/21525/8 "2020-04-11T15:24:21Z")

</div>

Closed on 11 April 2020 15:24 UTC

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