How does one create a sensor from a JSON format API?

I run an app called Pi-Star on a dedicated RasPi. I wanted to view some of the log files from this application in HA. Pi-Star is a read-only file system that is similar to HA, in that it is possible to modify things as root, but it is best left untouched, unless one has a strong understanding of how it works. With that hands-off approach in mind, I wrote a little script on HA that grabs the entire log file with curl and yanks out the data I want with an embarrassingly sloppy series of bash commands. It drops the final data into text files which HA picks up with a file sensor. If you sat all day and tried, you would not be able to think of a less efficient way of doing this, but hey, it worked. But buried on the support forum, someone just casually mentioned that there is an API for the data I want, and it is formatted in JSON. Perfect, right? Except I have spent several days worth of free time trying to grab the data, and getting nowhere. I know nothing about REST or JSON, but it looks so simple and logical. But I am the guy who spent months downloading a file every 10 seconds, breaking that file into five tiny files with awk and sed, and pulling them into HA with an automation, so logic probably isn’t my strong suit. It is a good thing I am in management, where my single brain cell actually manages to appear strong.

The JSON looks like this:

[
{
"time_utc": "2022-11-16 19:22:02",
"mode": "DMR Slot 2",
"callsign": "MIXXX",
"name": "William",
"callsign_suffix": "",
"target": "TG 91",
"src": "Net",
"duration": "",
"loss": "",
"bit_error_rate": "",
"rssi": ""
},
{
"time_utc": "2022-11-16 19:22:02",
"mode": "DMR Slot 2",
"callsign": "KNXXX",
"name": "James",
"callsign_suffix": "",
"target": "TG 31656",
"src": "Net",
"duration": "13.4",
"loss": "0%",
"bit_error_rate": "0.0%",
"rssi": ""
},
{
"time_utc": "2022-11-16 19:21:47",
"mode": "DMR Slot 2",
"callsign": "WAXXX",
"name": "Richard",
"callsign_suffix": "",
"target": "TG 91",
"src": "Net",
"duration": "96.6",
"loss": "0%",
"bit_error_rate": "0.1%",
"rssi": ""
},

</snip after dozens of pages>

I have been taking pot shots like this:

- platform: rest
  name: PiStar_Data
  json_attributes:
    - time_utc
    - callsign
    - target
    - duration
    - loss
    - bit_error_rate
  resource: http://<the-IP>/api/last_heard.php
  value_template: "{{ value_json.radio }}"
- platform: template
  sensors:
    callsign:
      friendly_name: Callsign
      value_template: "{{ state_attr('sensor.json_radio', 'callsign') }}"
    name:
      friendly_name: Name
      value_template: "{{ state_attr('sensor.json_radio', 'name') }}"

and this:

- platform: rest
  name: PiStar_Callsign
  json_attributes_path: "$.[0].time_utc"
  json_attributes:
    - callsign
    - name
    - target
    - duration
    - loss
  resource: http://<the-IP>/api/last_heard.php
  value_template: "{{ value_json[0].time_utc }}"

and this:

- platform: rest
  name: DMR_Log
  json_attributes:
    callsign: '[0].callsign'
    name: '[0].name'
    duration: '[0].duration'
  resource: http://<the-IP>/api/last_heard.php
  value_template: 'value_json[0]'

- platform: rest
  name: DMR_name
  resource: http://<the-IP>/api/last_heard.php
  value_template: 'value_json[0].name'

- platform: rest
  name: DMR_callsign
  resource: http://<the-IP>/api/last_heard.php
  value_template: 'value_json[0].callsign'
 
- platform: rest
  name: DMR_duration
  resource: http://<the-IP>/api/last_heard.php
  value_template: 'value_json[0].duration'

Are you laughing yet? :slight_smile: I grew up typing CALL -151 on a 6502 Apple ][. But that was a lifetime ago, and this is now. I hate the feeling that I am asking someone else to do my homework for me, but if someone would be kind enough to just nudge me in the right direction, I would be grateful.

1 Like

Provided basic Python :snake: skills (or the will to learn them) it may be more straight forward to build an integration than using templating :grimacing:

farmio, you are 10000% correct, of course, and those skills would benefit me in several other hobbies as well. This might be the push that I need to do it, too. I’ve watched a few entry level Python “How-To” videos, and it seems perfectly understandable, at least at the very simple levels I would be concerned with.

I was just hoping to delay having to immerse myself in a whole new learning project just to add this one thing to my HA dash, but you know what? It is as good an excuse as any.

Best wishes, mate, hope you (and everyone) had a fantastic holiday.