#Can't access variable with dot in name

1 messages · Page 1 of 1 (latest)

golden crest
#

I'm trying to write an automation that turns on my air conditioner to cool the house if the forecast top temperature for the day is in excess of some value (currently 26 celcius).
To access the value I have to call the built-in weather integration for daily forecasts, then get the "temperature" value out of the first item in the response. However, HomeAssistant doesn't seem to be able to access it because the key is weather.forecast_home which contains a ., so it tries to access an object called weather and then a lower-level object called forecast_home. Here's the YAML:

actions:
  - target:
      entity_id: weather.forecast_home
    data:
      type: daily
    response_variable: todays_forecast
    action: weather.get_forecasts
  - if:
      - condition: template
        value_template: >-
          {{ todays_forecast.weather.forecast_home.forecast[0].temperature > 26
          }}
    then:
      - action: remote.send_command
        metadata: {}
        data:
          num_repeats: 10
          delay_secs: 0.4
          hold_secs: 0
          device: lounge_air_conditioner
          command: cool_mode
        target:
          entity_id: remote.lounge_room_broadlink

When I checked the automation traces, I can see that the variable was set correctly:

context:
  id: 01JNW37496DBN0B8K2G2Z3MT2H
  parent_id: null
  user_id: null
todays_forecast:
  weather.forecast_home:
    forecast:
      - condition: partlycloudy
        datetime: '2025-03-09T01:00:00+00:00'
        wind_bearing: 9.9
        uv_index: 8.7
        temperature: 32.9
        templow: 22.7
        wind_speed: 25.2
        precipitation: 7.7
        humidity: 39
...

But the conditional failed because it couldn't access the value:
Error: In 'template' condition: UndefinedError: 'dict object' has no attribute 'weather'

Is there a different way I can instruct HomeAssistant to access this value that doesn't rely on the overloaded . notation?

#

I think I have to change todays_forecast.weather.forecast_home.forecast[0].temperature > 26 to some other command but not sure what it is. Enclosing weather.forecast_home in single/double quotes didn't work, nor did escaping the . with a \ and I'm not sure how to access the values of a variable directly like you can for state_attr() since it's not clear in the docs.

proud needle
#

{{ states('todays_forecast.weather.forecast_home.forecast')[0].temperature > 26 }} maybe?

golden crest
#

I have been testing with this in the Template editor:

{% set todays_forecast = {'weather.forecast_home': {'forecast': [
  {
    'condition': 'partlycloudy',
    'datetime': '2025-03-09T01:00:00+00:00',
    'wind_bearing': 9.9,
    'uv_index': 8.7,
    'temperature': 32.9,
    'templow': 22.7,
    'wind_speed': 25.2,
    'precipitation': 7.7,
    'humidity': 39
  },
  {
    'condition': 'sunny',
    'datetime': '2025-03-10T01:00:00+00:00',
    'wind_bearing': 59.2,
    'temperature': 31.4,
    'templow': 21.1
  }
]}} %}
#

The value of:

{{ states('todays_forecast.weather.forecast_home.forecast') }}

is "unknown"

#

so it indexes the letter 'u' and then tries to compare to 26 🙂

#

you can see if you check {{ todays_forecast }} that the object is correctly returned but the same indexing issue of the weather.forecast_home key is present

proud needle
#

could you not use this as a condition instead
{{ state_attr('weather.forecast_home', 'temperature') > 26 }}

golden crest
#

No, this value is the current temperature (updated hourly?). I want the forecast top for the day, which isn't in the default weather.forecast_home object

proud needle
#

ah right

#
description: ""
triggers: []
conditions: []
actions:
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: daily
    response_variable: forecast_data
    target:
      entity_id:
        - weather.forecast_home
  - if:
      - condition: template
        value_template: >-
          {% set forecast = forecast_data['weather.forecast_home'].forecast[0]
          %} 

          {{ forecast.temperature > 26 }}
    then:
      - action: switch.turn_on
        metadata: {}
        data: {}
mode: single
#

i had something simalar so i pulled bits out and wrote this quickly as an example

golden crest
#

works! What a legend! ❤️

#

Is this documented anywhere that I should have looked at? I'm currently raising an issue on GH because I feel like it's either not intended that dicts should have keys with dots or that they be accessed the same way

proud needle
#

i cant really remember to be honest. i made a weather automation a couple of months ago that i just pulled from. i probably got that from somewhere to begin with and built on it

golden crest
#

either way, very grateful. Thank you for your help!