{
  "$schema": "./.schemas/page.schema.json",
  "title": "Weather",
  "icon": "cloud.sun",
  "params": {
    "lat": {
      "pattern": "-?[0-9]{1,3}(\\.[0-9]{1,6})?",
      "example": "33.44",
      "default": "33.44"
    },
    "lon": {
      "pattern": "-?[0-9]{1,3}(\\.[0-9]{1,6})?",
      "example": "-94.04",
      "default": "-94.04"
    }
  },
  "title_from": {
    "ref": "place",
    "value": "places[0].name"
  },
  "sources": {
    "weather": {
      "url": "https://api.openweathermap.org",
      "secret": "openweather",
      "auth": {
        "type": "query",
        "name": "appid"
      },
      "every": "20m",
      "requests": {
        "one": "/data/3.0/onecall?lat={param.lat}&lon={param.lon}&units=imperial",
        "air": "/data/2.5/air_pollution?lat={param.lat}&lon={param.lon}",
        "air_ahead": "/data/2.5/air_pollution/forecast?lat={param.lat}&lon={param.lon}"
      },
      "derive": [
        {
          "name": "alert_count",
          "value": {
            "count": "one.alerts",
            "default": 0
          }
        },
        {
          "name": "wet_minutes",
          "value": {
            "count": "one.minutely",
            "where": {
              "path": "precipitation",
              "gt": 0
            },
            "default": 0
          }
        },
        {
          "name": "rain",
          "value": {
            "each": "one.minutely",
            "value": "precipitation",
            "buckets": 12,
            "keep": "max"
          }
        },
        {
          "name": "rain_starts",
          "value": {
            "first": "dt",
            "of": "one.minutely",
            "where": {
              "path": "precipitation",
              "gt": 0
            }
          }
        },
        {
          "name": "rain_stops",
          "value": {
            "first": "dt",
            "of": "one.minutely",
            "where": {
              "path": "precipitation",
              "equals": 0
            }
          }
        },
        {
          "name": "pressure_inhg",
          "value": {
            "path": "one.current.pressure",
            "scale": 0.02953,
            "round": 2
          }
        },
        {
          "name": "visibility_mi",
          "value": {
            "path": "one.current.visibility",
            "scale": 0.000621371,
            "round": 0
          }
        },
        {
          "name": "n_gusts",
          "value": {
            "each": "one.daily",
            "where": {
              "all": [
                {
                  "path": "dt",
                  "gt": {
                    "answer": "one.daily[0].dt"
                  }
                },
                {
                  "path": "wind_gust",
                  "gte": 46
                }
              ]
            },
            "limit": 1,
            "fields": {
              "dt": "dt",
              "text": {
                "template": "Gusts to {wind_gust|round} mph {dt|weekday}"
              },
              "icon": {
                "template": "wind"
              }
            }
          }
        },
        {
          "name": "n_air_0",
          "value": {
            "each": "air_ahead.list",
            "where": {
              "all": [
                {
                  "path": "dt",
                  "gte": {
                    "answer": "one.daily[1].sunrise"
                  }
                },
                {
                  "path": "main.aqi",
                  "gte": 3
                }
              ]
            },
            "limit": 1,
            "fields": {
              "dt": "dt",
              "word": {
                "path": "main.aqi",
                "values": {
                  "1": "Good",
                  "2": "Fair",
                  "3": "Moderate",
                  "4": "Poor",
                  "5": "Very poor"
                }
              }
            },
            "sort": {
              "by": "main.aqi",
              "order": "desc"
            }
          }
        },
        {
          "name": "n_air",
          "value": {
            "each": "n_air_0",
            "fields": {
              "dt": "dt",
              "text": {
                "template": "{word} air {dt|weekday}"
              },
              "icon": {
                "template": "lungs"
              }
            }
          }
        },
        {
          "name": "n_freeze",
          "value": {
            "each": "one.daily",
            "where": {
              "all": [
                {
                  "path": "dt",
                  "gt": {
                    "answer": "one.daily[0].dt"
                  }
                },
                {
                  "path": "temp.min",
                  "lte": 32
                }
              ]
            },
            "limit": 1,
            "fields": {
              "dt": "dt",
              "text": {
                "template": "Freezing {dt|weekday} night, {temp.min|round}°"
              },
              "icon": {
                "template": "snowflake"
              }
            }
          }
        },
        {
          "name": "n_rain_0",
          "value": {
            "each": "one.daily",
            "where": {
              "all": [
                {
                  "path": "dt",
                  "gt": {
                    "answer": "one.daily[0].dt"
                  }
                },
                {
                  "path": "rain",
                  "gte": 25.4
                }
              ]
            },
            "limit": 1,
            "fields": {
              "dt": "dt",
              "inches": {
                "path": "rain",
                "scale": 0.03937,
                "round": 1
              }
            }
          }
        },
        {
          "name": "n_rain",
          "value": {
            "each": "n_rain_0",
            "fields": {
              "dt": "dt",
              "text": {
                "template": "{inches} in of rain {dt|weekday}"
              },
              "icon": {
                "template": "cloud.rain"
              }
            }
          }
        },
        {
          "name": "n_snow",
          "value": {
            "each": "one.daily",
            "where": {
              "all": [
                {
                  "path": "dt",
                  "gt": {
                    "answer": "one.daily[0].dt"
                  }
                },
                {
                  "path": "snow",
                  "gte": 25.4
                }
              ]
            },
            "limit": 1,
            "fields": {
              "dt": "dt",
              "text": {
                "template": "Snow {dt|weekday}"
              },
              "icon": {
                "template": "snowflake"
              }
            }
          }
        },
        {
          "name": "n_some",
          "value": {
            "each": [
              "n_gusts",
              "n_air",
              "n_freeze",
              "n_rain"
            ],
            "fields": {
              "dt": "dt",
              "text": "text",
              "icon": "icon"
            }
          }
        },
        {
          "name": "notables",
          "value": {
            "each": [
              "n_some",
              "n_snow"
            ],
            "sort": {
              "by": "dt"
            },
            "fields": {
              "text": "text",
              "icon": "icon"
            }
          }
        },
        {
          "name": "notable_count",
          "value": {
            "count": "notables",
            "default": 0
          }
        },
        {
          "name": "uv_peak",
          "value": {
            "each": "one.hourly",
            "limit": 1,
            "where": {
              "path": "dt",
              "lte": {
                "answer": "one.daily[0].sunset"
              }
            },
            "sort": {
              "by": "uvi",
              "order": "desc"
            },
            "fields": {
              "dt": "dt",
              "uvi": {
                "path": "uvi",
                "round": 0
              }
            }
          }
        }
      ]
    },
    "place": {
      "url": "https://api.openweathermap.org",
      "secret": "openweather",
      "auth": {
        "type": "query",
        "name": "appid"
      },
      "every": "1d",
      "requests": {
        "places": "/geo/1.0/reverse?lat={param.lat}&lon={param.lon}&limit=1"
      }
    }
  },
  "cards": [
    {
      "id": "alerts",
      "type": "status",
      "label": "Weather alerts",
      "state": "unknown",
      "width": "full",
      "source": {
        "ref": "weather",
        "when": {
          "path": "alert_count",
          "gt": 0
        },
        "map": {
          "label": {
            "template": "{one.alerts[0].event}"
          },
          "state": {
            "path": "alert_count",
            "values": {
              "0": "ok"
            },
            "default": "warning"
          },
          "detail": {
            "if": {
              "path": "one.alerts[0].start",
              "gt": {
                "path": "one.current.dt"
              }
            },
            "then": {
              "template": "{one.alerts[0].start|day} {one.alerts[0].start|clock} – {one.alerts[0].end|clock} · {one.alerts[0].sender_name}"
            },
            "else": {
              "template": "Until {one.alerts[0].end|clock} · {one.alerts[0].sender_name}"
            }
          }
        }
      }
    },
    {
      "id": "alert-list",
      "type": "list",
      "label": "All alerts",
      "items": [
        "—"
      ],
      "source": {
        "ref": "weather",
        "when": {
          "path": "alert_count",
          "gt": 1
        },
        "map": {
          "items": {
            "each": "one.alerts",
            "sort": {
              "by": "start"
            },
            "limit": 4,
            "fields": {
              "text": "event",
              "detail": {
                "template": "{start|weekday} {start|clock} – {end|clock}"
              }
            }
          }
        }
      }
    },
    {
      "id": "now",
      "type": "stat",
      "label": "Now",
      "value": 0,
      "unit": "°F",
      "source": {
        "ref": "weather",
        "map": {
          "value": {
            "path": "one.current.temp",
            "round": 0
          },
          "delta": {
            "template": "Feels like {one.current.feels_like|round}° · {one.current.weather[0].description|title}"
          }
        }
      }
    },
    {
      "id": "today",
      "type": "stat",
      "label": "Today",
      "value": "—",
      "source": {
        "ref": "weather",
        "map": {
          "value": {
            "template": "{one.daily[0].temp.max|round}° / {one.daily[0].temp.min|round}°"
          },
          "delta": {
            "if": {
              "path": "one.daily[0].summary",
              "exists": true
            },
            "then": {
              "template": "{one.daily[0].summary}"
            },
            "else": {
              "template": "{one.daily[0].pop|percent:0} chance of rain"
            }
          }
        }
      }
    },
    {
      "id": "rain",
      "type": "columns",
      "label": "Next hour",
      "unit": "mm/h",
      "series": [
        "Rain"
      ],
      "baseline": "zero",
      "items": [
        {
          "label": "Now",
          "values": [
            0
          ]
        },
        {
          "label": "5m",
          "values": [
            0
          ]
        },
        {
          "label": "10m",
          "values": [
            0
          ]
        },
        {
          "label": "15m",
          "values": [
            0
          ]
        },
        {
          "label": "20m",
          "values": [
            0
          ]
        },
        {
          "label": "25m",
          "values": [
            0
          ]
        },
        {
          "label": "30m",
          "values": [
            0
          ]
        },
        {
          "label": "35m",
          "values": [
            0
          ]
        },
        {
          "label": "40m",
          "values": [
            0
          ]
        },
        {
          "label": "45m",
          "values": [
            0
          ]
        },
        {
          "label": "50m",
          "values": [
            0
          ]
        },
        {
          "label": "55m",
          "values": [
            0
          ]
        }
      ],
      "source": {
        "ref": "weather",
        "when": {
          "path": "wet_minutes",
          "gt": 0
        },
        "map": {
          "label": {
            "if": {
              "path": "one.minutely[0].precipitation",
              "gt": 0
            },
            "then": {
              "if": {
                "path": "rain_stops",
                "exists": true
              },
              "then": {
                "template": "Rain stopping {rain_stops|in}"
              },
              "else": {
                "template": "Rain all hour"
              }
            },
            "else": {
              "template": "Rain starting {rain_starts|in}"
            }
          },
          "items[0].values[0]": {
            "path": "rain[0]",
            "round": 1
          },
          "items[1].values[0]": {
            "path": "rain[1]",
            "round": 1
          },
          "items[2].values[0]": {
            "path": "rain[2]",
            "round": 1
          },
          "items[3].values[0]": {
            "path": "rain[3]",
            "round": 1
          },
          "items[4].values[0]": {
            "path": "rain[4]",
            "round": 1
          },
          "items[5].values[0]": {
            "path": "rain[5]",
            "round": 1
          },
          "items[6].values[0]": {
            "path": "rain[6]",
            "round": 1
          },
          "items[7].values[0]": {
            "path": "rain[7]",
            "round": 1
          },
          "items[8].values[0]": {
            "path": "rain[8]",
            "round": 1
          },
          "items[9].values[0]": {
            "path": "rain[9]",
            "round": 1
          },
          "items[10].values[0]": {
            "path": "rain[10]",
            "round": 1
          },
          "items[11].values[0]": {
            "path": "rain[11]",
            "round": 1
          }
        }
      }
    },
    {
      "id": "hours",
      "type": "columns",
      "label": "Next 24 hours",
      "unit": "°F",
      "series": [
        "Temperature"
      ],
      "baseline": "auto",
      "values_on_top": true,
      "items": [],
      "source": {
        "ref": "weather",
        "map": {
          "highlight": {
            "template": "{one.hourly[0].dt|hour}"
          },
          "items": {
            "each": "one.hourly",
            "limit": 24,
            "buckets": 12,
            "keep": {
              "min": "dt"
            },
            "fields": {
              "label": {
                "template": "{dt|hour}"
              },
              "values[0]": {
                "path": "temp",
                "round": 0
              }
            }
          }
        }
      }
    },
    {
      "id": "conditions",
      "type": "facts",
      "label": "Conditions",
      "items": [
        {
          "key": "Air quality",
          "value": "—"
        },
        {
          "key": "UV index",
          "value": "—"
        },
        {
          "key": "Wind",
          "value": "—"
        },
        {
          "key": "Humidity",
          "value": "—"
        },
        {
          "key": "Moon",
          "value": "—"
        },
        {
          "key": "Pressure",
          "value": "—"
        },
        {
          "key": "Visibility",
          "value": "—"
        }
      ],
      "source": {
        "ref": "weather",
        "map": {
          "items[0].value": {
            "path": "air.list[0].main.aqi",
            "values": {
              "1": "Good",
              "2": "Fair",
              "3": "Moderate",
              "4": "Poor",
              "5": "Very poor"
            },
            "default": "—"
          },
          "items[1].value": {
            "if": {
              "path": "uv_peak[0].dt",
              "exists": true
            },
            "then": {
              "template": "{one.current.uvi|round} now · {uv_peak[0].uvi} at {uv_peak[0].dt|clock_hour}"
            },
            "else": {
              "template": "{one.current.uvi|round} now"
            }
          },
          "items[2].value": {
            "if": {
              "path": "one.current.wind_gust",
              "exists": true
            },
            "then": {
              "template": "{one.current.wind_speed|round} mph {one.current.wind_deg|compass} · gusts {one.current.wind_gust|round}"
            },
            "else": {
              "template": "{one.current.wind_speed|round} mph {one.current.wind_deg|compass}"
            }
          },
          "items[3].value": {
            "template": "{one.current.humidity}% · dew point {one.current.dew_point|round}°"
          },
          "items[4].value": {
            "if": {
              "path": "one.daily[0].moon_phase",
              "lt": 0.03
            },
            "then": {
              "template": "New moon"
            },
            "else": {
              "if": {
                "path": "one.daily[0].moon_phase",
                "lt": 0.22
              },
              "then": {
                "template": "Waxing crescent"
              },
              "else": {
                "if": {
                  "path": "one.daily[0].moon_phase",
                  "lt": 0.28
                },
                "then": {
                  "template": "First quarter"
                },
                "else": {
                  "if": {
                    "path": "one.daily[0].moon_phase",
                    "lt": 0.47
                  },
                  "then": {
                    "template": "Waxing gibbous"
                  },
                  "else": {
                    "if": {
                      "path": "one.daily[0].moon_phase",
                      "lt": 0.53
                    },
                    "then": {
                      "template": "Full moon"
                    },
                    "else": {
                      "if": {
                        "path": "one.daily[0].moon_phase",
                        "lt": 0.72
                      },
                      "then": {
                        "template": "Waning gibbous"
                      },
                      "else": {
                        "if": {
                          "path": "one.daily[0].moon_phase",
                          "lt": 0.78
                        },
                        "then": {
                          "template": "Last quarter"
                        },
                        "else": {
                          "if": {
                            "path": "one.daily[0].moon_phase",
                            "lt": 0.97
                          },
                          "then": {
                            "template": "Waning crescent"
                          },
                          "else": {
                            "template": "New moon"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          },
          "items[5].value": {
            "template": "{pressure_inhg} inHg"
          },
          "items[6].value": {
            "template": "{visibility_mi} mi"
          }
        }
      }
    },
    {
      "id": "daylight",
      "type": "window",
      "label": "Daylight left",
      "start": "2026-01-01T12:00:00Z",
      "end": "2026-01-01T23:00:00Z",
      "start_label": "Sunrise",
      "end_label": "Sunset",
      "before_label": "Until sunrise",
      "after_label": "Until sunrise",
      "source": {
        "ref": "weather",
        "map": {
          "start": "one.daily[0].sunrise",
          "end": "one.daily[0].sunset",
          "next_start": "one.daily[1].sunrise"
        }
      }
    },
    {
      "id": "week",
      "type": "ranges",
      "label": "This week",
      "unit": "°F",
      "colors": "temperature",
      "info_colors": [
        "green",
        "blue"
      ],
      "muted": false,
      "items": [
        {
          "label": "Today",
          "low": 0,
          "high": 1
        }
      ],
      "source": {
        "ref": "weather",
        "map": {
          "items": {
            "each": "one.daily",
            "limit": 8,
            "fields": {
              "label": {
                "template": "{dt|weekday}"
              },
              "low": {
                "path": "temp.min",
                "round": 0
              },
              "high": {
                "path": "temp.max",
                "round": 0
              },
              "icon": {
                "path": "weather[0].icon",
                "values": {
                  "01d": "sun.max",
                  "01n": "moon.stars",
                  "02d": "cloud.sun",
                  "02n": "cloud.moon",
                  "03d": "cloud",
                  "03n": "cloud",
                  "04d": "cloud",
                  "04n": "cloud",
                  "09d": "cloud.drizzle",
                  "09n": "cloud.drizzle",
                  "10d": "cloud.rain",
                  "10n": "cloud.rain",
                  "11d": "cloud.bolt.rain",
                  "11n": "cloud.bolt.rain",
                  "13d": "snowflake",
                  "13n": "snowflake",
                  "50d": "cloud.fog",
                  "50n": "cloud.fog"
                },
                "default": "cloud"
              },
              "info[0]": {
                "template": "{wind_speed|round} {wind_deg|compass}"
              },
              "info[1]": {
                "if": {
                  "path": "pop",
                  "gte": 0.1
                },
                "then": {
                  "template": "{pop|percent:0}"
                },
                "else": {
                  "template": ""
                }
              }
            }
          },
          "items[0].now": {
            "path": "one.current.temp",
            "round": 0
          }
        }
      }
    },
    {
      "id": "notable",
      "type": "list",
      "layout": "chips",
      "max_items": 3,
      "items": [],
      "source": {
        "ref": "weather",
        "when": {
          "path": "notable_count",
          "gt": 0
        },
        "map": {
          "items": {
            "each": "notables",
            "fields": {
              "text": "text",
              "icon": "icon"
            }
          }
        }
      }
    }
  ]
}
