My Arduino Mega (web server) is sending this JSON code to each client connection (GET). The numbers are updated each second.
*
{ “A”: [24], “B”: [121], “C”: [101.9673]}
*
I would like to read data in OH2 in order to create rules based on it and to publish data in HABpanel. Could you help me with an example? Or is there any tutorial to start working on? Thanks.
This is pretty close to what you’re after: [SOLVED] Mqtt Temp and humidity setup There’s a chap half way down wanting to read from a json string (which you are also doing)
You will need the http binding to get your JSON first of all
You will need to cache it. It will be more efficient because you have 3 values in your JSON. Also because the arduino can be slow and not respond to 3 very close in time http requests for three items.
See the http binding for configuration.
The to retreive data from a JSON you need to install the JSONPATH transform then for the value A for example, your JSONPATH would be $.A
BUT the value retrieved will be [24] not 24
You will need to change that in the rule code which means one more item per value
Can you change your Arduino code to get rid of the [ and ]?
Then you will be able to get the values directly with JSONPATH
JSONPATH Transformation installed and Items/my.items file configured
Number EolianEvent { http="<[Eoliana.url:600:JSONPATH($.1.state.event)]" }
String EolianLastUpdated { http="<[Eoliana.url:600:JSONPATH($.1.state.lastupdated)]" }
Number SolarEvent { http="<[Solar.url:600:JSONPATH($.2.state.event)]" }
String SolarLastUpdated { http="<[Solar.url:600:JSONPATH($.2.state.lastupdated)]" }
Number BatteryEvent { http="<[Solar.url:600:JSONPATH($.3.state.event)]" }
String BatteryLastUpdated { http="<[Solar.url:600:JSONPATH($.3.state.lastupdated)]" }
Created this rule
Number EolianEvent { http="<[Eolian.url:600:JSONPATH($.1.state.event)]" }
String EolianLastUpdated { http="<[Eolian.url:600:JSONPATH($.1.state.lastupdated)]" }
rule "Json check"
when Item EolianLastUpdated changed
then
// use the transformation service to retrieve the value
val newValue = transform("JSONPATH", "$.1.state.lastupdated", EolianLastUpdated.state.toString)
//post the new value to the Number Item
EolianEvent.postUpdate( newValue )
logInfo("JSON", " = ".newValue)
end
But, I cannot see any data (.newValue) in LogViewer.
What’s next step?! Where I’m wrong?
Thanks for any ideea.
There is no time information in the json so your LastUpdated items will have to be timestamped differently with a rule
Number EolianEvent { http="<[arduino.url:1000:JSONPATH($.Eolian.[0])]" }
DateTime EolianLastUpdated
Number SolarEvent { http="<[arduino.url:1000:JSONPATH($.Solar.[0])]" }
DateTime SolarLastUpdated
Number BatteryEvent { http="<[arduino.url:1000:JSONPATH($.Battery.[0])]" }
DateTime BatteryLastUpdated
And a rule:
rule "Last updated"
when
EolianEvent changed or
SolarEvent changed or
BatteryEvent changed
then
var String itemName = triggeringItem.name.toString.replace("Event","LastUpdated")
postUpdate(itemName, now.toString)
end
Vincent, I’ve played around but no, not able to make it work.
I have 3 values into Json string sent by Arduino, but I’m trying to make it work the first item/value only - to be much easy to work and understand the process. Then, I’ll extend the code to include all 3 items/values.
So, I’ll try now to read in OH2 the first item from Json only: Eolian
Vincent, as you instruct, I changed to this in html.cfg:
Number EolianEvent { http="<[arduino.url:1000:JSONPATH($.Eolian.[0])]" }
DateTime EolianLastUpdated
The rule created:
rule "Last updated"
when
EolianEvent changed
then
var String EolianEvent = EolianLastUpdated.name.toString.replace("Event", "LastUpdated")
postUpdate(EolianEvent, now.toString)
end
The error in LogViewer:
2018-07-10 12:51:20.240 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'json.rules' has errors, therefore ignoring it: [4,5]: no viable alternative at input 'EolianEvent'
My fault:
I changed your modifications of the var statement. didn’t make sense with only one item.
rule "Last updated"
when
item EolianEvent changed
then
// var String EolianEvent = EolianLastUpdated.name.toString.replace("Event", "LastUpdated")
postUpdate("EolianLastUpdated", now.toString)
end
I have acces to an webpage (conected directly to Arduino via http) where I can see that data from “Eolian” is changing… since the windturbine is rotating right now (little wind outside ).
Yeaaaaaaaaaaaaaaaaaaaaaaaaaaa… that’s it (no: .url):
2018-07-10 13:17:48.564 [vent.ItemStateChangedEvent] - EolianEvent changed from 30.33459 to 39.43356
2018-07-10 13:17:48.589 [vent.ItemStateChangedEvent] - EolianLastUpdated changed from 2018-07-10T13:17:42.967+0300 to 2018-07-10T13:17:48.557+0300
Good, now change the other two items (Remove the .url)
And use this:
rule "Last updated"
when
item EolianEvent changed or
item SolarEvent changed or
item BatteryEvent changed
then
var String itemName = triggeringItem.name.toString.replace("Event","LastUpdated")
postUpdate(itemName, now.toString)
end
No… I have an off-grid strawbale house powered by Wind and Solar wich stored energy in the battery bank. I fired Fibaro and now I’m combining OH2 with Raspbbery to make it smart again.
But an “weather station” will be the next step. Now I’m getting data from individual sensors only via Arduino.