Change default value of binary sensor template on HA restart

is that possible to change default value of “binary_sensor.door_sensor_filtered” to “open” when HA restart? as I notice after HA restart, default value of door sensor always “closed”. below is my binary sensor template

template:
  - binary_sensor:
      name: "door sensor filtered"
      device_class: door      
      state: >-
        {% if is_state('binary_sensor.door_sensor_opening', 'unavailable') %}
        {{ states('binary_sensor.door_sensor_filtered') }}
        {% else %}
        {{ states('binary_sensor.door_sensor_opening') }}
        {% endif %}

Note:
“binary_sensor.door_sensor_opening” is the actual sensor, but this sensor always become unavailable when HA restart.

My 2cents, I run an automation on restart to set certain values, using a python script (HACS).
In your case it would only help if the sensor does get its sensor-value soon. The automation/script I use only sts it once so if the next interval still finds that sensor unavailable…it will make it so again

Other than script, do you know any other method? I not so familiar with script. That sensor only get his state once there is a door activity

I donot, the script is easy though…once installed via hacs,
example automation

alias: yourtitle
description: yourdescription(optional)
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: python_script.hass_entities
    data:
      action: set_state_attributes
      entity_id: sensor.whateversensor
      state: "on"
1 Like

It did look like simple automation :slightly_smiling_face:

I just tested, problem solved !
below is the solution

  1. add python_script: under configuration file,
  2. create config/python_scripts folder
  3. write below code(How to manually set state/value of sensor? - #9 by rodpayne) & save it under name “set_state.py” file inside “config/python_scripts”
#==================================================================================================
#  python_scripts/set_state.py 
#==================================================================================================

#--------------------------------------------------------------------------------------------------
# Set the state or other attributes for the entity specified in the Automation Action
#--------------------------------------------------------------------------------------------------

inputEntity = data.get('entity_id')
if inputEntity is None:
    logger.warning("===== entity_id is required if you want to set something.")
else:    
    inputStateObject = hass.states.get(inputEntity)
    inputState = inputStateObject.state
    inputAttributesObject = inputStateObject.attributes.copy()

    for item in data:
        newAttribute = data.get(item)
        logger.debug("===== item = {0}; value = {1}".format(item,newAttribute))
        if item == 'entity_id':
            continue            # already handled
        elif item == 'state':
            inputState = newAttribute
        else:
            inputAttributesObject[item] = newAttribute
        
    hass.states.set(inputEntity, inputState, inputAttributesObject)
  1. restart HA & add below automation
alias: ~~~ set door sensor state to OPEN when HA restart
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.door_sensor_filtered
      state: "on"
  1. you can try run the action & it will direct set the states of the sensor.

Noted:
I already delete the “binary_sensor.door_sensor_filtered” & direct change the state of the “binary_sensor.door_sensor_opening” which made more sense. There is no need the template anymore.

Hope this method will help other as there is no detail instruction how to made the python script work.

Below video is much more better solution which restore light status & can modify it to restore sensor state on HA restart !

https://www.youtube.com/watch?v=SxEHslRKqrw&t=365s