Calculate increment value based on state of an entity (store car mileage for specific driver)

Hi,
I want to store my car mileage for a specific driver. Can someone help me with the correct template-sensor?

Entities:

  • input_number.odometer is a helper to store the value of my car’s odometer. It is obviously incremented when the car is driven.
  • input_select.driver is a manual dropdown helper for selecting the car’s driver.

What I would like to create is a template-sensor that tracks the mileages for specific driver.

So in simple terms; IF driver xyz AND odometer increases THEN increment new odometer ELSE remain at current value.

I have no clue how I can track the mileage for a specific driver. …

# configuration.yaml
template:
  - sensor:
      - name: Driver xyz milage
        unit_of_measurement: mi
        state: >
          {% if is_state('input_select.driver','xyz') %}
            {{ states('input_number.odometer') }}
          {% else %}
            {{ this.state }}
          {% endif %}

No, sorry. That doesn’t work. Let me explain a little more: I want to track how much mileage is done per driver. Your code just takes over the value from the odometer when driver xyz is selected.

Example:

Moment 1:

  • odometer = 1.000 miles

Moment 2: Driver xyz drives 400 miles

  • odometer = 1.400 miles
  • mileage for driver xyz = 400 miles

Moment 3: Driver abc drives 200 miles

  • odometer = 1.600 miles
  • mileage for driver xyz = 400 miles

Oh right. Use Utility meter tariffs.

Create a utility meter with a tariff for each driver.

Switch tariffs using an automation based on the input select state.

e.g.

utility_meter:
  driver_mileage:
    source: input_number.odometer
    tariffs:
      - Arthur Dent
      - Tricia McMillan
      - Zaphod Beeblebrox

automation to switch tariffs

- id: 841ae395-4646-4c95-8991-14cdd7e9c834
  alias: 'Set Driver'
  trigger:
    - platform: state
      entity_id: input_select.driver
      not_to:
        - unknown
        - unavailable
  action:
    - service: select.select_option
      target:
        entity_id:
          - select.driver_mileage
        data:
          option: >
            {{ trigger.to_state.state }}

This will create three sensors one for each tariff/driver, and only when that tariff is selected will their sensor increment (if the input number increments).

A note on your tariffs. You can use full names with capital letters and spaces, e.g. John Citizen as long as they are matched exactly by your input_select.driver options.

You could actually do away with the automation and input_select.driver if you use the utility meter select.driver_mileage to select your driver instead. It will look just like your input select in a dashboard.

Thanks! That looks like it might work. I have implemented it and will test it tomorrow.

1 Like

It works, small change tho:


- id: 841ae395-4646-4c95-8991-14cdd7e9c834
  alias: 'Set Driver'
  trigger:
    - platform: state
      entity_id: input_select.driver
      not_to:
        - unknown
        - unavailable
  action:
    - service: select.select_option
      target:
        entity_id:
          - select.driver_mileage
        data:
          option: >
            "{{ trigger.to_state.state }}"

Last line between “”.

Thanks!!

No.

You only have to quote single line templates, so:

        data:
          option: >
            {{ trigger.to_state.state }}

or

        data:
          option: "{{ trigger.to_state.state }}"