Get attributes from json file

Hi all,

I´m changing this topic because I now trying with Restful to get the attributes out of my file.

All I want to do is get system, channel 1-8 and pitmaster out of follwing json file:

{"system":{"time":"1527327597","soc":31,"charge":false,"rssi":-89,"unit":"C"},"channel":[{"number":1,"name":"KT-BEEF","typ":3,"temp":83.90,"min":10.00,"max":92.00,"alarm":1,"color":"#0C4C88"},{"number":2,"name":"PIT","typ":1,"temp":109.00,"min":80.00,"max":140.00,"alarm":1,"color":"#22B14C"},{"number":3,"name":"Kanal 3","typ":1,"temp":999.00,"min":10.00,"max":150.00,"alarm":1,"color":"#EF562D"},{"number":4,"name":"KT-PORK","typ":1,"temp":79.70,"min":10.00,"max":92.00,"alarm":0,"color":"#FFC100"},{"number":5,"name":"Kanal 5","typ":1,"temp":999.00,"min":10.00,"max":45.00,"alarm":0,"color":"#A349A4"},{"number":6,"name":"TempAussen","typ":1,"temp":26.70,"min":10.00,"max":35.00,"alarm":0,"color":"#804000"},{"number":7,"name":"GR","typ":3,"temp":113.80,"min":80.00,"max":130.00,"alarm":0,"color":"#5587A2"},{"number":8,"name":"Kanal 8","typ":0,"temp":999.00,"min":10.00,"max":35.00,"alarm":0,"color":"#5C7148"}],"pitmaster":[{"id":0,"channel":2,"pid":2,"value":100,"set":110.00,"typ":"auto"},{"id":1,"channel":1,"pid":0,"value":0,"set":50.00,"typ":"off"}],"api":{"version":"2"}}

I tried with following:
sensor restful:

  • platform: rest
    name: JSON Channel 1
    resource: http://192.168.0.29/data
    value_template: ‘{{ value_json.channel.0 }}’
    json_attributes:

    • name
    • temp
  • platform: template
    sensors:
    name:
    friendly_name: ‘Name’
    value_template: ‘{{ states.sensor.json_channel_1.attributes[“name”] }}’
    temp:
    friendly_name: ‘Temp’
    value_template: ‘{{ states.sensor.json_channel_1.attributes[“temp”] }}’

any hint would be very appreciated. I tried several hours and days now.

Thanks
Philipp

I think the problem is json_attributes are not attributes of the result of value_template, but attributes of value_json. I don’t know if this would work, but maybe try:

json_attributes:
  - channel.0.name
  - channel.0.temp

that did not help :frowning:

sensor restful:
  - platform: rest
    name: JSON system
    resource: http://192.168.0.29/data
    value_template: '{{ value_json.channel.0 }}'
    json_attributes:
      - channel.0.name
      - channel.0.temp
        
  - platform: template
    sensors:
      name:
        friendly_name: 'name'
        value_template: '{{ states.sensor.json_system.attributes["channel.0.name"] }}'
      temp:
        friendly_name: 'temp'
        value_template: '{{ states.sensor.json_system.attributes["channel.0.temp"] }}'

still no attributes from the json file

So maybe try commenting out the template sensors for now and see if sensor.json_system works, and if so, what the attributes look like. Or are you getting errors for that, too?

Hi Phil,

unfortunately not. No attributes:
10

with this:

  • platform: rest
    name: JSON system
    resource: http://192.168.0.29/data
    value_template: ‘{{ value_json.channel.0 }}’
    json_attributes:
    • channel.0.name
    • channel.0.temp

In the log it is only an error with webostv but this is working correctly
2018-05-26 15:29:46 ERROR (MainThread) [homeassistant.loader] Unable to find component sensor.webostv

thanks for your help!

Greetings
Philipp

I think it should be similar to the openweather example at the bottom of this page

But I´m maybe not familar enough with json to get this figured out. I tried so many combination!

Thanks
Philipp

Hmm, sensor.json_system's state looks like a valid dictionary. I wonder if

{{ states.sensor.json_system.state.name }}

and

{{ states.sensor.json_system.state.temp }}

would work.

Hmm, maybe not, because I think states are really strings. But maybe worth a try.

You may end up having to write a python script.

Hi Phil,

should I add the states as attributes?

In the meantime I tried to follow the OWM example and got some attributes but also Object errors for channel. but the system seems to work

  - platform: rest
    name: WT_grilltest
    resource: http://192.168.0.29/data
    value_template: '{{ value_json["channel"][0]["name"].title() }}'
    json_attributes:
      - system
      - channel

I would prefer to not try python :slight_smile:

Thanks
Philipp

Ultimately where do you want the data to go and in what form? Do you want each individual piece of data shown in a different sensor? Do you want one sensor whose state somehow shows all the values, and if so, in what form? Something else? I guess it’s kind of hard to provide any other suggestions until that’s understood better.

I really haven’t used the RESTful Sensor much, but it might be that it can’t do what you’re trying to do in one sensor.

HI Phil,

what I want is something similar to like the Sonos panel or any other panel.
see the temparature, name, min, max and if clicking then change the name, min, max, etc…

that´s what I finially want. At them moment I would be happy to only show the name, temp, min, max as attributes.

Thanks
Philipp