Creating entities for multiple devices

I am going through my first steps in Home Assistant, please don’t kill me if this is trivial and has been asked a thousand times before - I probably didn’t know how to phrase my question.

I wrote a simple sensor definition to make up for the unusable current readings on my NOUS plugs:

sensor:
  - platform: template
    sensors:
      dose_02_ladegerate_wz_current_calculated:
        friendly_name: "D02WZ Ladegeräte Berechneter Strom"
        unit_of_measurement: "A"

        value_template: '{{ (states("sensor.dose_02_ladegerate_wz_power") |float  /  ( (0.001 |float + states("sensor.dose_02_ladegerate_wz_voltage") |float) ) ) | round(3) }}'
        icon_template: "{{ 'mdi:current-ac' }}"

I do own 12 of these plugs - will I have to copy/paste this snippet 12 times with updated entity-names? Or is there a way to add this to the device definition so it gets created for every single device automatically i.e.

class myNOUSplug inherits from NOUSplug

or something like that?

Yes.

No.

If you’re creating Template Sensors in YAML, use the “modern” configuration format. Your example is using a format that was deprecated over a year ago.


NOTE

There is a concept known as YAML Anchors and Aliases that can minimize repetitive code.

It comes with its own overhead so it may not be worth using unless your 12 sensors share several identical lines. For example, if they only share one or two identical lines then Anchors and Aliases aren’t worth the effort.

I went where Google pointed me and shed the required drops of sweat.
Thanks for pointing me to the modern format - looks way more sensible!

For future reference, if a search engine directs you to an example that’s more than a year old, look for a more recent example. Or refer to the examples in the official documentation. Home Assistant evolves rapidly so examples older than a year may be outdated.

2 Likes