Missing something re: templates in YAML

Hello! I just installed a Refoss home energy monitor and integrated it into my HA install. It was painless.

I was not happy with a lot of the default entity names that my Refoss created, and further, I wanted to combine some for split phase (e.g., my 240v items - I’m in north america). So, I thought I would just go and write some YAML.

I got the YAML to work (grr - reload YAML in developer tools does not reload). But I’m hitting a few roadblocks and I figure I may be going about this the wrong way.

I posted a sample of my YAML below. I see that for items I made I cannot edit them in the GUI (strange I cannot give myself unique IDs, but okay). I also cannot seem to assign an “area” to them - my HA dashboard is auto-created for me and I want them in their proper places.

I did some searching but mostly found bad tutorials. ChatGPT kept spewing garbage. Does anyone have a resource they could point me to to help me understand how I can integrate my custom templates better? Thanks!

–whoops forgot to paste my YAML


  - name: "ev_charger"
    unit_of_measurement: "W"
    state: >
      {{ (states('sensor.em16_a2_power')|float + states('sensor.em16_b2_power')|float) | round(1) }}
    attributes:
      amps: "{{ states('sensor.em16_a2_current') }}, {{ states('sensor.em16_b2_current') }}"
      voltage: "{{ states('sensor.em16_a2_voltage') }}, {{ states('sensor.em16_b2_voltage') }}"
      pf: "{{ states('sensor.em16_a2_power_factor') }}, {{ states('sensor.em16_b2_power_factor') }}"
      energy: "{{ states('sensor.em16_a2_this_month_energy') }}, {{ states('sensor.em16_b2_this_month_energy') }}"

If you set a unit of measurement, the state needs to return a numeric value.

You need to add a default for the state and/or add an availability template.

Thanks. For my beginner YAML I think the state is numeric.

I’ll google how to add a default, and what is meant by “availability template” thanks

I’m not sure what you mean there. I checked and you can add unique id’s in yaml. I don’t use them in my setup but I just added one to a template sensor and worked as expected.

hmm. I didn’t even try.

From This entity does not have a unique ID? - Home Assistant

it says:

Can I add a unique ID myself?

No, as an end-user, you cannot add a unique ID to an entity that doesn’t have one. Unique IDs are a feature that must be provided by the integration itself.

The template integration can provide a unique id. It is up to you to specify it though.

- name: "EV Charger" # will still make sensor.ev_charger but will look nicer in your cards
    unique_id: e9971788-f98f-43a2-bcfe-721f60f5c71d
    unit_of_measurement: "W"
    state: >
      {{ (states('sensor.em16_a2_power')|float + states('sensor.em16_b2_power')) | round(1) }}
    availability: >
      {{ has_value('sensor.em16_a2_power') and has_value('sensor.em16_b2_power') }}
    attributes:
      amps: "{{ states('sensor.em16_a2_current') }}, {{ states('sensor.em16_b2_current') }}"
      voltage: "{{ states('sensor.em16_a2_voltage') }}, {{ states('sensor.em16_b2_voltage') }}"
      pf: "{{ states('sensor.em16_a2_power_factor') }}, {{ states('sensor.em16_b2_power_factor') }}"
      energy: "{{ states('sensor.em16_a2_this_month_energy') }}, {{ states('sensor.em16_b2_this_month_energy') }}"

You can use any unique value. VSCode has a UUID generator, or you can use this, https://www.uuidgenerator.net/ or you can use the entity_id as that has to be unique too.

Also note what I did with the name.

Thanks a lot. I will start playing with this.

I went down a rabbit hole on another aspect of this project and soon back at this.

I also looked up how to add defaults as well, which pleasantly can be added as filter.

This works great - it now lets me use the built in GUI to do things like set my area, etc.

I now am trying to get it to work with my energy panel.

According to: Frequently Asked Questions about home energy management - Home Assistant
I need to have my device class and state class set. I understand that in YAML in HA I need to use strings, so I tried this.

I still can’t get my template sensor to show up under the list of eligible sensors for “individual devices” or “grid consumption”.

  - name: "EV Charger"
    unique_id: e9971788-f98f-43a2-bcfe-721f60f5c71d
    unit_of_measurement: "W"
    state_class: "measurement"
    device_class: "power"
    availability: >
      {{ has_value('sensor.em16_a2_power') and has_value('sensor.em16_b2_power') }}
    state: >
      {{ (states('sensor.em16_a2_power')|float + states('sensor.em16_b2_power')|float) | round(1) | default(0) }}
    attributes:
      amps: "{{ states('sensor.em16_a2_current') }}, {{ states('sensor.em16_b2_current') }}"
      voltage: "{{ states('sensor.em16_a2_voltage') }}, {{ states('sensor.em16_b2_voltage') }}"
      pf: "{{ states('sensor.em16_a2_power_factor') }}, {{ states('sensor.em16_b2_power_factor') }}"
      energy: "{{ states('sensor.em16_a2_this_month_energy') }}, {{ states('sensor.em16_b2_this_month_energy') }}"

I checked in developer tools and the state looks good, with the following attributes:

state_class: measurement
amps:
  - 0
  - 0
voltage:
  - 124.128
  - 123.871
pf:
  - 0
  - 0
energy:
  - 0
  - 0
unit_of_measurement: W
device_class: power
friendly_name: EV Charger

Still reading on how to get things to show up in the Energy panel.

Oh! I just realized the energy panel needs energy items, not power items. I see. I’ll work on that tomorrow. Thanks!

1 Like

I wasn’t sure if the etiquette here was to edit prev. post or to keep posting. Apologies for all the followups.

So now I have two questions

  • when editing configuration.yaml I am happily including another file where I keep these templates created. However, I would also like the integral sensors I am making (to create energy from power) to be in the same file. My understanding is that I cannot do this- I cannot simply include another configuration file, only replace a section such as sensor: or template:. Is there somehting I’m missing?
  • Why do I need to make my new sensors, which are derivatives of my underlying sensors, as “templates”? This makes me feel like I am creating something that can be re-used. I tried just making them as sensors but it didn’t like it. Any pointers to a page that explains what I am missing?

Thanks again!

I don’t understand this question.

Thanks! I’ll try out the packages, much appreciated.

The question that I was not clear with is why do I need to use:

template:
  - sensor:
    - name:

for my combination of two 120v rails into my 240v. Intuitively I would have assumed I would just use the top level “sensor:”

sensor:
  - ...

I am not sure what “template” refers to vs. the sensor in this context.

template is an integration. It supports sensors, lights, covers and more.

Confusingly light and sensor are also integrations that support platforms like Lifx, Hue, mqtt, etc… In the old days template was just another platform under other integrations like light and cover, etc…

Making it an actual integration has been a recent change. Being it’s own integration has some advantages, like the way it is organised in the frontend and probably development advantages too (guessing).

Beyond what Tom has explained about the integration, the term “template” in HA is usually just referring to something that uses of the Jinja2 templating language.

This is a fairly common misunderstanding. It is possible to use Jinja templating in HA to create reusable macros and constants.

These replies are helpful. I guess I should think of template: as jinja2: to access its features.

Also the language of calling those “sections” integrations helps me understand that they’re accessing plugins or modules or some such, so it makes more sense.