Rest-Sensor help

Hello all,

i need your help again.

I am trying to create a sensor with the help of the rest command.

The Problem is, that the resonse is full of informations.
But lets start at the beginning.

I want to work with the following API:

https://www.autoaid.de/developers/api_docs/ (and some more is here: https://developers.autoaid.de/api_docs/)
I am able to get some informations like make/model etc

with this:

- platform: rest
    resource: https://api-production.autoaid.de/cc/v0.1/status/vehicles/VINVNxxxxxx
    name: Make_car_01
    value_template: '{{ value_json.make }}'
    json_attributes:
      - make
    headers:
      Authorization: "xxxxxxxxxx"
      content-language: "en_US"
  - platform: template
    sensors:
      date:
        friendly_name: 'Make'
        value_template: '{{ states.sensor.json_make.attributes["make"] }}'

This is kind of easy (i still needed a lot time to understand this)

What i am looking for is the value of the fuel_level, which i get when i request

/status/vehicles/{vehicleId}

So, i am not sure how to get to this, as this is kind of within the response. The paramter “value” is mutiple times in this response as well.

I know i could work with a nested Json but i am not able to get this to work.

Hopefully you understand what i am trying to achive here.

Anyone here wo can help me ? Thanks a lot

Try this:

- platform: rest
    resource: https://api-production.autoaid.de/cc/v0.1/status/vehicles/VINVNxxxxxx
    name: Make_car_01_fuel_level
    value_template: '{{ value_json.eventStatusList[2].eventData.value }}'
    headers:
      Authorization: "xxxxxxxxxx"
      content-language: "en_US"

Paste the following sample data into the Template Editor and then experiment with the template shown at the very bottom:

{% set value_json = 
{
"deviceStatus": {
    "id": "861359033439520",
    "hardwareType": "autoaid Telematic",
    "hardwareVersion": "C4",
    "firmwareVersion": "1.0 M1",
    "deviceGroupId": null,
    "createTimestamp": "2019-04-03 09:27:24.810002",
    "modifyTimestamp": "2019-05-22 11:53:07.334445",
    "vendor": "autoaid",
    "isConnected": true,
    "vehicleId": "VINYV1ZW25UDK1332944",
    "tripId": "90f228de5c611c7148d5",
},
"vehicleStatus": {
    "id": "VINYV1ZW25UDK1332944",
    "vin": "YV1ZW25UDK1332944",
    "tecDocMakeId": 120,
    "tecDocModelId": 38603,
    "tecDocVehicleId": null,
    "make": "VOLVO",
    "model": "V60 II (225)",
    "vehicle": null,
    "fuelType": null,
    "kba": null,
    "numberPlate": null,
    "deviceId": "861359033439520",
    "tripId": "90f228de5c611c7148d5",
    "createTimestamp": "2019-04-10 13:58:09.149606",
    "modifyTimestamp": "2019-05-22 11:53:07.330029"
},
"eventStatusList": [{
    "eventType": "POSITION",
    "eventDataType": "GEO",
    "eventData": {
        "createTimestamp": "2019-04-10 13:58:23.100201",
        "modifyTimestamp": "2019-05-22 11:50:57.000000",
        "lon": "13.323766",
        "lat": "52.520815"
    }
    },{
    "eventType": "MILEAGE",
    "eventDataType": "PARAMETER",
    "eventData": {
        "createTimestamp": "2019-04-18 06:32:57.854499",
        "modifyTimestamp": "2019-05-22 11:50:20.276000",
        "value": "2237",
        "unitId": 111,
        "unitType": "km"
    }
    },{
    "eventType": "FUEL_LEVEL",
    "eventDataType": "PARAMETER",
    "eventData": {
        "createTimestamp": "2019-04-24 08:10:28.266681",
        "modifyTimestamp": "2019-05-22 11:50:20.276000",
        "value": "8.3",
        "unitId": 90,
        "unitType": "l"
    }
}]}
 %}

{{ value_json.eventStatusList[2].eventData.value }}
1 Like

Hi,

thanks for the answer, I tried the steps from you post.

The Result is nothing, I mean the filed stays empty, no result at all.

But I have to say, am not the familiar with the template editor. But I guess I did all correct (I removed the last line “{{ value_json.eventStatusList[2].eventData.value }}” from the template editor entry.

Are you saying that this new sensor:

sensor.make_car_01_fuel_level

has no state value, not even unknown?

If you removed the last line then you are definitely unfamiliar with the Template Editor. :slight_smile:

The purpose of the Template Editor is to test templates. The last line is a template. I offered it to you so you could experiment and learn how to create templates to extract data.

Hi,

thanks for the answer.

So yes, then I have to admit I do know nothing… sorry.

I attached to screens to show you what i mean, that nothing is displayed.
Hope it helps

20

So,

I just saw that there is in fact a value, not the correct one, but there is something.
But the Template Editor did not show anything.

So gathered through the text and was able to find the correct event. (18).
Thanks for you help here !!

1 Like

Hello,

i have to reopen this, as I just figured out, that the eventstatuslist is not fixed, the length as the sorting could be different anytime.

So i was wondering if this is still possible, of course I need to work now with a if-loop or for each loop, but that’s it, I do not know if this is possible at all.

In its simplest form, the template would look like this:

{% for i in value_json.eventStatusList %}
   {% if i.eventType == 'FUEL_LEVEL' %}
      {{ i.eventData.value }}
   {% endif %}
{% endfor %}

It iterates through eventStatusList, looking for eventType equal to FUEL_LEVEL. When it finds it, it reports eventData.value.

Hi,

the sound more then correct, but how would i write that in the config, I am lost here, sorry

This is wrong …

value_template: '{% for i in value_json.eventStatusList %}
                                  {% if i.eventType == 'FUEL_LEVEL' %}
                                  {{ i.eventData.value }}
                                  {% endif %}
                                  {% endfor %}'
value_template: >
  {% for i in value_json.eventStatusList %}
     {% if i.eventType == 'FUEL_LEVEL' %}
       {{ i.eventData.value }}
     {% endif %}
  {% endfor %}
1 Like

Thank you.

This is working fine and as expected ! Thanks !

1 Like