Help with template sensor calculation

Hello,

i will use a esp32 with Tasmota 13.0.0 flashed and a SR04 distance sensor connected to measure the volume of my oiltank. Tasmota works fine and i already have the distance entity of the sensor. In the developer tools i tested the syntax for the calucation but i am not able to put it in the configuration.yaml and get the new entities to work. I use the latest version of Home Assistant running on a VM on my NAS. This is the test from the developer tools which works:


{% set sensor = {
  
  "oilmeter_sr04_distance": 6,
  "oilmeter_100percent": 4050,
  "oilmeter_liter": 3915
  
} %}

sensor.oilmeter_liter
{{ (75*75*(180 - sensor.oilmeter_sr04_distance))/1000*4 }}

sensor.oilmeter_percent
{{ 100 / sensor.oilmeter_100percent * sensor.oilmeter_liter }}

This is my attempt in the configuration.yaml

template:
  - sensor:
    # Füllstand in Liter
      - name: oilmeter_liter
        unique_id: oilmeter_liter
        unit_of_measurement: "L"
        state: >-
            {{ (75*75*(180 - states('sensor.oilmeter_sr04_distance')))/1000*4 }}

  - sensor:
    # Füllstand in Prozent
      - name: oilmeter_percent
        unique_id: oilmeter_percent
        unit_of_measurement: "%"
        state: >-
            {{ 100 / 4050 * states('sensor.oilmeter_liter') }}

I think its “just” a problem with the syntax but even after hours try to find the correct syntax it wont work. Please give me a tip what is wrong. thx in advance

you need to correctly format all of your code by using three backticks ``` on the line before and the line after the code portion of your post.

without it it’s hard (or impossible) to find syntax errors.

thx for your fast reply. I try to fix it. I dont thinks there is a space or something else to much or less. I think “states” in front of the sensor cause the problem.

States are strings. You need to convert them to numbers before you can do math with them:

template:
  - sensor:
    # Füllstand in Liter
      - name: Oilmeter Liter
        unique_id: oilmeter_liter
        unit_of_measurement: "L"
        state: >
            {{ (75*75*(180 - states('sensor.oilmeter_sr04_distance')|float(0)))/1000*4 }}
        availability: "{{ states('sensor.oilmeter_sr04_distance')|is_number }}"
  - sensor:
    # Füllstand in Prozent
      - name: Oilmeter Percent
        unique_id: oilmeter_percent
        unit_of_measurement: "%"
        state: >
            {{ 100 / 4050 * states('sensor.oilmeter_liter')|float(0) }}
        availability: "{{ states('sensor.oilmeter_liter')|is_number }}"

Thank you very much. Works!

Hopping onto the back of this one, as it seems relevant. I’m trying to create (or modify, don’t mind which) another sensor by adding two days from a sensor containing a date “2023-07-26”.

Is the principle the same, as I don’t think I’m even near being right and don’t understand how I’d apply this to a date.

This is what I’ve currently got in the template too but it fails with TemplateSyntaxError: expected token ‘)’, got ‘string’

{% set tomoz = (states'sensor.tracker_date_tomorrow') + timedelta(days=2) %}

Your first bracket is in the wrong place. Probably just a typo.

Again, a reminder that states are strings. Make it a datetime first.

{% set tomoz = states('sensor.tracker_date_tomorrow') | as_datetime + timedelta(days=2) %}

You might need to put | as_datetime | as_local instead depending on the timezone of that sensor’s datetime.

That’s perfect, thank you :slight_smile: