#Unable to determine why automation always returns false

1 messages · Page 1 of 1 (latest)

bitter python
#

Below is my automation, it's intended to send me a notification whenever the temperature will freeze later in the evening. I'm using the National Weather Service as my weather provider.

alias: Check for Freezing Temperatures After Sunrise
description: >-
  Queries weather forecast 1 hour after sunrise to check for freezing temps
  tonight/tomorrow morning
triggers:
  - event: sunrise
    offset: "01:00:00"
    trigger: sun
conditions: []
actions:
  - alias: Get hourly weather forecast
    action: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.kgpm
    response_variable: weather_forecast
  - alias: Check for freezing temperatures
    choose:
      - conditions:
          - condition: template
            value_template: >
              {% set forecast_data = weather_forecast['weather.kgpm'].forecast %}
              {% set now = now() %}
              {% set tomorrow_morning = now.replace(hour=12, minute=0, second=0) + timedelta(days=4) %}
              {% set weather_entity = states['weather.kgpm'] %}
              {% set temp_unit = weather_entity.attributes.temperature_unit %}

              {% set freezing_found = false %}
              {% for entry in forecast_data %}
                {% set forecast_time = as_datetime(entry.datetime) %}
                {% if forecast_time >= now and forecast_time <= tomorrow_morning %}
                  {% if temp_unit == '°F' and entry.temperature <= 32 %}
                    {% set freezing_found = true %}
                  {% elif temp_unit == '°C' and entry.temperature <= 0 %}
                    {% set freezing_found = true %}
                  {% endif %}
                {% endif %}
              {% endfor %}
              {{ freezing_found }}
        sequence:
        - alias: Notify mobile
          action: notify.phone
          data:
            message: "It's gonna freeze tonight, pull in the bug's sleep medicine."
            title: "Shit's getting cold"

    default:
      - alias: Log no freezing conditions
        data:
          title: No Freezing Forecast
          message: |
            No freezing temperatures forecasted for tonight/tomorrow morning.
        action: persistent_notification.create
mode: single
#

The value of the now, tomorrow_morning, and temp_unit are the following:

now = 2025-12-27 23:09:20.308763-06:00
tomorrow_morning = 2025-12-31 12:00:00.308763-06:00
temp_unit = °F
#

I've programmed for 8 years, I'm very well familiar with the flow of logic. But without basic debugging tools - like an interactive console or basic logging - I'm finding it difficult and frustrating to figure out the problem

#

The Traces tab has been of a huge help, but it only records variables that have been modified instead of recording all variables like a proper breakpoint/debugger would

#

The Template editor is also a notable tool, but due to that and the Actions tab being separated, I'm not able to use them together very easily

silk moth
#

You have to use a namespace to modify a variable outside a for loop like that

bitter python
#

...why? 😭

#

I'll look into that, thank you

#

Where was that documented?

silk moth
bitter python
#

I love the part where Jinja clarifies why they made this decision 😭

#

Thank you for helping me so fast, I really do appreciate that /gen