Create a sensor that calculate kWh a day

Hello, i am new to HomeAssistant, and not a native English speaker, so upfront sorry for the stupid question and the language errors!

I try to create a sensor that shows the total kWh from a day.
in the configuration.yaml file I create a rule “sensor: !include sensors.yaml”

In the same folder is create a file “sensors.yaml”

In the sensors.yaml file I add the following config: ( I got this from the documentation [Template - Home Assistant]

template:
  - sensor:
    - name: "Totaal kWh vandaag"
      unit_of_measurement: "kWh"
      state: >
        {% set import1 = states('sensor.total_power_import_t1') | float %}
        {% set import2 = states('sensor.total_power_import_t2') | float %}
        {% set export1 = states('sensor.total_power_export_t1') | float %}
        {% set export2 = states('sensor.total_power_export_t2') | float %}
        {{ ((import1 + import2) - (export1 + export2))| round(1, default=0) }}

The total_power sensors are created with the utility meters and giving the right data

When I put it in the template (graphical interface) it give me the right number.

When I put it in the config file it give the error: “Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None.”

I also tried to ad the config direct into the configuration.yaml file → same result.

I am bumping my had to this, can someone put me in the right direction?

template: does not go in your sensors.yaml file. It goes in your configuration.yaml file. It is an integration in its own right.

1 Like

thanks Tom! sometimes it is so easy… but you don’t see it.
Thanks again, and have a great day!

1 Like