Build rest command to send data to abetterrouteplanner.com

Hello All,

Recently I have bought a ovms module to read my car metrics.
I allready have this data available in HA but now I want to build a rest command to upload the metrics to abetterrouteplanner.com.
The ovms software has a plugin that does the same but it crashes sometimes and it costs extra data.

I have made a rest command but it does not work as expected.

Error. Url: http://api.iternio.com/1/tlm/send?api_key=32b2162f-9599-4647-8139-66e9f9528370. Status code 400. Payload: b’{“tlm”: {“utc”: 1620813223}’ (I removed the rest of the request for testing)

There is an error in the payload. I think it should not start with b’

rest-command:

rest_command:
  abrp:
    url: 'http://api.iternio.com/1/tlm/send?api_key=32b2162f-9599-4647-8139-66e9f9528370'
    method: GET
    headers:
      content_type:  'application/json; charset=utf-8; application/x-www-form-urlencoded'
      AUTH-TOKEN: !secret abrp_token
    payload:  "{{ state_attr( 'sensor.opel_amperae', 'abrp') }}"

Sensor to get data from:

      opel_amperae:
        friendly_name: "Opel Amperae"
        value_template:  "{{states.sensor.main_batt_soc.state_with_unit}}"
        attribute_templates:
          range: "{{states('sensor.range') }}"
          soc:  "{{states('sensor.main_batt_soc') }}"
          parktime: "{{states('sensor.park_time') }}"
          12v voltage: "{{ states('sensor.12v_voltage') }}"
          speed: "{{ states('sensor.speed') }}"
          latitude: "{{ states('sensor.amperae_latitude') }}"
          longitude: "{{ states('sensor.amperae_longitude') }}"
          altitude:  "{{ states('sensor.amperae_altitude') }}"
          ambienttemp: "{{states.sensor.ambient_temp.state_with_unit}}"
          batterytemp: "{{states.sensor.battery_temp.state_with_unit}}"
          linevoltage: "{{states.sensor.amperae_linevoltage.state_with_unit}}"
          chargecurrent: "{{states.sensor.amperae_chargecurrent.state_with_unit}}"
          chargestate: "{{ states('sensor.amperae_chargestate') }}"
          chargemode:  "{{ states('sensor.amperae_chargemode') }}"
          chargeduration: "{{ states('sensor.amperae_chargeduration') }}"
          chargekwh: "{{ states('sensor.amperae_chargekwh') | float * 0.10 }}"
          chargeinfo: "Chargeinfo: {{ states('sensor.amperae_chargestate') }} {{states.sensor.amperae_linevoltage.state_with_unit}}/{{states.sensor.amperae_chargecurrent.state_with_unit}} total: {{states('sensor.amperae_chargekwh') | float * 0.10}} Kwh"
          network: "{{ states('sensor.amperae_network') }}"
          abrp: "tlm={\"utc\": {{ utcnow().strftime('%s') }},\"soc\":{{ states('sensor.main_batt_soc') }},\"soh\":99,\"voltage\":{{states.sensor.amperae_linevoltage.state}},\"current\":{{states.sensor.amperae_chargecurrent.state}},\"is_charging\":{{ states('sensor.amperae_chargestate') }},\"alt\":{{ states('sensor.amperae_altitude') }},\"lat\":{{ states('sensor.amperae_latitude') }},\"lon\":{{ states('sensor.amperae_longitude') }},\"batt_temp\":{{ states('sensor.battery_temp')}},\"ext_temp\":{{ states('sensor.ambient_temp')}}}"

I was looking for a way to create a json of the data but I can’t get this to work as a template.
{% set abrp_json = {
“utc”: utcnow().strftime(’%s’),
“soc”: states(‘sensor.main_batt_soc’),
“voltage”: states(‘sensor.amperae_linevoltage’)

} %}

There is api documentation available at: Iternio Telemetry API

Can anyone help me out on this ?
Any help is appriciated

Thanks,

Edwin

1 Like

Problem solved by not using payload: but only use url:
also added values ‘speed’, ‘is_parked’ & ‘kwh_charged’

rest-command

  abrp:
    url: "{{ state_attr( 'sensor.opel_amperae', 'abrp') }}"
    method: POST
    headers:
      content_type: 'charset=utf-8; application/x-www-form-urlencoded'

sensor

  opel_amperae:
    friendly_name: "Opel Amperae"
    value_template:  "{{states.sensor.main_batt_soc.state_with_unit}}"
    attribute_templates:
      range: "{{states('sensor.range') }}"
      soc:  "{{states('sensor.main_batt_soc') }}"
      parktime: "{{states('sensor.park_time') }}"
      12v voltage: "{{ states('sensor.12v_voltage') }}"
      speed: "{{ states('sensor.speed') }}"
      latitude: "{{ states('sensor.amperae_latitude') }}"
      longitude: "{{ states('sensor.amperae_longitude') }}"
      altitude:  "{{ states('sensor.amperae_altitude') }}"
      ambienttemp: "{{states.sensor.ambient_temp.state_with_unit}}"
      batterytemp: "{{states.sensor.battery_temp.state_with_unit}}"
      linevoltage: "{{states.sensor.amperae_linevoltage.state_with_unit}}"
      power: "{{ states('sensor.amperae_power') }}"
      chargecurrent: "{{states.sensor.amperae_chargecurrent.state_with_unit}}"
      chargestate: "{{ states('sensor.amperae_chargestate') }}"
      chargemode:  "{{ states('sensor.amperae_chargemode') }}"
      chargeduration: "{{ states('sensor.amperae_chargeduration') }}"
      chargekwh: "{{ states('sensor.amperae_chargekwh') | float * 0.10 }}"
      chargeinfo: "Chargeinfo: {{ states('sensor.amperae_chargestate') }} {{states.sensor.amperae_linevoltage.state_with_unit}}/{{states.sensor.amperae_chargecurrent.state_with_unit}} total: {{states('sensor.amperae_chargekwh') | float * 0.10}} Kwh"
      network: "{{ states('sensor.amperae_network') }}"
      abrp: "http://api.iternio.com/1/tlm/send?token=<your token>&api_key=32b2162f-9599-4647-8139-66e9f9528370&tlm={\"utc\":{{ utcnow().strftime('%s') }},\"is_parked\":{{ states('sensor.amperae_is_parked') }},\"soc\":{{ states('sensor.main_batt_soc') }},\"soh\":99,\"voltage\":{{states.sensor.amperae_linevoltage.state}},\"current\":{{states.sensor.amperae_chargecurrent.state}},\"is_charging\":{{ states('sensor.amperae_is_charging') }},\"kwh_charged\":{{states('sensor.amperae_chargekwh') | float * 0.10}},\"elevation\":{{ states('sensor.amperae_altitude') }},\"lat\":{{ states('sensor.amperae_latitude') }},\"lon\":{{ states('sensor.amperae_longitude') }},\"batt_temp\":{{ states('sensor.battery_temp')}},\"ext_temp\":{{ states('sensor.ambient_temp')}},\"power\":{{states('sensor.amperae_power')}},\"speed\":{{ states('sensor.speed') }} }"

  amperae_is_charging:
    value_template: >
      {% if is_state('sensor.amperae_chargestate', 'charging) %}
        1
      {% else %}
        0
      {% endif %}

  amperae_is_parked:
    value_template: >
      {% if states('sensor.park_time') | int > 300 %} 1 {% else %} 0 {% endif %}

I have used this thread to import vehicle data into HA

Automation:

alias: ABRP upload
description: ''
trigger:
  - platform: time_pattern
    seconds: /10
condition:
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.amperae_is_parked
        below: '1'
      - condition: numeric_state
        entity_id: sensor.amperae_is_charging
        above: '0'
action:
  - service: rest_command.abrp
mode: single
1 Like

Thank you for your work, I had a similar problem and have now managed it through your help.

For all those who are less familiar with it:

Token can be generated in the ABRP app.

Unfortunately, I get only relatively little data via the VW ID API. I use the Volkwagen ID Binding from @skagmo, also thanks to him :slight_smile:

I have solved the whole thing a bit messier, but relatively simple:

rest_command:
  abrp:
    url: 'http://api.iternio.com/1/tlm/send?api_key=<api_key>&token=<api_token>&tlm={"utc":{{ utcnow().strftime(''%s'') }},"soc":{{ states(''sensor.soc_percent'') }},"soh":99,"is_charging":{{ states(''sensor.charging_bool'')}},"car_model":"volkswagen:id3:20:58:mr"}'
    method: POST
    headers:
      content_type: "charset=utf-8; application/x-www-form-urlencoded"

Automation ist also simple:

alias: ABRP upload
description: ''
trigger:
  - platform: time_pattern
    seconds: /10
action:
  - service: rest_command.abrp
mode: single

I also considered if it is better to use the UTC timestamp of the vehicle, since i only get an update of the vehicle data every 10 minutes.

Is there a simple way to transfer the data from “sensor.time”, which is in the format "
17. Juni 2021, 22:34:04 " to the required UTC format?

Thanks again to all who made this possible!

Greetings
Lukas

2 Likes

Thank you!

Your examples helped me to setup the same functionality for my audi.

Thankfully the audi connect integration provides a last update sensor upon which i can trigger the automation to update soc and position.

Greetings

Martin

1 Like

Is it correct that I have to contact iternio by mail to get the api key? no other way?

Hi Andy,

In ABRP there is an option to enable live data. This is the place to retrieve youre api key.
Screenshot is in Dutch language but look for ‘live data settings’ at the bottom of your car profile.
image

Thanks, Edwin

@Edwinth Yeh, there I already picked my token, but I also need the api key…

ah sorry, the apikey is a global key that is used to access the ABRP API.
I use OVMS with abrp upload script. ABRP: abetterrouteplanner.com — Open Vehicles documentation

OVMS_API_KEY : API_KEY to access to ABRP API, given by the developer
const OVMS_API_KEY = “32b2162f-9599-4647-8139-66e9f9528370”

@Edwinth THX!!!

@lorddoener

I’ve tried it with my Kia E-Niro:

rest_command:
abrp:
url: ‘http://api.iternio.com/1/tlm/send?token=Token>I&api_key=32b2162f-9599-4647-8139-66e9f9528370&tlm={“utc”:{{ utcnow().strftime(’’%s’’) }},“soc”:{{ states(’‘sensor.kia_soc’’) }},“soh”:99,“is_charging”:{{ states(’‘binary_sensor.kia_plug’’)}},“car_model”:“kia:niro:19:64:other”}’
method: POST
headers:
content_type: “charset=utf-8; application/x-www-form-urlencoded”

As response I am getting:
400 tlm field missing or is not a loadable JSON

Do I miss something?

Greetings,
Willem

Hello, looks good.
Maybe you used the wrong quotation marks or it was just a formatting error on the forum.Try the following. Alternatively, you can just copy the URL in the browser and paste static values instead of the sensors. You will find the errors quite fast. But I am quite sure that if the following does not work, one of your sensors does not give the value you expect.

Greetings
Lukas

rest_command:
  abrp:
    url: 'http://api.iternio.com/1/tlm/send?token=Token>I&api_key=32b2162f-9599-4647-8139-66e9f9528370&tlm={"utc":{{ utcnow().strftime(''%s'') }},"soc":{{ states(''sensor.kia_soc'') }},"soh":99,"is_charging":{{ states(''binary_sensor.kia_plug'')}},"car_model":"kia:niro:19:64:other"}'
    method: POST
    headers:
      content_type: "charset=utf-8; application/x-www-form-urlencoded"

Hi Willem,

Did you allready found what the problem was ?
If not then test your url in the Developers → template section in your HA.
Check if the url builds correctly with all variables used.

Good luck,

Edwin

Still works fine for me.
Docs at Iternio Telemetry API

I use this

  - platform: rest
    resource_template: 'https://api.iternio.com/1/tlm/send?tlm={ %22utc%22:{{ now().timestamp() | round }}, %22soc%22: {{ states.sensor.kia_soc.state }}, %22soh%22:100, %22is_charging%22: {{ is_state( "binary_sensor.kia_charging", "on") | int }}, "est_battery_range":{{ states.sensor.kia_range.state }}, "odometer":{{ states.sensor.kia_odo_meter.state }}, %22lat%22:{{ states.sensor.mijnkia_canbus.attributes["CanbusLast"]["position"]["Lattitude"] }}, %22lon%22:{{ states.sensor.mijnkia_canbus.attributes["CanbusLast"]["position"]["Longitude"] }}, %22car_model%22:%22kia:niro:19:64:other%22 }&token=<token>&api_key=6f6a554f-d8c8-4c72-8914-d5895f58b1eb'

You can find the token in the app on the phone under car → live data instructions AutoPi.

I’m trying to get this to work as well but for some reason it’s not updating my live data.

This is what my configuration.yaml looks like:

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensor.yaml
homeassistant:
  customize:
    sensor.laadpaal_verbruik_totaal_v4:
      state_class: total_increasing
  packages: !include_dir_named packages
rest_command:
  abrp:
    url: 'http://api.iternio.com/1/tlm/send?api_key=<API key from iternio>&token=<USER token from ABRP app>&tlm={"utc":{{ utcnow().strftime(''%s'') }},"soc":{{ states(''sensor.enyaq_battery_level'') }},"soh":99,"is_charging":{{ states(''sensor.enyaq_is_charging'')}},"est_battery_range":{{ states(''sensor.enyaq_electric_range'') }}}'
    method: POST
    headers:
      content_type: "charset=utf-8; application/x-www-form-urlencoded"

This is my sensor.yaml to create the is_charging sensor

- platform: template
  sensors:
    enyaq_is_charging:
        value_template: >
          {% if is_state('switch.enyaq_charging', 'on') %}
            1
          {% else %}
            0
          {% endif %}

and then I created an automation to upload this to ABRP:

alias: ABRP Telemetry
description: ""
trigger:
  - platform: time_pattern
    minutes: /2
condition: []
action:
  - service: rest_command.abrp
    data: {}
mode: single

However nothing seems to happen. The automation works fine and I get no errors back but nothing is showing up in ABRP?

Hi Ruurd,

First check is if the url works in you’re browser.
Paste it in the template tab under development tools to resolve the entities states/values
Now paste the result in a webbrowser and check if this works and the car is showing online in abrp.

If you get ‘401 Unauthorized Token’ in return then something is wrong with you’re api key.
Check it, fix it and try again.

Thanks for your help. I get this in return when I load it into the developer tab in HA

TemplateSyntaxError: expected token ',', got 'string'

When I pasted earlier into the URL of my browser i got this:

400 tlm field missing or is not a loadable JSON

Now I’m not a developer so I have no clue what it means. I’m assuming you’re dutch, I got this from gathering.tweakers.net so I’m not sure something changed in the syntax since that post: Het grote Škoda ENYAQ iV Home Assistant integratie topic - Smarthome - GoT

1 Like

Some characters are not in the right format. (quotation marks)
try in developers > template 'http://api.iternio.com/1/tlm/send?api_key=<API key from iternio>&token=<USER token from ABRP app>&tlm={"utc":{{ utcnow().strftime("%s") }},"soc":{{ states("sensor.enyaq_battery_level") }},"soh":99,"is_charging":{{ states("sensor.enyaq_is_charging")}},"est_battery_range":{{ states("sensor.enyaq_electric_range") }}}'


Thanks!, looks like this one works, in the developer tab at least :smiley:

This template updates at the start of each minute.

This template listens for the following state changed events:

Entity: sensor.enyaq_battery_level
Entity: sensor.enyaq_electric_range
Entity: sensor.enyaq_is_charging

Rebooting now to test it for real…Hmm…doesn’t seem to do anything to ABRP yet.

When I paste the URL in edge it still states:

400 tlm field missing or is not a loadable JSON

Any thoughts? The entities are correct so this should contain those values right?

Where do you paste the url ? I think you paste the whole script in the template tab ?!

Look at my screenshots in the previous reply and only paste the url and check if al entities resolve to a value.
http://api.iternio.com/1/tlm/send?api_key=<API key from iternio>&token=<USER token from ABRP app>&tlm={"utc":{{ utcnow().strftime("%s") }},"soc":{{ states("sensor.enyaq_battery_level") }},"soh":99,"is_charging":{{ states("sensor.enyaq_is_charging")}},"est_battery_range":{{ states("sensor.enyaq_electric_range") }}}

No I only pasted the URL in the browser. First it came back with invalid key of course but after I fixed that it came back with the message:

But edge of course does not understand these parameters? Shouldn’t I replace those with numeric values to properly test it?