Creating an odometer entity that can be manually updated and retains long term history

Responses in my feature request thread indicate that HA already has the necessary pieces to create (and update) odometers for my e-bikes. There isn’t currently a HA integration for our bikes, so I created odometer entities manually using custom variables in my config, i.e.:

var:
  bike_odometer_hsd:
    friendly_name: "Tern HSD Bike Odometer"
    unique_id: bike_odometer_hsd
    unit_of_measurement: mi
    icon: mdi:counter
    restore: true
  bike_odometer_nbd:
    friendly_name: "Tern NBD Bike Odometer"
    unique_id: bike_odometer_nbd
    unit_of_measurement: mi
    icon: mdi:counter
    restore: true

I’ve been using a Siri Shortcut on my phone that calls var.update to manually update them every week or two. These vars don’t retain long-term data though, so I’d love to replace them with proper sensor entities as described in the responses to my other thread.

I read the long-term stats documentation, did many troubleshooting attempts using logged errors as guidance, and am to the point where I need help. I have yet to see my new entities to appear in the entities list. My config includes:

template:
template odometers: !include templates/sensors/odometers.yaml

…and templates/sensors/odometers.yaml contains:

- sensors:
  - name: "bike_odometer_hsd"
    device_class: distance
    # state_class: total_increasing
    state_class: measurement
    unit_of_measurement: mi
    state: "{{ states('var.bike_odometer_hsd') | int }}"
    
  - name: "bike_odometer_nbd"
    device_class: distance
    # state_class: total_increasing
    state_class: measurement
    unit_of_measurement: mi
    state: "{{ states('var.bike_odometer_nbd') | int }}"

I wasn’t sure what to put as the state for these, since there’s no actual device or integration to set/update their values. Figured I could continue using the custom variables entities I created previously until there’s a proper integration. Where am I going wrong in the config, and what mechanism should I use to update the odometer values for each bike?

That all looks ok (from a brief glance at the custom variables docs). You dont need the |int filters but they also do no harm.

If this is the first time you have used the template integration you will need to restart Home Assistant. After that you can use reload templates for subsequently added template sensors.

If you did restart and still are not seeing the entities sensor.bike_odometer_hsd and sensor.bike_odometer_nbd are there any relevant errors related to these templates in Settings → System → Logs?

FYI you dont need to use underscores in the names. e.g. You can use

  - name: "Bike Odometer Hsd"

and it will generate the same entity id as well as looking better in your dashboard.

The template sensors will update automatically when the variables update. i.e. when you do this:

EDIT: also as another test, check that this returns the expected values in the Developer Tools → Temlplates editor:

{{ states('var.bike_odometer_hsd') }}
{{ states('var.bike_odometer_nbd') }}