#HELP NEEDED: RESTful response (JSON) to attribute, but Returned JSON is a array with no key

1 messages · Page 1 of 1 (latest)

tidal cipher
#

Hello everyone,

I have a problem with the RESTful Sensor.
I want to create a table of my car trips with psa_car_controller and flex-table-card. However, I can't get the trips of my car into the home assistant as a table.

Under the URL http://192.168.2.207:5000/vehicles/trips there is a list of all trips. It looks like this, for example:


[
{
"altitude_diff": 0,
"consumption": 1.38,
"consumption_km": 12.5454545454545,
"distance": 11,
"duration": 22,
"id": 1,
"mileage": 431.2,
"speed_average": 30,
"start_at": "Mon, 05 Aug 2024 05:09:43 GMT"
},
{
"altitude_diff": 15,
"consumption": 2.76,
"consumption_km": 16.8292682926829,
"distance": 16.4,
"duration": 42.4666666666667,
....
},
and so on...
]

I would now like to display this data in a table, with time, distance and consumption.
But I can't manage to transfer the data to a sensor using a RESTful sensor.

I have already tried the following:

- platform: rest
name: corsa_e_trips
resource: http://192.168.2.207:5000/vehicles/trips
scan_interval: 60
value_template: "{{ value_json }}"

Then the sensor is unknown. Because of the result length longer than the max 255 chars.

State max length is 255 characters.

The same occours if i use

value_template: "{{ value }}"

What works a little is the following:

- platform: rest
name: corsa_e_trips
resource: http://192.168.2.207:5000/vehicles/trips
scan_interval: 60
value_template: "{{ value_json | length }}"
json_attributes:
- altitude_diff
- consumption
- consumption_km
- id
...

But then I only get the first value in the status attributes, not a list of all values. This of course means that the table-card cannot render a list.

#

Can someone help me how I can either get the JSON into a property or what I have to adjust so that I can display the trips in a table?
Thank you all very much!

tidal cipher
#

HELP NEEDED: RESTful response (JSON) to attribute, but Returned JSON is a array with no key

silent needle
#

I don't believe you'll be able to do this in a single process. You'll likely need to create multiple sensors but you may still walk in to the 255 character template limit. I have also walked in to this multiple times since json_attributes supports only strings and not templates.

Using this json and jinja as an example, allowing you to tinker with it in Template editor....

{% set value_json = 
[
    {
        "altitude_diff": 0,
        "consumption": 1.38,
        "consumption_km": 12.5454545454545,
        "distance": 11,
        "duration": 22,
    },
    {
        "altitude_diff": 15,
        "consumption": 2.76,
        "consumption_km": 16.8292682926829,
        "distance": 16.4,
        "duration": 42.4666666666667
    }
]

%}
# Get all of the consumptions values in a list.
{% set consumptions = value_json | map(attribute='consumption') | list %}
#

To integrate this, do this:

rest:
  - resource: "http://192.168.2.207:5000/vehicles/trips"
    scan_interval: 3600
    sensor:
      - name: "altitude_diffs"
        unique_id: altitude_diffs
        value_template: "{{ value_json | map(attribute='altitude_diff') | list}}"
      - name: "consumptions"
        unique_id: consumptions
        value_template: "{{ value_json | map(attribute='consumption') | list}}"

And then use a template to bring them all together in to attributes of another sensor.

template:
  - sensor:
      - name: Vehicle Trips
        unique_id: vehicle_trips
        icon: mdi:car
        state: OK
        attributes:
          altitude_diff: "{{ states('sensor.altitude_diffs') }}"
          consumption: "{{ states('sensor.consumptions') }}"
silent needle
#

I've gone so far as to install the flex-table card as well and it seems to work as expected for me using this config for my example above:

type: custom:flex-table-card
title: Vehicle Trips
entities:
  include: sensor.vehicle_trips
sort_by: null
strict: true
clickable: true
columns:
  - data: altitude_diff
    name: Altitude Diff
  - data: consumption
    name: Consumption
    suffix: ' %'
#

To avoid the issue where you might have hundreds of entries in the JSON and using the above rest template(s) causes the 255 char limit to be reached, you may consider limiting the number of entries by using this for the rest integration instead, limiting the number of entries to retrieve to 3 in this case.

rest:
  - resource: "http://192.168.2.207:5000/vehicles/trips"
    scan_interval: 60
    sensor:
      - name: "altitude_diffs"
        unique_id: altitude_diffs
        value_template: |
          {% set limited_entries = value_json[:3] %}
          {{ limited_entries | map(attribute='altitude_diff') | list}}
      - name: "consumptions"
        unique_id: consumptions
        value_template: |
          {% set limited_entries = value_json[:3] %}
          {{ limited_entries | map(attribute='consumption') | list}}