Template entities should support "this" or something similar

Example Configuration:

template:
  - binary_sensor:
      - name: a_test
        unique_id: bs_a_test
        state: >-
          {{
            (
              is_state('binary_sensor.a_test', 'on')  #REGARDING THIS!
              or is_state('input_boolean.test_1', 'on')
            )
            and
            (
              is_state('input_boolean.test_2', 'on')
            )
          }}

The documentation suggests the above configuration for a template entity that needs to reference itself. However, it is problematic because the entity_id of the binary_sensor can be changed in the UI. It’s also possible that the obvious entity_id (‘binary_sensor.a_test’) is already used and therefore this binary_sensor gets a different entity_id.

I would like to see something similar to the this keyword available in automation triggers to refer to the state of the template entity itself. With this in place, something like the following would work…

template:
  - binary_sensor:
      - name: a_test
        unique_id: bs_a_test
        state: >-
          {{
            (
              is_state(this, 'on')  #MUCH BETTER!
              or is_state('input_boolean.test_1', 'on')
            )
            and
            (
              is_state('input_boolean.test_2', 'on')
            )
          }}

Voted; this would be handy (and so would, shameless plug for my own FR, variables in Template Sensors).

If implemented, this would be an object (like it is for automations) so the example you posted

is_state(this, 'on')

would probably look more like

is_state(this.entity_id, 'on')

based on the properties of a State Object.

1 Like