Self Reference in Sensor Template Always Returns Unknown

Tags: #<Tag:0x00007f32658c13f8> #<Tag:0x00007f32658c12e0>

Hi all, I’m new to the forums so thank you in advance for your time - seems like a fantastic community.

I am working with the AlarmDecoder integration and specifically sensor.alarm_panel_display entity.

I want to adjust the output the integration feed provides from my alarm panel through the integration (which works). Basically, I want to parse and adjust the values returned. Note I am running 0.117.0 of the HA core on a Pi 4.

I’ve studied templates and have have used the developer template editor tool and also created the following entry in my configuration,yaml file:

sensor:
  - platform: template
    sensors:
      alarm_panel_display:
        friendly_name: 'Alarm Panel Display'
        value_template: >-
          {% if 1 %}
            {{ states('sensor.alarm_panel_display') }}
          {% else %}
            Test
          {% endif %}

The problem for me is when this template is activated in HA, it always returns unknown. Interestingly, when the template is removed from the configuraiton.yaml file, it works in the test template editor.

I’m thinking there is a self-reference or other instantiation issue where the sensor’s value isn’t available to the template. I’ve seen a few other articles about self-reference issues, but nothing in the information that suggests what I am trying to do is legal or not.

Can anyone thumbs up or down for me if a sensor self-reference as a template is doable or not?

Much appreciated.

-David

Ok. I think I solved my own problem. This can probably be filed under dumb-newbie error #999.

Apparently, one has to create a new sensor name in order to reference the integration sensor as I’ve defined it. Otherwise, there is a circular reference which doesn’t work (I can’t be the only guy that ever made this mistake).

In the above template code, all I did was create a new sensor name like this:

sensor:
  - platform: template
    sensors:
      alarm_panel_display1:

And then reference alarm_panel_display1 in my code and it works.

Hopefully, I’m not the only one who has made this rookie mistake and I hope this helps someone else.