Climate object attributes not available yet during Python startup script

I need to retrieve the thermostat current temperature whenever HA starts. So I wrote a homeassistant “start” trigger which runs a simply python script

  • id: ‘0001’
    alias: 0001_home_assistant_startup
    description: ‘’
    trigger:
    • platform: homeassistant
      event: start
      condition:
      action:
    • service: python_script.startup
      mode: single

The problem is that when the Python script reads the current temperature from the climate:

thermostat_obj = hass.states.get(‘thermostat_id’)
thermostat_attribs = thermostat_obj.attributes.copy()
logger.info(thermostat_attribs[‘current_temperature’])

, it returns Error executing script: ‘current_temperature’. In other words, the “current_temperature” attribute of climate object is still not available during HA startup.

This is confirmed by the fact that if I run

logger.info(f"Attribs in startup.py: {thermostat_attribs}")

in the same Python startup script, it returns some attributes, but NOT current_temperature:

Attribs in startup.py: {‘restored’: True, ‘hvac_modes’: [‘heat’], ‘min_temp’: 5.0, ‘max_temp’: 35.0, ‘preset_modes’: [‘none’, ‘away’, ‘schedule’, ‘comfort’, ‘eco’, ‘boost’, ‘complex’], ‘supported_features’: 17, ‘friendly_name’: ‘corridoio_valvola_thermostat’}

If I manually run the same script a few seconds later, it works perfectly with all thermostat attributes:

Attribs in startup.py: {‘hvac_modes’: (‘heat’,), ‘min_temp’: 5.0, ‘max_temp’: 35.0, ‘preset_modes’: [‘none’, ‘away’, ‘schedule’, ‘comfort’, ‘eco’, ‘boost’, ‘complex’], ‘current_temperature’: 25.0, ‘temperature’: 22.5, ‘hvac_action’: ‘idle’, ‘preset_mode’: ‘none’, ‘system_mode’: ‘[4]/heat’, ‘occupancy’: <Occupancy.Occupied: 1>, ‘occupied_heating_setpoint’: 2250, ‘unoccupied_cooling_setpoint’: 1500, ‘friendly_name’: ‘corridoio_valvola_thermostat’, ‘supported_features’: 17}

So it clearly depends on the fact that this object is not ready at startup time, so I’m wondering if I may add a try/except section in Python with the sleep function… which would require the time library (import time) which as we know it not supported in HA.

Please use the code blocks </> button, or three backticks ``` to format your code, not the quote block.

See point 11 here.

Why not add a delay in the automation that runs the script at startup?