Templates in sensors

Hello
newb here.
Hoping someone can enlighten me please. I recently discovered that I can use variables with triggers and sensors (https://community.home-assistant.io/t/why-cant-we-use-variables-in-a-template-sensor/467170). I’ve gotten the variables to work, thanks.
I’m seeing though, that the code in {{}} is not parsed in the same way in all the sensor yaml parts. and am wondering if someone could let me know of there is a clever way to get this to work please?

- trigger:
    - platform: state
      entity_id: sensor.environ001_temperature
      variables:
        adjustment: -0.026004273
  sensor:
    # These work
    - name: "{{ state_attr(trigger.entity_id, 'friendly_name') }} Cal"
      state: "{{ (((states(trigger.entity_id) | float(0))) + adjustment ) | round(2) }}"
      availability: "{{ states(trigger.entity_id) | is_number }}"
      # These dont work
      unique_id: "{{ trigger.entity_id.split('.')[-1] }}_cal"
      unit_of_measurement: "{{ state_attr(trigger.entity_id, 'unit_of_measurement') }}"
      device_class: "{{ state_attr(trigger.entity_id, 'device_class') }}"

I want to pass calibrations to a bunch of environment sensors and currently i’m using a very verbose approach with tons of repetition and places for me to mess up, so being able to reference the parent entity means I only need to specify the initial entity and the adjustment, which would be very nice.

Thanks in advance

According to the documentation, those three options don’t support templates.

Ahh thanks. I misunderstood that. Documentation says it wants a string but we can’t pass something that renders a string? Has to be a straight up string.

With you. Thanks very much

1 Like