Hey there, I have been working on trying to get HomeAssistant to read data from an existing graphite database, and I have gotten as far as getting the JSON via API, and have run into some trouble while parsing it. Im hoping the HA community might help, and maybe teach me something new.
I believe the issue is that its not really in appropriate JSON format, and I am either going to have to regex or otherwise extract the data… and I have tried a little bit, but theres got to be a better way.
Heres what my results look like from an API call for the most recent datapoint:
[
{
"target": "GarageFridge.temp",
"tags": {
"name": "GarageFridge.temp"
},
"datapoints": [
[
3.7975857242827153,
1747081200
]
]
}
]
Or in raw as HA receives it:
[{"target": "GarageFridge.temp", "tags": {"name": "GarageFridge.temp"}, "datapoints": [[3.7975857242827153, 1747081200]]}]
This include a timestamp, which is mostly irrelevant since HA will manage its own timestamps, and I am not looking to duplicate my history, mostly trying to use HA for alerts since Grafana alerts are less configurable.
I think my goal would be to gather the first datapoint as my sensor state, right? Using a JSONPath evaluation tool, I was able to isolate my datapoint that I wanted at $.datapoints[0]…[0], but I could never quite figure out how to get HA to accept it as a state.
I tried many iterations of something like this, but could never get it quite right:
sensor:
- platform: rest
name: "GarageFridge Test"
resource: (My obfuscated API path)
value_template: "{{ value_json.datapoints[0]..[0] }}"
unit_of_measurement: "°C"
headers:
Authorization: Bearer (Also obfuscated bearer token)
The best I ever got was having the entire API reply as the state, or unknown somehow, I assume because this result is an array and not a properly formatted JSON result with named parameters.
In a hacky workaround, I got it to grab the value by setting the value template to “{{ value[85:90] }}”, but this only works if I only care about the first datapoint, and the data characters is always in the same place.
Is this API call result something I can parse via value_json, or is regex wizardry more appropriate(and if so, how), or is my value template via character count really the best way of doing this with my limited data source?
Alternatively, am I just dumb and missing something obvious?
As for why I dont connect the sensor directly to HA, then have HA write to Graphite, and then consume that info in Grafana… I already have a lot of sensors writing directly to Graphite, consumed via Grafana Dashboards, and I would rather not have to change all of the sensor’s data ingestion paths since they are rather rock solid and stable.