Can someone help my understand this new way of doing things?

Hello all!

Extremely new to writing my own stuff in HA… I have been trying to learn how the heck these templates work, but I admit i’m at a loss. There are two ways to make them it seems, a new way, and a old way, and I cant even figure out which way is the new way…

What I have now are two methods I have figured:
Method 1-
in configuration.yaml add a line

template: !include template.yaml

in template.yaml I added

template:
  - sensors:
    energy_total:
      friendly_name: 'Total Fish Room Energy'
      unique_id: total_fish_room_energy_12432tser23rfew
      device_class: "energy"
      state_class: total
      entity_id:
        - sensor.4_0_fish_tank_main_pwr_bus_1_this_month_s_consumption
        - sensor.5_0_fish_tank_main_pwr_bus_2_this_month_s_consumption
      value_template: "{{ (states('sensor.4_0_fish_tank_main_pwr_bus_1_this_month_s_consumption')|float + states('sensor.5_0_fish_tank_main_pwr_bus_2_this_month_s_consumption')|float)|round(2) }}"
      unit_of_measurement: "kWh"

This option gives me the problem that I need to make the device_class energy so I can inport the created entity into the HA energy panel.

Method 2-
add the following to configuration.yaml

template:
  - sensor:
    friendly_name: 'Total Fish Room Energy'
    unique_id: total_fish_room_energy_12432tser23rfew
    device_class: "energy"
    state_class: total
    entity_id:
      - sensor.4_0_fish_tank_main_pwr_bus_1_this_month_s_consumption
      - sensor.5_0_fish_tank_main_pwr_bus_2_this_month_s_consumption
    value_template: "{{ (states('sensor.4_0_fish_tank_main_pwr_bus_1_this_month_s_consumption')|float + states('sensor.5_0_fish_tank_main_pwr_bus_2_this_month_s_consumption')|float)|round(2) }}"
    unit_of_measurement: "kWh"

This issue seems to be formatted all wrong, but I honestly cant figure out why…

Which method is the one I should be using these days?

Any insights would be really greatly appreciated and thanks so much for taking the time to help educate me!

Neither of those is the “new” format… which is more than 4 years old at this point.

The one in the docs.

Or you can avoid the YAML and set it up either as a “Template Helper” or a “Combine the state of several sensors Helper” in the Helpers Settings.

What docs did you consult, or did you maybe use ChatGPT? There’s a lot wrong here.

The first problem is how your configuration is broken up. Start simple. Just put everything in your configuration.yaml for starters. You have the template: key twice. It should only be defined once. YAML and Jinja (this is the templating part) are two different things. Your problems seem much less about templates, but more about YAML. Google for Thomas Loven’s YAML guide.

Secondly, your indentation of energy_total is off, and it should be a list item (which is denoted by a -).

Thirdly, what is the entity_id: key doing there? Either you want to define a trigger-based template sensor, in which case you’re missing a trigger: key (and a platform: state, which is the only one that will use entity_id:), or just omit it, as the template sensor will listen to all entities referenced. Going with that, value_template: is invalid and should be state:, if you’re not using the legacy template sensor format.

Lastly, carefully read about the usage of state_class. If your source sensors reset at some point, you should be using total_increasing.

And when you actually have a template issue, test it in the template editor first. That part seems fine though.

So, in summary, there’s a lot going on here and it seems like you dove in big bang. Start small. Start simple.

I’ve taken a bit of a different approach @Dimitry as mention by @Didgeridrew

Created a Helper (Settings → Devices & Services → Helpers → Create Helper → Combine the state of several Sensors) to calculate the the sum of power utilization in Watts.

Secondly I created a “Integral Sensor” helper to calculate the consumption to reflect in the power Dash board

image

This does also require a little bit of YAML to supress the Energy Sensor Error message, which is purely cosmetic BTW.

sensor.power_consumption_lower_level:
  device_class: energy

This code code can be directly in the configuration.yaml or a reference customization in my case I have it in the custom file

homeassistant:
  customize: !include customize.yaml