Set defaults for weather forecast sensor

I have several automations that use data from the weather_forecast sensor. The sensor is configured as below (including a forced update on HAss restart).

# Weather forecast - met.no
- sensor:
  - name: Daily weather forecast - met.no
    unique_id: weather_forecast_daily
    state: "{{ now().isoformat() }}"
    attributes:
      forecast: "{{ daily_forecast['weather.home'].forecast }}" 
  trigger:
  - platform: time_pattern
    hours: /1
  # Force sensor to be populated on restart
  - platform: homeassistant
    event: start
  # and on template entity reload - https://community.home-assistant.io/t/updating-a-trigger-based-template-on-startup/315677
  - platform: event
    event_type: event_template_reloaded
  action:
  - service: weather.get_forecasts
    data:
      type: daily
    target:
      entity_id: 
      - weather.home
      #- weather.home_2
    response_variable: daily_forecast

However, when HAss restarts, there seems to be a short interval in which the sensor values are not populated. Which means that all my automations/code that use the sensor crash.

The sensor data is accessed like this:

{{ state_attr('sensor.daily_weather_forecast', 'forecast')[0].condition }}

And, for example, the homeassistant-streamdeck-yaml code crashes on HAss restart, because for a brief moment there is no [0] in that list.

My questions are:

  1. Is there a way to avoid that brief moment in which the attribute is ‘None’?
  2. If not, what would be a proper way to set some sane defaults to be used during that brief interval?

I think there is an availability option for templates handling weather entities.

That is interesting - thank you!

However, state_attr still seems to return values for unavailable entities, so I assume it will still crash on that [0] index. My question is more on “how do I check if there is anything in the list?” :slight_smile:

Haven’t used either of these, but based on the docs the following events seem promising for your usecase:

  • Use the homeassistant_started event instead of the dedicated homeassistant_start trigger. This seems to imply that the event will only fire after the entire startup process is complete, rather than firing when it’s started.
  • Use the component_loaded event for your weather service. However note the below warning, so you might need to add a delay for that specific trigger.
Thus this event can not be used to run automations during startup as it would have missed these events

Look at the availability parameter in this template sensor.

    - name: "Precipitation Probability Today"
      unique_id: precipitation_probability_today
      icon: mdi:weather-rainy
      unit_of_measurement: "%"
      state: "{{ state_attr('sensor.daily_envcan', 'forecast')[0]['precipitation_probability'] | int(none) }}"
      attributes:
        provider: "Environment Canada" 
      availability: >
        {{ state_attr('sensor.daily_envcan', 'forecast') != none
            and state_attr('sensor.daily_envcan', 'forecast')[0] is defined
            and state_attr('sensor.daily_envcan', 'forecast')[0]['precipitation_probability'] is defined
            and is_number(state_attr('sensor.daily_envcan', 'forecast')[0]['precipitation_probability']) }}