#json from api into home assistant

1 messages · Page 1 of 1 (latest)

manic vault
#

i launched a weather balloon the other day and have been tracking it on aprs.fi but would love to pull data from their api to add to a dashboard for things like "last heard time" "altitude" "speed" and maybe plot it on a map. but the main goal being to flash a light if its updated and hasnt been heard from for awhile.

i have looked around google but havent found anything quite like this. am i asking too much here or am i just missing a simple solution?

TIA

simple parrot
#

It's definitely possible but maybe not so simple.

#

If you read that and still need further help maybe share some more of the API details and we can walk through it.

manic vault
#

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

# testing for my balloon

rest:
  - resource: 
"https://api.aprs.fi/api/get?name=KQ4TUX-1&what=loc&apikey=******&format=json"
    sensor:
    
      - name: "Name"
        value_template: "{{value_json.name}}"
        
      - name: "Altitude"
        value_template: "{{value_json.altitude}}"
          
      - name: "Last Time"
        value_template: "
{{value_json.lasttime}}"
#

that is what configuration.yaml looks like after trying the rest stuff. but i cant find it anywhere in home assistant so i dont know if its even getting the data

#

the testing for my balloon part is a comment, dunno what discord did to that

pallid gateBOT
#

To format your text as code, enter three backticks on the first line, press Enter for a new line, paste your code, press Enter again for another new line, and lastly three more backticks.
```yaml
example: here
```
Don't forget you can edit your post rather than repeatedly posting the same thing.

simple parrot
#

Maybe try one more time with the formatting, looks like you got half of it

#

If you're comfortable you can DM me the API key privately and I could test the link and see what comes back. (fine too if you don't want to)

manic vault
#

i sent you what the api spits out at me in a dm. im fumbeling around with it now trying a different way in the config file

simple parrot
#

maybe it's {{ value_json.entries[0].altitude }}

manic vault
#

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


# testing for my balloon
rest:
    scan_interval: 120
    resource: "https://api.aprs.fi/api/get?name=KQ4TUX-1&what=loc&apikey=******&format=json"
    sensor:
      - name: "KQ4TUX-1"
        json_attributes_path: "$.response.entries"
        value_template: "OK"
        json_attributes:
          - "name"
          - "time"
          - "lasttime"
          - "lat"
          - "lng"
          - "altitude"
          - "course"
          - "speed"
          - "path"
#

does that look more promising? or should i go back to the first way and add the entries like you said?

simple parrot
#

Entries looks like an array so you'll need to index it

#

Either approach is probably fine.
The first approach will give you 4 sensors, this new approach will give you one sensor with several attributes

manic vault
simple parrot
#

try this:

json_attributes_path: "$.entries[0]"
json_attributes:
  - "name"
  - "time"
  - "lasttime"
  - "lat"
  - "lng"
  - "altitude"
  - "course"
  - "speed"
  - "path"
#

Yeah the [0] means to get the first item in the list. Entries looks like it might return an array of several objects. but in your case you have only one

manic vault
#

in the api i can request data for more than one station, im guessing each station would be its own

#

so now i restart ha and then these will be under devices?

simple parrot
#

a sensor entity, not a device.

manic vault
#

should be a single entity or one for each ?

simple parrot
#

I believe it will be one, with the state OK, and all those json_attributes you listed will be attributes

manic vault
#

i dont see anything under it, theres one thats "kq4tux 1" clicking it shows a history and a logbook

simple parrot
#

What does it look like in developer-tools/states

manic vault
#

unknown

simple parrot
#

ok I got my own key. It worked for me:

manic vault
#

that was from my second one just adding the index?

simple parrot
#
rest:
  scan_interval: 120
  resource: "https://api.aprs.fi/api/get?name=KQ4TUX-1&what=loc&apikey=000000.ZZZZZZZZZZZZ&format=json"
  sensor:
    - name: "KQ4TUX-1"
      json_attributes_path: "$.entries[0]"
      value_template: "OK"
      json_attributes:
        - "name"
        - "time"
        - "lasttime"
        - "lat"
        - "lng"
        - "altitude"
        - "course"
        - "speed"
        - "path"
manic vault
#

oh wow! i must have messed up something somewhere but it works now! thank you so much. i didnt know the api key would have helped that much or i would have sent it over. sorry. but again thank you! now to put it on a map and play with the rest of the stuff haha

simple parrot
#

no problem

#

FYI if you want to map it, you'll need to do a little more manipulation.

#

To map it wants an entity with latitude and longitude attributes

#

So you'll need to make another template to correct that

#

e.g.

template:
  sensor:
    - name: "Weather Balloon"
      state: "{{ state_attr('sensor.kq4tux_1', 'altitude')}}"
      attributes:
        latitude: "{{ state_attr('sensor.kq4tux_1', 'lat')}}"
        longitude: "{{ state_attr('sensor.kq4tux_1', 'lng')}}"
#

(you can put whatever you want as the state, I picked altitude)

manic vault
#

if i wanted to do math to change from kph to mph id have to use the first way i tried right?

simple parrot
#

I would keep what you have and just manipulate it as part of a second sensor

#
template:
  sensor:
    - name: "Weather Balloon"
      state: "{{ state_attr('sensor.kq4tux_1', 'altitude')}}"
      attributes:
        latitude: "{{ state_attr('sensor.kq4tux_1', 'lat')}}"
        longitude: "{{ state_attr('sensor.kq4tux_1', 'lng')}}"
        speed: "{{(state_attr('sensor.kq4tux_1', 'speed') | float) * 0.621371}}"
#

Getting all the attributes at once is more efficient, if you make 4 sensors it's going to hit the API 4x as many times.

manic vault
#

sorry this is the first time im diving this far into ha, that template goes in the config.yaml as well right?

simple parrot
#

yes

#

you can put it in other places too, but by default configuration.yaml is where all this goes

#

just make sure you don't have duplicate template: lines, that won't work

manic vault
#

gotcha, would there be a trick to making the time more friendly to read?

#

so that one i made first (kq4tux) will be an entity i never use right? im using it to make this new sensor that makes everything nice and pretty for me and ill use that one (weather balloon) to make my dashboard

simple parrot
#

yeah you can just leave that alone

#

that's what will be updated from the API, and this pretty sensor will get updated from that

manic vault
#
template:
  sensor:
    - name: "Weather Balloon"
      state: "{{ state_attr('sensor.kq4tux_1', 'altitude')}}"
      attributes:
        latitude: "{{ state_attr('sensor.kq4tux_1', 'lat')}}"
        longitude: "{{ state_attr('sensor.kq4tux_1', 'lng')}}"
        speed: "{{(state_attr('sensor.kq4tux_1', 'speed') | float) * 0.621371}}"
        time: "{{(state_attr('sensor.kq4tux_1, 'time').as_datetime()"
#

thats how i would do the time?

simple parrot
#

I'll do like this:

    - name: "Weather Balloon"
      state: "{{ state_attr('sensor.kq4tux_1', 'altitude')}}"
      attributes:
        latitude: "{{ state_attr('sensor.kq4tux_1', 'lat')}}"
        longitude: "{{ state_attr('sensor.kq4tux_1', 'lng')}}"
        time: "{{ state_attr('sensor.kq4tux_1', 'lasttime') | as_datetime | as_local }}"
    - name: "Weather Balloon (Last Time)"
      state: "{{ state_attr('sensor.kq4tux_1', 'lasttime') | as_datetime}}"
      device_class: timestamp
#

That makes a second timestamp class sensor, which you can use in the UI to show like the relative time

#

and that also formats it in the attributes

#

anyway gotta run for a bit, happy flying 👍