Template Sensor for Energy Cost Today

Hello, I’m trying to create a template sensor in my configuration file that calculates today’s energy usage in kW x my energy cost of (.16) to display the cost of energy so far today in my dashboard. The following code returns a error message. Ideas?

  - sensor:
      - name: "home Energy Cost Today"
        unique_id: "energy_cost_today"
        state: '{{states('sensor.dogwood_123_1d') | float * 0.16) | round(2) }}'

Message: can not read an implicit mapping pair; a colon is missed. unexpected end of stream within a single quoted scalar.

The quotes used inside the template should not be the same as the ones used outside the template. Replace the outer single-quotes with double-quotes.

        state: "{{ (states('sensor.dogwood_123_1d') | float * 0.16) | round(2) }}"

In addition, add an opening parenthesis ( before the states function.

Now that the coding seems correct, I don’t see the newly created sensor/entity in my home assistant setting after a restart. Ideas?

When I dropped the parathesis after .16, the entity showed up in HA.
But, now it’s not rounding :slight_smile:

Taras already gave you the hint and right (). You should have a) for every (

1 Like

Thanks so much. Using:

    state: "{{states('sensor.dogwood_123_1d') | float * 0.16 | round (2) }}"

I get: state: “4.0403199999999995”

Why isn’t it rounding?

It’s not rounding the way you expect because the round() function is being applied only to the 0.16 value. In other words, it rounds 0.16 to two decimal places (effectively changing nothing) then multiplies it with the sensor’s value.

That’s why the two terms of the multiplication must be grouped with parentheses to ensure that the result of the multiplication is rounded.

1 Like

Perfect Thanks! Tell’s you how I did in algebra :slight_smile: