Updating EV Mileage on my Website through Home Assistant
Situation and Goals
my current car is a model 3, which can be nicely integrated into home assistant to read out the odometer (need to activate the diagnostic for odometer, specifically). how to integrate it is well documented
my website has a simple api request to update the car, with the following syntax:
domain.tld/evs/update?car_id={id:string}&mileage={mileage:integer}&secret={secret:string}"
The Scripting needed in Home Assistant
to edit the yaml files directly, install f.e. 'Studio Code Server'.
secrets.yaml
in secrets.yaml add a line called update_ev_secret. by default, it will then look like this:
# Use this file to store secrets like usernames and passwords.
# Learn more at https://www.home-assistant.io/docs/configuration/secrets/
some_password: welcome
update_ev_secret: {the secret}
configuration.yaml
in configuration.yaml, add a restful command category called rest_command, then add your update method that looks like this:
rest_command:
update_current_ev:
url: "https://domain.tld/evs/update?car_id=5&mileage={{ states(mileage) | round(0) }}&secret={{ !secret update_ev_secret }}"
this rest command will require one parameter of type mileage, of any numeric type, and rounds it to the nearest integer. it will, as well, require the above specified update_ev_secret from the secrets.yaml
automations.yaml
the automation can be mostly set up through the gui, but the resulting modification in automations.yaml should look like this:
- id: "1789566059441"
alias: Update Mileage
description: ""
triggers:
- trigger: state
entity_id: sensor.epona_odometer
conditions: []
actions:
- action: rest_command.update_current_ev
metadata: {}
data:
mileage: sensor.epona_odometer
mode: single
the id will differ every time, and your sensor might be called different. mine is called sensor.epona_odometer as Epona is my cars name.
The Result
the final result is a live-updating odometer on my EV mileage website, which can be found here: EVs of Davepermen