Optimizing Energy Efficiency: Calculating Accumulator Energy in Home Assistant

I need help because my my template not work!
The parameters thay i use is:

  1. Sensors:
  • Current Water Temperature (External Sensor):
    • sensor.contador_agua_temperatura_promedio_dia_anterior
    • This sensor provides the current water temperature in the accumulator.
  • Accumulator Temperature (Internal Sensor):
    • sensor.temperatura_acumulador_agua
    • This sensor indicates the temperature that will increase in the accumulator.
  1. Accumulator Parameters:
  • Accumulator Capacity:
    • Capacity of the water accumulator in liters (e.g., 200 liters).
  • Thermal Efficiency of the Accumulator:
    • This value represents the thermal efficiency of the accumulator. It can be a decimal number (e.g., 0.9 for 90%).
  • Water Density:
    • Density of water in kg/liter.
  • Water Heat Capacity:
    • Heat capacity of water in J/(kg°C).

And code:

sensor:
  - platform: template
    sensors:
      acumulador_energy:
        friendly_name: "Energía Acumulada en Acumulador"
        unit_of_measurement: "kWh"
        value_template: >
          {% set water_mass_liters = 200 %}
          {% set specific_heat_water = 4.186 %}  # Calor específico del agua en KJ/(kg°C)
          {% set initial_temperature = states('sensor.contador_agua_temperatura_promedio_dia_anterior') | float %}
          {% set final_temperature = states('sensor.temperatura_acumulador_agua') | float %}

          {{ (water_mass_liters * specific_heat_water * (final_temperature - initial_temperature) / 3600) | round(2) }}

Thaaanks!

Your template works if i replace the sensors with numbers.


There must be something with your sensors.

Thanks for your reply!

Now I just replaced the sensors with numbers, and the error persists

sensor:
  - platform: template
    sensors:
      acumulador_energy:
        friendly_name: "Energía Acumulada en Acumulador"
        unit_of_measurement: "kWh"
        value_template: >
          {% set masa_agua_litros = 200 %}
          {% set densidad_agua = 1 %}  # Suponiendo que la densidad del agua es 1 kg/litro
          {% set calor_especifico_agua = 4.186 %}  # Calor específico del agua en KJ/(kg°C)
          {% set temperatura_inicial = 20 %}
          {% set temperatura_final = 50 %}

          {{ ((masa_agua_litros * densidad_agua * calor_especifico_agua * (temperatura_final - temperatura_inicial)) / 3600) | round(2) }}

You were right, @VDRainer. I had the wrong sensor name. I’ve corrected it and tested it in the template editor, and it works. However, in the “Helpers” section, I’m getting an error because it says the result is a string. I’ve tried several options, but I still have this error

sensor:
  - platform: template
    sensors:
      acumulador_energy:
        friendly_name: "Energía Acumulada en Acumulador"
        unit_of_measurement: "kWh"
        value_template: >
          {% set masa_agua_litros = 200 %}
          {% set densidad_agua = 1 %}
          {% set calor_especifico_agua = 4.186 %}
          
          {% set temperatura_inicial_raw = states('sensor.contador_agua_temperatura_promedio_dia_anterior') %}
          {% set temperatura_final_raw = states('sensor.esphome_web_7baa2c_acumulador') %}
          
          {% set temperatura_inicial = temperatura_inicial_raw | float(0) %}
          {% set temperatura_final = temperatura_final_raw | float(0) %}

          {% set energia_acumulada = (masa_agua_litros * densidad_agua * calor_especifico_agua * (temperatura_final - temperatura_inicial) / 3600) | round(2) %}
          
          {{ energia_acumulada | float if not temperatura_inicial_raw == 'unknown' and not temperatura_final_raw == 'unknown' else 0 }}