Put item values into JSON string

I’ve found a lot of solutions how to strip JSON responses into individual item values, but I need it the other way around:
I have several data coming from my ambient sensors like temperature, air pressure and humidity and would like to send those to my virtual weather station on Openweathermap.

Example:

Number Temperature { mqtt="<[mosquitto:/esp8266four/temperature/shadow:state:default]" }
Number Barometer <pressure>  { tinkerforge="name=baro" }

Those values, together with an actual unix time stamp and my station id, need to go into a JSON like

[
  {
    "station_id": "123456789",
    "dt": 1479817340,
    "temperature": 18.7,
    "pressure": 1021,
  }
]

I know how to put the data into vars, but that’s it :grinning:

var Number temp = Temperature.state as DecimalType

Do I need the JsonPath transformation or http binding handling the Json? Or something different to that?

Thx for any hints.

If you’d like it in a rule, I suggest to do it simply as a String variable:

	var weatherstring = '[{"station_id": "123456789","dt": 1479817340,"temperature": '+Temperature.state.toString()+',"pressure": '+Barometer.state.toString()+',}]'
 	...

with that string you can upload it (depending on the method/api provided)

You could put all the values in a comma separated string and send that string to a javascript transform with a JSON.stringify function to return a fully valid JSON

Vincent’s idea is a good one for complex JSON. For something as simple as this I would use Thomas’s approach. But you can break it up a little if it is easier for you to read using a StringBuilder.

val weatherString = new StringBuilder
weatherString.append("[")

  weatherString.append("{")
    weatherString.append('"station_id": "123456789",')
    ...
  weatherString.append("}")

weatherString.append("]")

weatherString.toString // to get the JSON out.

Notice how I split up the adding of the opening and closing brackets and I use indentation to help show where you are in the JSON hierarchy as you add the values to the String.

You end up with a hole lot more lines of code but this is one place where the extra lines makes it more clear to the human what is going on. So long as the code passes syntax checking, you should always prioritize human legibility over terseness.

1 Like

Thanks Rich, I also ensures that the JSON is valid and one doesn’t have to work out where to insert the [ and { other : and , that make up a JSON string, not to mention the "

Regards

1 Like

Thanks to all three of you, this is what I have mixed together so far :sunglasses:
No errors in openhab.log, so I assume it’s working.
Unfortunately the OWM Api does not support calling individual stations anymore, so I need to find a way to verify where my data is hiding :grinning:

rule "owm post weather data"
when
    Time cron "0 */15 * * * ?"
then
	var Number epoch = now().getMillis()
	var Number tempfahr = TemperatureShadow.state as DecimalType
	var Number temp = tempfahr*9/5+32;
	var Number hum = Humidity.state as DecimalType
	//var Number dew = temp-(100-hum)/5;
	var Number barohpa = Barometer.state as DecimalType
	var Number baromin = barohpa/33.864
	var String URL="http://api.openweathermap.org/data/3.0/measurements?appid=yxyxyxyxyxyxyxyxy"
	var weatherstring = '[{"station_id":"yxyxyxyxyxyxyxyxy","dt":'+epoch+',"temperature": '+temp+',"pressure": '+baromin+'}]'
	sendHttpPostRequest(URL,"application/json",weatherstring)
	logInfo("EXTRA","OWM: Temperature send to OWM: " +TemperatureShadow.state +" °C " + "(" + temp + " °F)"  )
	logInfo("EXTRA","OWM: Pressure send to OWM: "+baromin +" in"  )
	logInfo("EXTRA","OWM: Humidity send to OWM: "+hum +" %")
	//logInfo("EXTRA","OWM: Dewpoint send to OWM: "+dew +" °F ")
	logInfo("EXTRA","OWM: Timestamp sent: "+epoch)
	logInfo("EXTRA","OWM: API sent: "+URL+weatherstring)
end