Mqttitude - Battery %

I have got Mqttitude working and showing location on a map by following the instructions on this link Google Maps in a sitemap from a Mqttitude Mqtt message

Now i would like to display the battery %, I assume that you would need to follow the instructions in the link except the map.html part.

but when i check on my item using 192.0.0.1:8080/rest/items/AndroidPaul_Battery
I get Uninitialized

Item

String AndroidPaul_Battery “Paul’s Battery [%.1f %]” (Tests1) { mqttitude=“broker:owntracks/paul/1:state:JS(mqttitude-PHBatt.js)]”}

Transform

var location = eval(’(’ + input + ‘)’);
result = location.batt;

Not sure what the issue is as this is all over my head but i would appreciate some input.

I think you need to use the MQTT binding for this, rather than the Mqttitude binding. Mqttitude uses MQTT but does special handling by parsing out the location and waypoint information. You just want to read the raw payload on the MQTT topic and parse out the battery percentage.

Hope this makes sense!

Hi, Yes that does make sense and i would be able to do it if the battery percentage was sent to its own topic but the message sent to MQTT is quite long and i do not know how to take out the data i need.

here is a example:

received PUBLISH (d0, q0, r0, m0, ‘owntracks/paul/1’, … (109 bytes))
{"_type":“location”,“lon”:-0.0000000,“t”:“u”,“batt”:94,“tst”:1453631526,“tid”:“PH”,“acc”:25,“lat”:00.0000000}

Hi,

there’s also a 2nd wiki page with a example how to process the json data:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "MqttPostionParsePatrik"
  when 
    Item mqttPositionPatrikRaw changed
  then
    var String json = (mqttPositionPatrikRaw.state as StringType).toString
    // {"_type": "location", "lat": "47.5010314", "lon": "8.3444293",
    //    "tst": "1422616466", "acc": "21.05", "batt": "40"}
    var String type = transform("JSONPATH", "$._type", json)
    if (type == "location") {
      var String lat  = transform("JSONPATH", "$.lat", json)
      var String lon  = transform("JSONPATH", "$.lon", json)
      var String acc  = transform("JSONPATH", "$.acc", json)
      var String batt = transform("JSONPATH", "$.batt", json)

      sendCommand(mqttPatrikLatitude,  lat)
      sendCommand(mqttPatrikLongitude, lon)
      sendCommand(mqttPatikAccuracy,   acc) 
      sendCommand(mqttHtcOneBattery,  batt)
    }
  end

with kind regards,
Patrik

1 Like

Would the example not make more sense using postUpdate instead of sendCommand? After all, you’re not commanding an item to go to a specific longitude, just updating its state to be that longitude. Here is a slight modification that uses a Location item mqttPatrikLocation:

import org.openhab.core.library.types.*

rule "MqttPostionParsePatrik"
  when 
    Item mqttPositionPatrikRaw changed
  then
    var String json = mqttPositionPatrikRaw.state.toString
    // {"_type": "location", "lat": "47.5010314", "lon": "8.3444293",
    //    "tst": "1422616466", "acc": "21.05", "batt": "40"}
    var String type = transform("JSONPATH", "$._type", json)
    if (type == "location") {
      var String lat  = transform("JSONPATH", "$.lat", json)
      var String lon  = transform("JSONPATH", "$.lon", json)
      var String acc  = transform("JSONPATH", "$.acc", json)
      var String batt = transform("JSONPATH", "$.batt", json)

      mqttPatrikLocation.postUpdate(new PointType(lat + "," + lon))
      mqttPatrikAccuracy.postUpdate(new DecimalType(acc))
      mqttHtcOneBattery.postUpdate(new PercentType(batt))
    }
  end
2 Likes

Thanks guys, all done now! :slight_smile:

The other option is to use mqttwarn (Google it) to listen for the Owntracks publishes and use that to parse out the battery level (and other values) and republish to individual MQTT topics.

This is what I do personally, as it means those values are available for any other systems or services which are MQTT enabled.

And I definitely agree that you should use postUpdate for these types of item updates.

Ben - (and apologies to all for going slightly off topic) - is there a chance you could share (or point to) how MQTT topic parsing as you describe can be accomplished using mqttwarn? I understand how to extract single values and then push to Openhab, but not for multiple values in one message and to then republish onto separate single topics for Openhab to consume.

Today I am using a rule like the one Patrik references.

Thanks in advance

Sorry I am away from home and unsure when I will be back at this stage. I am pretty sure there are some examples in the mqttwarn readme which should point you in the right direction hopefully.