Skip to content

Template Sensor for Fuel Percentage

Wayne Drescher edited this page Oct 16, 2024 · 1 revision

How to Create a Template Sensor for Fuel Percentage in Home Assistant

This guide will help you create a template sensor in Home Assistant that converts an entity representing the remaining fuel into a percentage (0-100 range). This will allow the Ultra Vehicle Card to display fuel percentage based on the remaining fuel data.

Steps to Create a Template Sensor

  1. Open Home Assistant UI:

    • Navigate to Settings > Devices & Services > Helpers.
    • Click on Create Helper in the bottom-right corner.
  2. Select the Template Option:

    • Choose Template > Template a sensor from the list.
  3. Configure the Template Sensor:

    • Name: Enter a name for your sensor (e.g., "Fuel Percentage").
    • State Template: Use the following template code:
    {% set max_volume = 100 %}  {# Replace 100 with the max volume of your tank in liters #}
    {% set current_fuel = states('sensor.your_fuel_entity') | float(0) %}  {# Replace 'sensor.your_fuel_entity' with your actual fuel entity #}
    {{ ((current_fuel / max_volume) * 100) | round(0) }}
    

Explanation:

  • max_volume: Replace 100 with the maximum capacity of your fuel tank in liters (or gallons).
  • current_fuel: Replace 'sensor.your_fuel_entity' with your actual entity that tracks the remaining fuel.

Set the Unit of Measurement:

  • Set Unit of Measurement to %.

Set Device Class:

  • Set Device Class to Volume (optional but recommended for categorizing the entity).

Set State Class:

  • Set State Class to Measurement.

Save the Template Sensor:

  • Click Create to save the new template sensor.

Testing and Usage

Once created, the new template sensor will appear in your entity list. It will dynamically calculate the fuel percentage based on the provided values.

You can now use this sensor in the Ultra Vehicle Card by referencing the newly created fuel_percentage entity.