Template renders unexpectedly

Hi,

I want to present the electrical cost for today, this month and this year using data from the Energy statistics. However, my templates render unexpectedly and I cannot really find out why.

I fetch the consumption data for the day:

  - platform: sql
    db_url: !secret recorder_db_entry
    queries:
      - name: electric_energy_startofday
        query: "SELECT state FROM statistics WHERE metadata_id=103 AND start>=CURDATE() AND state <> 'unknown' ORDER BY id ASC LIMIT 1;"
        column: 'state'

The state of electric_energy_startofday is as expected:

72118.6899414062

The state of the current energy reading (sensor.electric_mains_energy_consumption) is also as expected:

72124.5500488281

I have made an template to calculate the difference

- platform: template
  sensors:
    electric_meter_consumption_today:
      value_template: >
        {{ (states("sensor.electric_mains_energy_consumption")|float) - (states("sensor.electric_energy_startofday")|float) }}

I expect the electric_meter_consumption_today to contain the difference between the two values. However, it contains:

#5.8779296875873115 5.8779296875873115

What am I missing…? Thankful for support!

but

72124.5500488281 - 72118.6899414062 is 5.860107421889552 which is close to your value. The calc seems correct. What’s the problem?

Hi,

The calculation seems OK. But I get a hashtag in front of the value and thus HA doesn’t recognise it as a number

Well, I’m guessing what you posted is not what’s actually in your configuration. The system isn’t going to add special characters randomly, they are coming from what you put in there. Is there a commented out line in your actual configuration?

Thank you for your support! You are of course correct.
I had commented out a section. Or I thought so. That explains both the hashtag and the double values.

Sometimes the solution is too obvious to notice. :slight_smile:

Yep, you can’t comment out areas of templates when you use a multiline template because it sees the text as part of the tempalte.