Disclaimer: I’m not looking for a solution, I have one. I just find it cumbersome
Probably due to my dev background, I like to decouple functionality and infrastructure aspects of my devices.
E.g., if I have a random temperature/humidity sensor in my living room, I keep the “infrastructure” naming of the device as-is, then create “sensor.living_room_temperature” / "sensor.living_room_humidity template entities above them, which is constant across my configuration. If the device dies, or I want to use something else, I just change the source of the template sensor and done.
Currently, that involves quite a bit of “boilerplate” code, so I use an external Go template generator which does most of the work, e.g.
## gomplate template
## install:
## go install github.com/hairyhenderson/gomplate/v4/cmd/gomplate@latest
## docker:
## docker run -v .:/config hairyhenderson/gomplate:stable -d 'data=file:///config/gomplate/templates_template_occupancy.json' -f /config/gomplate/templates_template_occupancy.tmpl -o /config/platforms/templates/gmp_templates_template_occupancy.yaml
## run:
## cat gomplate/templates_template_occupancy.json | gomplate -d 'data=stdin:?type=application/array%2Bjson' -f gomplate/templates_template_occupancy.tmpl -o platforms/templates/gmp_templates_template_occupancy.yaml
- sensor:
{{- range $c := (ds "data") }}
{{ $slug_name := $c.name | strings.Slug | strings.ReplaceAll "-" "_" }}
- name: "{{ $c.name }}_temperature"
unique_id: "{{ $c.uid }}_temperature"
state: '{{ "{{" }} (states("{{ $c.entity_id }}_temperature")|round(1)) {{ "}}" }}'
unit_of_measurement: "°C"
device_class: temperature
state_class: measurement
availability: '{{ "{{" }} states("{{ $c.entity_id }}_temperature")|is_number {{ "}}" }}'
- name: "{{ $c.name }}_humidity"
unique_id: "{{ $c.uid }}_humidity"
state: '{{ "{{" }} (states("{{ $c.entity_id }}_humidity")|round(1)) {{ "}}" }}'
unit_of_measurement: "%"
device_class: humidity
state_class: measurement
availability: '{{ "{{" }} states("{{ $c.entity_id }}_humidity")|is_number {{ "}}" }}'
{{- end }}
It would be more elegant to be able to derive a template entity directly from the parent, infrastructural one, e.g.
template:
- sensor:
- name: "living_room_temperature"
unique_id: "living_room_temperature"
base_entity_id: "sensor.temperature_humidity_sensor_0cb4_temperature"
which would take all characteristics of the base entity, with the capability to override specific aspects if needed, rather than having to recode a complete template definition.
Surely and advanced need/request, but hey…