snarky-snark / home-assistant-variables

A custom Home Assistant component for declaring and setting generic variable entities dynamically.
Apache License 2.0
278 stars 16 forks source link

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

Open jb1228 opened 1 year ago

jb1228 commented 1 year ago

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.