Sensor in confiuration.yaml files stays in unknwon state

Code in configuration.yaml file below


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      name: "Main Geyser Energy Needed"
      unit_of_measurement: "kWh"
      state_class: "measurement"
      force_update: true
      state: >
        {% set cold_temp = states('sensor.main_geyser_temp_current') | float(default=0) %}
        {% set delta_t = (65 - cold_temp) | abs %} # Calculate temp difference
        # Formula: 1.16 Wh/L/°C * Volume * Delta T / 1000 (to get kWh)
        {% set energy_wh = 1.16 * 150 * delta_t %}
        {{ (energy_wh / 1000) | round(2) }}

Same code in template give a value as seen in pictures

So i would appreciate it someone can fix my code. I hand a similar template but unfortunately my rpi4 broke so i cant code anymore. Currently i am running a containter(docker) home assistant in a mini pc.

Thanks in advance


Due to me being new im only allowed to post one image per post. here is the sample code in the template tester to show that the code actually gives a value

That is not how you do comments in jinja templates. This is:

{# Calculate temp difference #}

The faulty comments are messing up your template.

Failing to recognise that is one failing of the template editor.

The other is that it always interprets the template result type. Only attribute templates do that. State templates do not.

Thanks. working now appreciated

Now i have another question, how do i prevent negative values and set them to a o value?

Sometime the geyser temp continues to rise and the zero value exceeds the target value of 65 degrees celsuis.

    {% set delta_t = (65 - cold_temp) | abs %}

aka if the cold_temp (geyser_current temp) exceeds 65 it will have a negative value but i want it to be 0


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
      name: "Main Geyser Energy Needed"
      unit_of_measurement: "kWh"
      state_class: "measurement"
      state: >
        {% set cold_temp = states('sensor.main_geyser_temp_current') | float(default=0) %}
        {% set delta_t = (65 - cold_temp) | abs %}
        {% set energy_wh = 1.16 * 150 * delta_t %}
        {{ (energy_wh / 1000) | round(2) }}

      name: "Backup Geyser Energy Needed"
      unit_of_measurement: "kWh"
      state_class: "measurement"
      state: >
        {% set cold_temp = states('sensor.backup_geyser_temp_current') | float(default=0) %}
        {% set delta_t = (65 - cold_temp) | abs %}
        {% set energy_wh = 1.16 * 150 * delta_t %}
        {{ (energy_wh / 1000) | round(2) }}
{{ [your_value_here,0]|max }}