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