Json_attribute

Hi all

I need to map some Attributes from a Json-File to Attributes of one Restful-Sensor.

I’ve tried a lot of combination (try an error) but now I have to ask some experts:-)

Example-Data:

  • platform: rest
      resource: https://fahrplan.search.ch/api/route.json?from=Einsiedeln&to=Z%C3%BCrich,+F%C3%B6rrlibuckstr.+60
      name: blablababla
      json_attributes:
          - ?????? if I want connections.0.departure from Json
          - ?????? if I want connections.0.arrival  from Json
          - ?????? if I want connections.0.legs.0.line  from Json
      value_template: '{{ value_json.??????????? }}' 
    

I need a sensor with anything as state and some attributes from the json-File.

thx

value_template: ‘{{ value_json.blablababla.??? }}’
where the ??? are the attribute you want to map to the sensor… I am not familiar with the json_attributes key and I would just create multiple sensors for multiple attributes.

The documentation shows you what you need.

sensor:
  - platform: rest
    name: JSON time
    json_attributes:
      - date
      - milliseconds_since_epoch

So how about something like:

platform: rest
  resource: https://fahrplan.search.ch/api/route.json?from=Einsiedeln&to=Z%C3%BCrich,+F%C3%B6rrlibuckstr.+60
  name: blablababla
  json_attributes:
      - connections[0].departure
      - connections[0].arrival
      - connections[0].legs[0].line
  value_template: '{{ value_json[0].duration }}' ```

Are you sure this will work? Looking at the code (sensor/rest.py), I don’t think you can use anything but a “top level” JSON key for json_attributes. E.g., I think you can only use something like connections in this case. Then you’d have to use template sensors the get the individual pieces.

If this does work I’d be happy to hear that! :slight_smile:

It’s not working:-)

No attributes were mapped.

Of course I can create a sensor for each attribute I need, but then I will pull the hole Json-File with each call. That’s not necessary, because I get the hole file with the first call and the infos I need are all there…

I’m on the wrong way???

I didn’t think that would work (although I was hoping! :slight_smile: ) But what should work is to use the rest sensor to get the JSON results and put the connections key into an attribute. Then with template sensors you can extract the pieces you need, albeit into individual sensors.

Take a look at this reply to another person trying to do a similar thing and let me know if you think that would work for you, and if you need any help:

great!

It seems to work.

 - platform: rest 
    resource: https://fahrplan.search.ch/api/route.json?from=Einsiedeln&to=Z%C3%BCrich,+F%C3%B6rrlibuckstr.+60
    name: blabla
    json_attributes:
      - connections
    value_template: '{{ value_json.connections[0].duration }}' 
    scan_interval: 60    
  - platform: template
    sensors:
      connections_departure:
        value_template: >
          {{ state_attr('sensor.blabla', 'connections')
               [0]['departure'] }}
      connections_duration:
        value_template: >
          {{ state_attr('sensor.blabla', 'connections')
               [0]['duration'] }}               
      connections_line:
        value_template: >
          {{ state_attr('sensor.blabla', 'connections')
               [0]['legs'][0]['line'] }}   

thx lot

2 Likes