Rest Sensor for Spritmonitor.de / Vehicle fuel and cost tracker

When I started to use HA missed out a implementation for the service Spritmonitor.
I created a rest sensor for the API to display the data via markdown card.

This sensor implementation is not perfect but can give you a startingpoint for your implementation or visualization. Feel free to share improvements or optimizations.

Overview about Spritmonitor Service
Spritmonitor calculates the fuel consumption of your vehicle and helps you manage your car costs.
The consumption and cost analysis as well as many other functions are available to you free of charge
after registration. In addition to the web interface, you can also use Spritmonitor with their smartphone apps.

Content:
Implementation of a REST-Sensor to access data from Spritmonitor Service via the API

Prerequisite:

  • Spritmonitor Account: Spritmonitor.de
    You can register for free over the portal

  • BEARER_TOKEN
    a) You can create a new access bearer token on the Spritmonitor Password Page.
    Format: “Bearer < your bearer token >”
    b) !secret configuration:
    The code example is set up with the !secret configuration. Find the official documentation here:
    Storing secrets - Home Assistant

  • APP_TOKEN
    Spritmonitor assigned meanwhile an dedicated APP-Token, so you can use it directly. No individual request needed anymore.
    Token: 190e3b1080a39777f369a4e9875df3d7

Sensor configuration in configuration.yaml
Replace < vehiclex > in the code with your naming/plate

Example: Access 3 vehicles with one API Request
rest:
  - resource: "https://api.spritmonitor.de/v1/vehicles.json"
    authentication: basic
    scan_interval: 3600
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST Sensor
      Application-Id: 190e3b1080a39777f369a4e9875df3d7
      Authorization: !secret spritmonitor_bearer

    sensor:
      - name: "vehicle_<vehicle1>_avgconsumption"
        value_template: "{{ value_json[0].consumption }}"
        device_class: volume
        unit_of_measurement: "l/100km"
        json_attributes_path: "$.[0]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_<vehicle1>_trip"
        value_template: "{{ value_json[0].tripsum }}"
        device_class: distance
        unit_of_measurement: "km"
        json_attributes_path: "$.[0]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_<vehicle2>_avgconsumption"
        value_template: "{{ value_json[1].consumption }}"
        device_class: volume
        unit_of_measurement: "l/100km"
        json_attributes_path: "$.[1]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_<vehicle2>_trip"
        value_template: "{{ value_json[1].tripsum }}"
        device_class: distance
        unit_of_measurement: "km"
        json_attributes_path: "$.[1]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_<vehicle3>_avgconsumption"
        value_template: "{{ value_json[2].consumption }}"
        device_class: volume
        unit_of_measurement: "l/100km"
        json_attributes_path: "$.[2]"
        json_attributes:
          - make
          - model
          - sign

	  - name: "vehicle_<vehicle3>_trip"
        value_template: "{{ value_json[2].tripsum }}"
        device_class: distance
        unit_of_measurement: "km"
        json_attributes_path: "$.[2]"
        json_attributes:
          - make
          - model
          - sign

Visuailization with Markdown Card:

01-09-2024_09-29-41

Markdown card code
<table>
<tr><td>{{ state_attr('sensor.vehicle_<vehicle1>_avgconsumption','model') }}&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle1>_avgconsumption') }}l&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle1>_trip') }} km&ensp;</td>
</tr>
<tr><td>{{ state_attr('sensor.vehicle_<vehicle2>_avgconsumption','model') }}&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle2>_avgconsumption') }}l&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle2>_trip') }} km&ensp;</td>
</tr>
<tr><td>{{ state_attr('sensor.vehicle_<vehicle2>_avgconsumption','model') }}&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle3>_avgconsumption') }}l&ensp;</td>
<td>{{ states('sensor.vehicle_<vehicle3>_trip') }} km&ensp;</td>
</table>

More information you can find on their Github page:


Update 02.02.2025: Requesting of APP-Token is not necessary anymore. Dedicated Token available, see text. Code example updated as well.

Update 06.02.2025: Added remark fĂĽr the !secret configuration which is used in the code example.

3 Likes

Thanks for sharing your code, I’ll give it a go. App token is under review :slight_smile:
Small remark: In your example code the yaml is messed up from line 61. It looks fine on the screen, but there is an indentation on the empty line.

1 Like

Hi Bravo,

thanks for your remark. :grinning:
I corrected it in the code here in the original post.

Hey @mochii,

great implementation! As we at Spritmonitor are getting now more and more requests for App Tokens for your script - could you please add it here, as we don’t want to provide individual instances of your script with different tokens.

Thank you
Spritmonitor

1 Like

Hello,

to anyone who wants to use @mochii 's script, please use the following APP_TOKEN:

190e3b1080a39777f369a4e9875df3d7

Thank you
Spritmonitor

2 Likes

Thanks to Spritmonitor :slight_smile:
Was not clear if the token is used for individual purposes and can/should be official. Based on your response I updated the original post with this information.

Can I get some help please? I copied the config from the original post, made slight amendments, added my bearer code to secrets file (including the new update), rebooted HA and the sensors are created however none of the sensors are updating with data. The only changes I made was unit_of_measurement:

rest:
  - resource: "https://api.spritmonitor.de/v1/vehicles.json"
    authentication: basic
    scan_interval: 3600
    headers:
      Content-Type: application/json
      User-Agent: Home Assistant REST Sensor
      Application-Id: 190e3b1080a39777f369a4e9875df3d7
      Authorization: !secret spritmonitor_bearer

    sensor:
      - name: "vehicle_jt04_abc_avgconsumption"
        value_template: "{{ value_json[0].consumption }}"
        device_class: volume
        unit_of_measurement: "MPG (Imp)"
        json_attributes_path: "$.[0]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_jt04_abc_trip"
        value_template: "{{ value_json[0].tripsum }}"
        device_class: distance
        unit_of_measurement: "mi"
        json_attributes_path: "$.[0]"
        json_attributes:
          - make
          - model
          - sign
         
      - name: "vehicle_gc18_fnp_avgconsumption"
        value_template: "{{ value_json[1].consumption }}"
        device_class: volume
        unit_of_measurement: "MPG (Imp)"
        json_attributes_path: "$.[1]"
        json_attributes:
          - make
          - model
          - sign

      - name: "vehicle_gc18_fnp_trip"
        value_template: "{{ value_json[1].tripsum }}"
        device_class: distance
        unit_of_measurement: "mi"
        json_attributes_path: "$.[1]"
        json_attributes:
          - make
          - model
          - sign

Hi Townsmcp,

check first if the connection is working and tokens are right.
You can test it on the CLI with replacing “BToken” with your token and execute.
The json string with your data should be the response.

curl https://api.spritmonitor.de/v1/vehicles.json -H "Accept: application/json" -H "Authorization: Bearer BToken" -H "Application-Id: 190e3b1080a39777f369a4e9875df3d7"

If this is working, something in your rest or sensor config will be wrong.

1 Like

@mochii thanks for that. I found my issue - it was because I didn’t add Bearer to the secrets files variable, so I have updated that to spritmonitor_bearer: "Bearer XXXXXXXXXXX" rather than spritmonitor_bearer: "XXXXXXXXXXX" and everything sprang to life after a reboot.

Perfect, this is exactly the reason why I created the post. I found it also not straight forward what to put where.