Save last state of a device when it goes unavailable

Hello, I just got some door contat sensors working with tuya and configured them with tuya local. They works perfectly fine except when they go in deep sleep.
The sensor after sending the status (on/off) goes in deep sleep and becomes unavailable but I need to remember the last state they sent. Since I’m new with home assistant I found template sensors. And got a functional code where it returns the status of the sensor but the issue is the same, when the door sensor goes in deep sleep also the template sensor return the “off” state even if the door sensor went in deep sleep while it was on the “on” state.

The code below is what I used in the dev tools to check if it works.

{%set temp%}
{%endset%}
{%if (states('binary_sensor.finestracamera') == 'on' or states('binary_sensor.finestracamera') == 'off') and states('binary_sensor.finestracamera') != 'unavailable' %}
  {%set temp = states('binary_sensor.finestracamera')%}   
{%endif%}

stato effettivo: {{states('binary_sensor.finestracamera')}}
Stato: {{temp}}

At this point I can assume that the temp variable get reset every time the status changes, included when it goes unavailable so I need to save the status somewhere it doesn’t get reset everytime. I read something about helpers but if I could avoid to create an helper for every variable it would be nice (since i have 10 door sensors which every one as both contact and battery level sensors)

A trigger sensor should do the job fine if the sensor is not updated very often.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.finestracamera
      - platform: event
        event_type: event_template_reloaded
    binary_sensor:
      - name: finestra stato effettivo
        device_class: door
        state: "{{ is_state('binary_sensor.finestracamera', 'on') }}"

On the other hand, if the door sensor is often updated, a simple sensor is enough for the job

binary_sensor:
  - platform: template
    sensors:
      finestra_stato_effettivo:
        device_class: door
        value_template: : "{{ is_state('binary_sensor.finestracamera', 'on') }}"

This will return OFF when the sensor goes unavailable (not ON). Or have I missed something?

I would try this:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.finestracamera
        to:
          - "on"
          - "off"
    binary_sensor:
      - name: finestra stato effettivo
        device_class: door
        state: "{{ states('binary_sensor.finestracamera') }}"

Indeed, you’re right (I’m talking like ChatGPT).
Using is_state will be ‘on’ when on and ‘off’ otherwise.

Thank you! That works perfectly, I didn’t quite understand how triggers worked before.
I applied that also with the battery percentage and works too

Great to hear that it is working…

Basically the triggers will define when the conditions will be checked. If nothing is triggered, the conditions (if any) won’t be checked.
When a condition is checked (after a trigger), then it will run the actions.

The tricky part about using the trigger variable as you have tried initially is the fact that it will be different based on how your automation was triggered and won’t exists if you run the automation manually in the UI. But it is a power tool that I use a lot on many of my automations.