Files
2026-11-17 20:25:07 +01:00

32 lines
1.7 KiB
YAML

# 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 %}