RESTful Sensor - Parsing JSON

I’m trying to extract the current activity of my Harmony hub, from HA-BRIDGE into a sensor using the RESTful sensor combined with the value_template option, using value_json.

However it seems I can’t quite stumble upon the correct format to extract the JSON, I’ve tried various combinations (bad practice but I can’t share those combinations as I haven’t been saving them) - wondering if anyone can assist me with the correct template/jinja values?

I want to get the first “PowerOff” from activity:label.

[{"hub":"LivingRoom","activity":{"label":"PowerOff","suggestedDisplay":"Default","id":-1,"activityTypeDisplayName":"Default","controlGroup":[],"tuningDefault":false,"fixit":{"39894601":{"id":"39894601","power":"OFF","alwaysOn":false,"relativePower":false},"39802617":{"id":"39802617","power":"OFF","alwaysOn":false,"relativePower":false},"41940410":{"id":"41940410","power":"OFF","alwaysOn":false,"relativePower":false},"40031105":{"id":"40031105","alwaysOn":false,"relativePower":false}},"type":"PowerOff","icon":"Default"}}]

My configuration thus far with all errors removed is:

- platform: rest
  resource: http://<REDACTED>/api/devices/harmony/show
  method: GET
  value_template: '{{ value_json }}'

Thanks!

Try

value_template: '{{ value_json.activity.label }}'

Thanks for the reply… That’s one I did try - no go unfortunately

ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'list object' has no attribute 'activity'

Ah. The whole thing is a list. You want to pick the first element, so my next guess would be

    value_template: '{{ value_json[0].activity.label }}'

Perfect, that worked - thank you!

I had tried value_json.activity[0] and other combinations like value_jason.activity.label[0] and had ended up thinking I was completely on the wrong track. Appreciate your help.

Is there a reason you are not using the harmony component?

It resolves all of that for you.

Partly just because I never did use it - I set up Maddox’s harmony-api for that purpose before the component was released.

Partly a design decision for my home - I don’t want to have to restart HomeAssistant every time I expose a device to the Echo, so am working towards replacing emulated_hue with ha-bridge. And I’d rather have one component managing all that, with the status synced into HomeAssistant (this is just personal preference).

And finally, the main reason which I’m sure most people here can relate to… I feel like tinkering haha

1 Like

I had a feeling it was something like this :slight_smile:

hello some one please help me with this

im trying to get below json value on rest sensor.

sensor setup

  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=aljubail&country=SaudiArabia&method=2
    name: Prayer Time
    value_template: '{{ value_json.fajr }}'

json

{"code":200,"status":"OK","data":{"timings":{"Fajr":"04:11","Sunrise":"05:17","Dhuhr":"11:42","Asr":"15:13","Sunset":"18:07","Maghrib":"18:07","Isha":"19:13","Imsak":"04:01","Midnight":"23:42"},"date":{"readable":"16 Apr 2017","timestamp":"1492321943"}}}

i get the empty value
but if i change value_template

value_template: '{{ value_json.code }}'

i get the sensor value 200

The field you want is part of a structure, so you need to specify the outer parts of that structure to get at it. Try

value_template: '{{ value_json.data.timings.Fajr}}'
1 Like

Hi @gpbenton thanks for the reply

it worked for Fajr but I get “unknown” in few other sensor value

this is my sensor config

  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Fajr
    value_template: '{{ value_json.data.timings.Fajr }}'
    
  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Sunrise
    value_template: '{{ value_json.data.timings.Sunrise }}'
    
  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Dhuhr
    value_template: '{{ value_json.timings.Dhuhr }}'
    
  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Asr
    value_template: '{{ value_json.data.timings.Asr }}'
    
  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Maghrib
    value_template: '{{ value_json.timings.Maghrib }}'

  - platform: rest
    resource: http://api.aladhan.com/timingsByCity?city=jubail&country=SA&method=2
    name: Isha
    value_template: '{{ value_json.timings.Isha }}'

please help

1 Like

You are missing the .data part for the other sensors :wink:

ohh :smile: thank you so much…

is it possible to run an automation when the current time and sensor value are same?

Probably, but I think you would need a template trigger to do that, and it is beyond my Yaml capabilities. I suggest you start a new topic for that question.

Ok Thank you

Hi, Thanks for sharing this lovely script. Now i’ve it replicated in my HA as well :slight_smile: Did you manage to get it to trigger automations?

I made a PR to resolve multiple values from the JSON response into attributes:
https://github.com/home-assistant/home-assistant/pull/13677

Feel free to comment or review

Hi @gpbenton and all,

I am trying to connect my surveillance Camera with my HA. I so far managed to use a RESTful Sensor that fetches the state of the camera. and for that I send the following GET command:

platform: rest
resource: http://myInstarIPCameraAddress/cgi-bin/hi3510/param.cgi?cmd=getioattr
name: Camera

And as expected and also detailed in the camera CGI commands documentation, it returns the following, exactly in this format:

var io_enable="0";
var io_flag="1";

when I simulate the command in an API tester, it returns the same and that the Content-Type is text/html.
Where I am blocked is how to extract the 0 or 1 value and put it in a usable sensor in HA.

thanks in advance for the help,
Amir.
I also posted my question here: Fetch and format data from a device with a REST sensor