Advice on my first manual template sensor config

I’m still very much learning HA, and need some advice on my first manual template sensor.

Rather than editing configuration.yaml, I’ve tried to follow the guides to support individual sensor and template files.

In configuration.yaml:

sensor: !include_dir_list sensors/
template: !include_dir_list templates/

I created a folder for sensors and templates off \homeassistant\ and its parsing the files.

The first file I have created is a template sensor to record my daily oil usage:

state:
  sensor:
    name: 'Daily oil used'
    entity_id: 'sensor.daily_oil_used'
    unit_of_measurement: L
    device_class: volume
    state_class: measurement
    state: >-
      {% set current_oil_level = states('number.oil_oil_remaining_litres') | float(0) %}
      {% set daily_oil_used = input_number.oil_tank_capacity - current_oil_level | float(0) %}
      {{ '{:.2f}'.format(daily_oil_used) }}

I have tried multiple different versions of this file and I am clearly missing something. Error message is currently:

Logger: homeassistant.config
Source: config.py:357
First occurred: 13:24:25 (27 occurrences)
Last logged: 14:05:20

* Invalid config for 'template' at templates/oil_daily_oil_used.yaml, line 1: expected a dictionary '', got [{'sensor': [{'name': 'Daily oil used', 'entity_id': 'sensor.daily_oil_used', 'unit_of_measurement': 'L', 'device_class': 'volume', 'state_class': 'measurement', 'state': "{% set current_oil_level = states('number.oil_oil_remaining_litres') | float(0) %} {% set daily_oil_used = input_number.oil_tank_capacity - current_oil_level | float(0) %} {{ '{:.2f}'.format(daily_oil_used) }}"}]}]
* Invalid config for 'template' at templates/oil_daily_oil_use.yaml, line 1: 'entity_id' is an invalid option for 'template', check: sensor->0->entity_id
* Invalid config for 'template' at templates/oil_daily_oil_used.yaml, line 1: 'entity_id' is an invalid option for 'template', check: sensor->0->entity_id
* Invalid config for 'template' at templates/oil_daily_oil_used.yaml, line 1: 'state' is an invalid option for 'template', check: state Invalid config for 'template' at templates/oil_daily_oil_used.yaml, line 2: 'entity_id' is an invalid option for 'template', check: sensor->0->entity_id
* Invalid config for 'template' at templates/oil_daily_oil_used.yaml, line 1: 'state' is an invalid option for 'template', check: state

I was also hoping to add some more to the sensor, to check availability of the input value. I can’t do that until I get this working.
Part of the issue may be that I have been reading other posts, some a few years old, and they may not be correct any more.

There could be other issues, but at the very least, the first line should be template: not state:.

1 Like

The first state: should be removed.
It should not be replaced with template, since you are having that part in the configuration.yaml where you include the dir list

entity_id is not an option.
There is a unique_id instead.

2 Likes

Also it should be a list, not a dictionary, note the dashes.

- sensor:
    - name: 'Daily oil used'
      entity_id: 'sensor.daily_oil_used'
      unit_of_measurement: L
      device_class: volume 
      etc...

Thank you, these errors have now cleared.

Current yaml:

sensor:
  name: 'Daily oil used'
  unique_id: 'sensor.daily_oil_used'
  unit_of_measurement: L
  device_class: volume
  state_class: measurement
  state: >-
    {% set current_oil_level = states('number.oil_oil_remaining_litres') | float(0) %}
    {% set daily_oil_used = input_number.oil_tank_capacity - current_oil_level | float(0) %}
    {{ '{:.2f}'.format(daily_oil_used) }}

Now receiving a new error:

Log details (ERROR)
Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:203
integration: Template (documentation, issues)
First occurred: 14:45:31 (1 occurrences)
Last logged: 14:45:31

TemplateError('UndefinedError: 'input_number' is undefined') while processing template 'Template<template=({% set current_oil_level = states('number.oil_oil_remaining_litres') | float(0) %} {% set daily_oil_used = input_number.oil_tank_capacity - current_oil_level | float(0) %} {{ '{:.2f}'.format(daily_oil_used) }}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.daily_oil_used'

input_number.oil_tank_capacity is a value entered on one of my dashboard, its the total capacity of the oil tank. Its set at 1,200 and has been for a long time.

Is there a reason I can’t refer to this within the template sensor? I was trying to avoid hard coding the tank capacity inside the yaml. It is already used successfully within my dashboard and helpers to calculate % used/remaining etc.

Thanks.

I read the documentation on splitting up the files, I thought the - was when you use one of the other types of lookup in the configuartion.yaml file?

I have taken another look at this, trying a few different things.

This now works:

sensor:
  name: 'Daily oil used'
  unique_id: 'sensor.daily_oil_used'
  unit_of_measurement: L
  device_class: volume
  state_class: measurement
  state: >-
    {% set current_oil_level = states('number.oil_oil_remaining_litres') | float(0) %}
    {% set oil_tank_capacity = states('input_number.oil_tank_capacity') | float(0) %}
    {% set daily_oil_used = oil_tank_capacity - current_oil_level | float(0) %}
    {{ '{:.2f}'.format(daily_oil_used) }}

Rather than referencing other variables in the calculations, I instead created an additional local variable for the tank capacity. The error has now gone.

However, it is giving a warning that ‘measurement’ is probably not the correct state_class. Any thoughts on this?

Should I be using total_increasing?

Also, what should I do when the tank is filled? How do I deal with that?

Plus, there is an anomaly on the first day (today), as we are not starting with a full tank, so its showing a huge usage for today.

You did not have to create local variables. You could use it as before, but you would have to use it as you do in the last post here, where you reference it with states(…)

The secret is in the the method you used:

See where it says “list”?

That means it is expecting a list. Not a dictionary.

The way you have it, if you have more than one template sensor it will fail.