Problems with comments

I am having very strange effects with comments and have tried everything I could come up with. The code below works just fine in the template editor and it compiles also. But the sensor has the state “unavailable”. If I remove all comments, it works fine.

What am I missing about the comments?

- sensor:
    # Name of the sensor
    - name: "Pellet_Usage_Difference"
      
      # Unique identifier for the sensor
      unique_id: e7900b1b-e89b-4356-a050-a6928781042f
      
      # Icon representing the sensor
      icon: mdi:weight-kilogram
      
      # Unit of measurement for the sensor value
      unit_of_measurement: "kg"
      
      state: >-
        {# Get pellet count at midnight, default to 0 #}
        {% set gewicht_start = states('input_number.pelletsatmidnight') | float(0) %}
        
        {# Get current pellet usage, default to 0 #}
        {% set gewicht_end = states('sensor.solarfocus_biomass_boiler_pellet_usage_total') | float(0) %}
        
        {# Check if the current usage is zero or the sensor is unavailable #}
        {% if gewicht_end == 0 or 
              is_state('sensor.solarfocus_biomass_boiler_pellet_usage_total', 'unavailable') or
              is_state('sensor.solarfocus_biomass_boiler_pellet_usage_total', 'unknown') %}
          {# Return 0 if any conditions are met #}
          0  
        {% else %}
          {# Calculate the difference and ensure it's at least 0 #}
          {{ max(0, (gewicht_end - gewicht_start) | round(2)) }}
        {% endif %}
        
      availability: >
        # Check if the sensor is available (not unknown or unavailable)
        {{ states('sensor.solarfocus_biomass_boiler_pellet_usage_total') not in ['unknown', 'unavailable'] }}

That one should be a jinja comment {# #} too as it is in the template.

2 Likes

Thank you. I spent a lot of time until I gave up… Seems to work now!!!