#Help parsing json request as a template

1 messages · Page 1 of 1 (latest)

bitter karma
#

I currently have a shell command that gets a playing status from a music streamer and wanted to know how I could parse the url to the album art image to display it on my lovelace dasboard.

#
shell command:
  cider_now_playing: "curl -X GET 'http://192.168.1.177:10767/api/v1/playback/now-playing' -H 'apptoken: ew6k8q2vdql4c2ewg6pey7r6'"

output

stdout: >-
  {"status":"ok","info":{"albumName":"What Was I Made For? (From The Motion
  Picture \"Barbie\") -
  Single","discNumber":1,"genreNames":["Alternative"],"trackNumber":1,"hasLyrics":true,"durationInMillis":222370,"releaseDate":"2023-07-13T12:00:00Z","name":"What
  Was I Made For? (From The Motion Picture \"Barbie\")","artistName":"Billie
  Eilish","artwork":{"width":3000,"url":"https://is1-ssl.mzstatic.com/image/thumb/Music126/v4/7d/64/76/7d64761e-a9b3-6754-8ae1-b457338beead/23UMGIM77779.rgb.jpg/640x640sr.jpg","height":3000,"textColor3":"222112","textColor2":"12140b","textColor4":"292c1b","textColor1":"090500","bgColor":"86905b","hasP3":false},"playParams":{"id":"i.dlvq22qf64QN5dN","kind":"song","isLibrary":true,"reporting":true,"catalogId":"1696819855","reportingId":"1696819855"},"composerName":"Billie
  Eilish &
  FINNEAS","isrc":"UniversalUSUM72307484","previews":[{}],"currentPlaybackTime":71.09714799999995,"remainingTime":151.27285200000006,"inFavorites":false,"inLibrary":false,"shuffleMode":1,"repeatMode":0}}
stderr: "% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100  1015  100  1015    0     0   167k      0 --:--:-- --:--:-- --:--:--  198k"
returncode: 0
raven burrow
#

Can you capture that JSON output in a variable eg. value_json? Then the path to the URL would be value_json.info.artwork.url.

fast storm
#

Looks like you need a REST sensor

bitter karma
#

What would the config look like for that. I was having issues getting the template right

fast storm
#

specify the resource, the headers, and the template in value_template as mentioned above

bitter karma
#

I have an entity with attributes now but am having issues specifying the right attribute, when I ue this I get null and get a result type of dictionary ```{{state_attr('sensor.cider_now_playing', 'attributes') }}

#

Here is my sensor.yaml config ```yaml

  • platform: command_line
    name: Cider Now Playing
    command: >
    curl -H "Authorization: {{ states('input_text.cider_token_macbook_pro') }}"
    -H "Content-Type: application/json"
    http://192.168.1.177:10767/api/v1/playback/now-playing
    value_template: "{{ value_json.is_playing }}"
    json_attributes:

    • info
  • platform: template
    sensors:
    cider_album_art:
    friendly_name: "Cider Album Art"
    value_template: >
    {% if state_attr('sensor.cider_now_playing', 'info') and
    state_attr('sensor.cider_now_playing', 'info').artwork %}
    {{ state_attr('sensor.cider_now_playing', 'info').artwork.url }}
    {% else %}
    None
    {% endif %}

#

The album art url oneisn't getting anything just yet

fast storm
#

That's not a rest sensor

#

But okay

#

Does that attribute have the fields you're referencing?

bitter karma
#

I think so

fast storm
#

I suggest trying it in devtools -> Templates

#

and states can only be 255 characters

bitter karma
#

Ok

#

I wonder if it would be better to not have a sensor and just have an automation use a template to download the image

fast storm
#

I don't see how you could do that

bitter karma
#
  name: Cider Now Playing
  resource: "http://192.168.1.177:10767/api/v1/playback/now-playing"
  method: GET
  headers:
    Authorization: "{{ states('input_text.cider_token_macbook_pro') }}"
  value_template: "{{ value_json.status }}"  
  json_attributes:
    - info``` I tried giving my hand at a rest sensor but I only get a friendly name attribute
fast storm
#

you were passing the content-type before, and now you're not

#

I don't know if that matters, but it's different

#

you can also review the HA logs

bitter karma
#

Logger: homeassistant.components.rest.util
Source: components/rest/util.py:37
integration: RESTful (documentation, issues)
First occurred: 2:03:39 PM (4 occurrences)
Last logged: 2:18:39 PM

REST result could not be parsed as JSON

#

Logger: homeassistant.helpers.template
Source: helpers/template.py:2769
First occurred: 2:03:38 PM (41 occurrences)
Last logged: 2:22:39 PM

Template variable warning: 'dict object' has no attribute 'status' when rendering '{{ value_json.status }}'
Template variable error: 'None' has no attribute 'artwork' when rendering '{{ state_attr('sensor.now_playing', 'info').artwork.url }}'

fast storm
#

comment out the second template sensor while you're working on the REST sensor

bitter karma
#

ok

#

i tried to give them both unique ids, so I could see them both but that wasn't working quite right

fast storm
#

you also said this in the beginning:

-H 'apptoken: ew6k8q2vdql4c2ewg6pey7r6'"

#

you're not using that