Set icon and unit of measurement in a template using a blueprint

Hi all,

I have a bunch of hygrometers I would like them all to extend them with template sensors (e.g. calculating the absolute humidity by taking in the relative humidity and temperature). I got this working so far with template sensors, but it’s a bit ugly I have to add such template sensor including all the code for calculation for each hygrometer.

But I noticed Blueprints and tried them. Documentation for Blueprints in combination with Templates is pretty basic (if at all) but got a Blueprint running for just adding an offset to a sensor value.

Problem now is that the unit of measurement from the used sensor is not used for the output of the blueprint. I tried to add the unit_of_measurement in the sensor section of the blueprint, but this does not work. Same applies to the icon.
I also tried to define it in the template, but this causes an error. See the following example:

Blueprint (value_correction.yaml):

blueprint:
  name: Corrects a given sensor value by given offset
  description: Creates a sensor which holds the corrected value of a reference sensor
  domain: template
  input:
    reference_entity:
      name: Sensor to be corrected
      description: The sensor which needs to have its value corrected
      selector:
        entity:
          domain: sensor
    offset:
      name: Numerical offset
      description: Numerical offset added to the sensors value
variables:
  reference_entity: !input reference_entity
  offset: !input offset
sensor:
  # this has no effect
  unit_of_measurement: "%"
  icon: mdi:water-percent
  state: >
    {% set h = states(reference_entity) | float %}
    {% set o = offset | float %}
    {{ h + o }}
  availability: "{{ states(reference_entity) not in ('unknown', 'unavailable') }}"

configuration.yaml

template:
  name: BLA
  unique_id: bla
  # here it causes an error
  # unit_of_measurement: "%"
  # icon: mdi:water-percent
  use_blueprint:
    path: homeassistant/value_correction.yaml # relative to config/blueprints/template/
    input:
      reference_entity: sensor.rel_humidity
      offset: -4.5

Has anybody an idea of what I’m doing wrong? I would like to keep the unit out of the blueprint to keep it as versatile as possible.

Template Blueprints are still very much a work in progress. They were only added in the 2024.11 release. Since unit_of_measurement does not accept templates, the user will have to provide it manually. But icon and name can both be templated to derive a value from the reference.

The following is working for me:

blueprint:
  name: Corrects a given sensor value by given offset
  description: Creates a sensor which holds the corrected value of a reference sensor
  domain: template
  input:
    reference_entity:
      name: Sensor to be corrected
      description: The sensor which needs to have its value corrected
      selector:
        entity:
          domain: sensor
    offset:
      name: Numerical offset
      description: Numerical offset added to the sensors value
      default: 0
    unit:
      name: Unit of Measurement
      description: The unit used by your source sensor
variables:
  reference_entity: !input reference_entity
  offset: !input offset
sensor:
  name: "Adjusted {{ state_attr(reference_entity, 'friendly_name')|default(reference_entity|slugify, 1) }}"
  unit_of_measurement: !input unit
  icon: "{{ state_attr(reference_entity, 'icon')}}"
  state: >
    {% set h = states(reference_entity) | float(0) %}
    {% set o = offset | float(0) %}
    {{ h + o }}
  availability: "{{ has_value(reference_entity) }}"

Example Sensor Configuration (make sure to adjust path to your folder’s location)

template:
  - unique_id: sensor_adjustment_bp_0001
    use_blueprint: 
      path: didgeridrew/adjusted_value.yaml
      input:
        reference_entity: sensor.living_room_humidity
        offset: -5.0
        unit: "%"