"bridge" template sensor for unavailable entities

Hi,

I came across a little bit of issues with attempt of creating template sensor for samsung syncthru sensor.
The problem with samsung syncthru sensors is that when the printer if switched off, sensors are replaced with yellow containers saying that entity doesn’t exist. I therefore started to look into a way of having template sensor which will update itself when syncthru sensors are available and while thye’re not, it will display its own value. I’ve added a lot of 6 new template sensors to the recorder to make sure that the value will be available after HA restart but despite that fact, the template sensor value displays as empty string and only mirrors syncthru sensor when they become available. Below is my attempt to solve the problem:

  - platform: template
    sensors:
      samsung_c43x_series_19216809_tray_1_template:
        entity_id:
          - sensor.samsung_c43x_series_19216809_tray_1
          - sensor.samsung_c43x_series_19216809_tray_1_template
        friendly_name: 'Input tray'
        value_template: >-
          {%- if states.sensor.samsung_c43x_series_19216809_tray_1 is defined and states.sensor.samsung_c43x_series_19216809_tray_1.state != '' -%}
            {{ states.sensor.samsung_c43x_series_19216809_tray_1.state }}
          {%-  else -%}
            {{ states.sensor.samsung_c43x_series_19216809_tray_1_template.state }}
          {%- endif %}

Can you see what is causing a value from the template sensor not to be restored from DB?
Thanks in advance.

Your template_sensor refers to itself. Is a template_sensor allowed to be recursive?

I’ve converted your template_sensor into pseudo-code to make it easier to see how it refers to itself. I honestly don’t know how Home Assistant handles this situation.

PSEUDO-CODE

  - platform: template
    sensors:
      tray1_template:  <---
        entity_id:
          - tray1
          - tray1_template  <---
        value_template:
          if states.sensor.tray1 is valid
            tray1.state
          else
            tray1_template.state  <---
          endif

@123, a template sensor can refer to itself. I’ve seen that in several valid solutions.

The issue here is that a template sensor, although saved to the database, so that history can be seen, is not restored from the database at startup. For that you would need to use, e.g., an input_text.

My recommendation would be to use an automation to keep an input_text updated as to the latest state of sensor.samsung_c43x_series_19216809_tray_1, then have the template sensor reflect the status of the input_text. If there are states of sensor.samsung_c43x_series_19216809_tray_1 that you don’t want to see you could filter them out either in the automation or in the template sensor. Then, when HA restarts, the input_text will be restored, and the template sensor should show that restored state.

1 Like

thank you both for your engagement! @pnbruckner I have followed your recommendation and I am pleased to confirm that all template sensors fetching values from input_texts are behaving as planned :slight_smile:

Thanks again, greatly appreciated.