home-assistant-variables icon indicating copy to clipboard operation
home-assistant-variables copied to clipboard

Request: Ability to use 'this' for Self-Referencing

Open jb1228 opened this issue 1 year ago • 0 comments

This feature is available in the native sensor/binary_sensor templates and is GREAT for creating multiple template definitions that are basically the same... especially when you have several attributes and/or complex templates.

Example from Home Assistant Template: Self-Referencing

template:
  - sensor:
      - name: test
        state: "{{ this.attributes.test | default('Value when missing') }}"
        # not: "{{ state_attr('sensor.test', 'test') }}"
        attributes:
          test: "{{ now() }}"

For anyone else reading this, one thing to remember when using this is to always set a default value ({{ this.state | default('whatever') }}) so there are no errors when HA first initializes the entity.

By the way, EXCELLENT job in creating this integration. It will be very useful for me and also save me from having to create tons of helpers for each value I need to reference. Maybe someday HA will have an 'input_dictionary' helper or something; but until then, this is perfect. Plus it allows for templating, which the other variable integrations do not.

jb1228 avatar Mar 25 '23 21:03 jb1228