MQTT Owntracks Rule HELP!

Hello All,

So basically trying to capture the data received from owntracks via mqtt and have it populate fields such as latitude, longitude, battery, etc…

I am trying to get another users rule working to no avail,

rule "MQTT Owntracks JC"
  when
    Item JCRawData changed
  then
      logInfo("MQTT", "MQTT Owntracks JC data received")
      val String json = (JCRawData.state as StringType).toString
      val String type = transform("JSONPATH", "$._type", json)
      if (type == "location") {
      val String lat  = transform("JSONPATH", "$.lat", json)
      val String lon  = transform("JSONPATH", "$.lon", json)
      val String acc  = transform("JSONPATH", "$.acc", json)
      val String batt = transform("JSONPATH", "$.batt", json)
      logInfo("MQTT", "MQTT Updating values")
      JCLatitude.postUpdate(lat)
      JCLongitude.postUpdate(lon)
      JCLocation.postUpdate(new PointType(lat + "," + lon))
      JCAccuracy.postUpdate(new DecimalType(acc))
      JCBattery.postUpdate(new PercentType(batt))
    }
end

When mqtt posts to JCRawData i get the following

Error during the execution of rule 'MQTT Owntracks JC': Cannot cast org.openhab.core.library.types.DecimalType to org.openhab.core.library.types.StringType

Can anyone help me get this working!!? pleaaasseee! :wink:

What is in your items file? Curious about the item type of JCRawData but others as well.

Hi @watou,

mosquitto_sub gives me this data to work with:

mosquitto_sub -h localhost -p 1883 -v -t 'owntracks/jcid/#'
owntracks/jcid/jay-cell {"batt":25,"lon":-78.83458136588226,"acc":10,"p":98.40177917480469,"vel":0,"vac":64,"lat":42.77174698192884,"conn":"w","tst":1486605765,"alt":170,"_type":"location","tid":"JC"}

and i only have this so far for items:

* mosquitto */
Number JCRawData "Jay @ Home %d" (persist) { mqtt="<[mosquitto:owntracks/jcid/jay-cell/state:default]" }
Number JCPosition "Position: %d" (persist)
Number JCLatitude "Lattitude: %d" (persist)
Number JCLongitude "Longitude: %d" (persist)
Number JCLocation "Location: %d" (persist)
Number JCAccuracy "Accuracy: %d" (persist)
Number JCBattery "Battery: %d" (persist)

I have also tried different variations of “Switch” “Number” “String” etc…

and my cfg…

### Mosquitto ###
# Optional. The lat/lon coordinates of 'home'
#mqttitude:home.lat=42.7717
#mqttitude:home.lon=-78.8345

# Optional. Distance in metres from 'home' to be considered 'present'
#mqttitude:geofence=100

### Broker Settings ###
mqtt:mosquitto.url=tcp://localhost:1883
mqtt:broker.clientId=openhab
mqtt:mosquitto.user=openhab
mqtt:mosquitto.pwd=
#mqtt:mosquitto.qos=2
#mqtt:mosquitto.retain=true
#mqtt:mosquitto.async=true
#mqtt-eventbus:broker=mosquitto
#mqtt-eventbus:commandPublishTopic=/openHAB/out/${item}/command
#mqtt-eventbus:stateSubscribeTopic=/owntracks/jcid/#

Your items file has incorrect item types. You must choose ones that match the data you want to hold. For example, the raw data should be a String item. Please go back to your source example and also find docs on items and item types. I hope this helps!

Ive changed them all over and over and no luck. I still get

2017-02-09 10:13:38.945 [INFO ] [.io.transport.mqtt.MqttService] - MQTT Service initialization completed.
2017-02-09 10:13:38.948 [INFO ] [o.i.t.m.i.MqttBrokerConnection] - Starting MQTT broker connection 'mosquitto'
2017-02-09 10:14:09.401 [INFO ] [org.openhab.model.script.MQTT ] - MQTT Owntracks JC data received
2017-02-09 10:14:14.356 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'MQTT Owntracks JC': Cannot cast org.openhab.core.library.types.DecimalType to org.openhab.core.library.types.StringType
String JCRawData "RawData" (persist) { mqtt="<[mosquitto:owntracks/jcid/]" }
Number JCPosition "Position: %d" (persist)
Number JCLatitude "Lattitude: %d" (persist)
Number JCLongitude "Longitude: %d" (persist)
Number JCLocation "Location: %d" (persist)
Number JCAccuracy "Accuracy: %d" (persist)
Number JCBattery "Battery: %d" (persist)
rule "MQTT Owntracks JC"
  when
    Item JCRawData changed
  then
      logInfo("MQTT", "MQTT Owntracks JC data received")
      val String json = (JCRawData.state as StringType).toString
      val String type = transform("JSONPATH", "$._type", json)
      if (type == "location") {
      val Number lat  = transform("JSONPATH", "$.lat", json)
      val Number lon  = transform("JSONPATH", "$.lon", json)
      val Number acc  = transform("JSONPATH", "$.acc", json)
      val Number batt = transform("JSONPATH", "$.batt", json)
      logInfo("MQTT", "MQTT Updating values")
      JCLatitude.postUpdate(lat)
      JCLongitude.postUpdate(lon)
      JCLocation.postUpdate(new PointType(lat + "," + lon))
      JCAccuracy.postUpdate(new DecimalType(acc))
      JCBattery.postUpdate(new PercentType(batt))
    }
end

I think I may be getting the errors partly due to not actually getting the correct data from mqtt… { mqtt="<[mosquitto:owntracks/jcid/]" }

Says this needs more fields…

JCRawData only has a 1 in it and i would think it should have that entire mqtt string of data in it no??

<item>
<type>StringItem</type>
<name>JCRawData</name>
<state>1</state>
<link>https://my.domain.net/rest/items/JCRawData</link>
</item>

ok i changed to this

String JCRawData "RawData %s" (persist) { mqtt="<[mosquitto:owntracks/jcid/jay-cell:state:default]" }

and now getting this

<item>
<type>StringItem</type>
<name>JCRawData</name>
<state>{"batt":82,"lon":-78.87596945095038,"acc":165,"p":98.97682189941406,"vac":10,"lat":42.88403628808889,"t":"u","conn":"m","tst":1486652485,"alt":192,"_type":"location","tid":"JC"}</state><link>https://...../rest/items/JCRawData</link>
</item>

and this

2017-02-09 10:42:45.620 [INFO ] [org.openhab.model.script.MQTT ] - MQTT Owntracks JCRawData data received
2017-02-09 10:42:45.642 [INFO ] [org.openhab.model.script.MQTT ] - MQTT Updating values
2017-02-09 10:42:45.645 [ERROR] [o.o.c.s.ScriptExecutionThread ] - Error during the execution of rule 'MQTT Owntracks JC': Could not invoke method: org.openhab.model.script.actions.BusEvent.postUpdate(org.openhab.core.items.Item,java.lang.Number) on instance: null


YAYYYY I got it…

had to change back to this…

rule "MQTT Owntracks JC"
  when
    Item JCRawData changed
  then
      logInfo("MQTT", "MQTT Owntracks JC data received")
      val String json = (JCRawData.state as StringType).toString
      val String type = transform("JSONPATH", "$._type", json)
      if (type == "location") {
      val String lat  = transform("JSONPATH", "$.lat", json)
      val String lon  = transform("JSONPATH", "$.lon", json)
      val String acc  = transform("JSONPATH", "$.acc", json)
      val String batt = transform("JSONPATH", "$.batt", json)
      logInfo("MQTT", "MQTT Updating values")
      JCLatitude.postUpdate(lat)
      JCLongitude.postUpdate(lon)
      JCLocation.postUpdate(new PointType(lat + "," + lon))
      JCAccuracy.postUpdate(new DecimalType(acc))
      JCBattery.postUpdate(new PercentType(batt))
    }
end
1 Like

Ok, next project is a google map. I know you mentioned adding a group. Gotta dig into that now.

Ok when trying to use:

I have the location data…

<item>
<type>LocationItem</type>
<name>JCLocation</name>
<state>42.88382506152628,-78.87612307550532</state>
<link>https://..../rest/items/JCLocation</link></item>

and installed the map.html etc… with

url: "/rest/items/JCLocation?type=json",

in map.html however i get error 500 from the map page in the sitemap and a error “Oops something went wrong” via firefox…

Just getting -

Oops! Something went wrong.This page didn’t load Google Maps correctly. See the JavaScript console for technical details.

You should have success if you use a MapView widget in your sitemap with your Location item (no extra .js files needed).

Thanks mine is working flawlessly. This is an old post :wink: