First commit
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
# Home Assistant files
|
||||
|
||||
Three files to connect Home Assistant to Kilometer Log. They are the same as what the app shows under **Admin → Home Assistant**, filled in for a Volvo XC40.
|
||||
|
||||
| File | Where it goes |
|
||||
|---|---|
|
||||
| `configuration.yaml` | Add to Home Assistant's `configuration.yaml`, then restart Home Assistant. |
|
||||
| `secrets.yaml.example` | Add the line to Home Assistant's `secrets.yaml`, with your `HA_TOKEN` after `Bearer `. |
|
||||
| `automation.yaml` | *Settings → Automations & scenes → Create automation → ⋮ → Edit in YAML*: replace everything with it and save. |
|
||||
|
||||
Before you use them:
|
||||
|
||||
- **Plate:** replace `AB-123-C` in `configuration.yaml` with your car's plate.
|
||||
- **Entity names:** if your car's entities have other names than `sensor.volvo_xc40_odometer` and `device_tracker.volvo_xc40_location`, change them in `automation.yaml`.
|
||||
- **Address:** `configuration.yaml` reaches the app over Docker's internal network, at `http://kilometer-log:3000`.
|
||||
- Home Assistant runs with `network_mode: host`? Use `http://127.0.0.1:3077` and uncomment the `ports:` lines in the compose file.
|
||||
- Using the public address instead? Use the exact `https://` address, without a redirect.
|
||||
|
||||
To test it, open the automation and choose **Run actions**. After a minute, Admin → Home Assistant shows "First reading saved".
|
||||
The automation reads the sensor directly, so this manual test works; older versions used `trigger.to_state` and failed when run by hand.
|
||||
@@ -0,0 +1,31 @@
|
||||
# Kilometer Log — paste this in Settings → Automations & scenes → Create automation → ⋮ → Edit in YAML
|
||||
# Generated from the app's Admin → Home Assistant page. Replace AB-123-C with your plate,
|
||||
# and the sensor/tracker names if yours differ.
|
||||
|
||||
alias: "Kilometer Log: send odometer"
|
||||
description: "Sends the odometer and position to Kilometer Log, which adds the distance as a trip."
|
||||
mode: queued
|
||||
triggers:
|
||||
- trigger: state
|
||||
entity_id: sensor.volvo_xc40_odometer
|
||||
conditions:
|
||||
- condition: template
|
||||
value_template: "{{ states('sensor.volvo_xc40_odometer') | is_number }}"
|
||||
actions:
|
||||
# give the car's position a moment to update after the odometer
|
||||
- delay: "00:01:00"
|
||||
- action: rest_command.kilometer_log_odometer
|
||||
# the app's answer (status and message) shows up in this automation's trace
|
||||
response_variable: kilometer_log
|
||||
data:
|
||||
# read from the sensor itself, so "Run actions" (a manual test) works too
|
||||
odometer: "{{ states('sensor.volvo_xc40_odometer') }}"
|
||||
unit: "{{ state_attr('sensor.volvo_xc40_odometer', 'unit_of_measurement') or 'km' }}"
|
||||
time: "{{ states['sensor.volvo_xc40_odometer'].last_changed.isoformat() }}"
|
||||
# the app looks up the town and country for this position
|
||||
latitude: "{{ state_attr('device_tracker.volvo_xc40_location', 'latitude') }}"
|
||||
longitude: "{{ state_attr('device_tracker.volvo_xc40_location', 'longitude') }}"
|
||||
# only used if the town can't be looked up: the zone name, when the car is in one
|
||||
location: >-
|
||||
{% set s = states('device_tracker.volvo_xc40_location') %}
|
||||
{% if s == 'home' %}{{ state_attr('zone.home', 'friendly_name') }}{% elif s not in ['not_home', 'unknown', 'unavailable'] %}{{ s }}{% endif %}
|
||||
@@ -0,0 +1,15 @@
|
||||
# Kilometer Log — add this to Home Assistant's configuration.yaml, then restart Home Assistant
|
||||
# Generated from the app's Admin → Home Assistant page. Replace AB-123-C with your plate,
|
||||
# and the sensor/tracker names if yours differ.
|
||||
|
||||
rest_command:
|
||||
kilometer_log_odometer:
|
||||
url: "http://kilometer-log:3000/api/ha/odometer"
|
||||
method: POST
|
||||
headers:
|
||||
Authorization: !secret kilometer_log_auth
|
||||
content_type: "application/json"
|
||||
payload: >-
|
||||
{"plate": "AB-123-C", "odometer": {{ odometer }}, "unit": "{{ unit }}",
|
||||
"time": "{{ time }}", "latitude": {{ latitude | to_json }}, "longitude": {{ longitude | to_json }},
|
||||
"location": {{ location | to_json }}, "category": "personal"}
|
||||
@@ -0,0 +1,4 @@
|
||||
# Kilometer Log — add this line to Home Assistant's secrets.yaml.
|
||||
# Use the same token as HA_TOKEN in the app's stack, after "Bearer ".
|
||||
|
||||
kilometer_log_auth: "Bearer <the same token as HA_TOKEN>"
|
||||
Reference in New Issue
Block a user