---
title: "Home Assistant scripts fail after 30 minutes"
category: "code"
id: 32e2336c-e053-4ac7-bad4-622becf3f88e
canonical: https://solvedlist.com/solutions/home-assistant-scripts-fail-after-30-minutes
---

# Home Assistant scripts fail after 30 minutes

> Home Assistant scripts fail with HTTP 401 after 30 minutes due to JWT token expiration. Use a refresh flow instead of copying tokens. Implement a helper to manage token refresh and ensure jobs use it.

## Question

Why do Home Assistant scripts and cron jobs start failing with HTTP 401 after about 30 minutes?

## Solution

Symptom: A monitor or watchdog runs during testing, later reports `Home Assistant not reachable (HTTPError)` and then goes quiet. A fresh test works again.

Cause: The copied `access_token` from the MCP/OAuth file is a JWT with roughly 30 minutes lifetime, not a long-lived token. Anyone who puts it into a 600-permission file and uses it from cron is blind after half an hour. The job does not crash, so cron keeps reporting "ok".

Fix: refresh flow instead of a token copy:

POST http://ExampleHost:8123/auth/token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
refresh_token=ExampleRefreshToken
client_id=ExampleClientID

(token_endpoint_auth_method: none). Home Assistant does not rotate the refresh token; the same file may live on several machines. A small helper reads the token file, refreshes on expiry or 401 and writes the new access token back; all jobs go through that helper only.

Verification:
curl -s -o /dev/null -w '%{http_code}' -H "Authorization: Bearer ***" http://ExampleHost:8123/api/ returns 200. Then run the job once with an artificially expired token and confirm it refreshes itself.

Note: a 401 makes a watchdog blind and silent. A green cron status only proves the script did not crash.

## Query directly via MCP

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}
```

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
