Real-time tracking of Claude Code CLI token usage and costs in Home Assistant with custom dashboard and icon.
- Claude Pro/Max subscription
- Claude Code CLI installed (
npm install -g @anthropics/claude-code) ccusagetool (comes with Claude Code CLI)- PostgreSQL (optional, for historical data)
- Custom Brand Icons integration (HACS)
- Claude Code CLI tracks your usage with proper model-specific pricing
ccusagecalculates costs per model:- Opus: $15/$75 per M tokens (input/output)
- Sonnet: $3/$15 per M tokens
- Different cache rates per model
collect_claude_tokens.pyruns every 5 min via cron:- Calls
npx ccusage daily --json --offline - Stores in PostgreSQL (optional)
- Creates
/config/scripts/claude_token_cache.json
- Calls
- Home Assistant sensors read the cache JSON
npm install -g @anthropics/claude-code
claude-code auth login # Authenticate with your Pro/Max accountnpm install -g ccusageCopy to your Home Assistant /config/scripts/ directory:
collect_claude_tokens.py- Main script (runs ccusage, creates cache)claude_token_usage.py- Alternative: reads Claude's stats-cache.json directlyclaude_token_usage_db.py- Alternative: reads from PostgreSQL
Install via HACS: https://github.com/elax46/custom-brand-icons
The Claude icon (pezbox:clawd) is pending in PR #1114: elax46/custom-brand-icons#1114
Once merged, the icon will be available. In the meantime, you can use the included pezbox-icons.js file.
*/5 * * * * python3 /config/scripts/collect_claude_tokens.pyIf using the database features, update DB credentials in collect_claude_tokens.py.
Otherwise, comment out the DB sections and just use the cache file generation.
Add the configuration from claude_sensors.yaml to your configuration.yaml.
Option A: Separate Dashboard (Recommended)
- Go to Settings → Dashboards → Add Dashboard
- Name it "Claude"
- Select "Configure UI" → Raw Configuration Editor
- Paste contents of
lovelace_claude_dashboard.yaml
Option B: Add to Existing Dashboard
- Edit your existing dashboard
- Add the cards from
lovelace_claude_dashboard.yaml
| File | Purpose | Required? |
|---|---|---|
collect_claude_tokens.py |
Main collector - runs ccusage, creates cache | YES |
claude_sensors.yaml |
HA sensor definitions | YES |
lovelace_claude_dashboard.yaml |
Dashboard configuration | YES |
pezbox-icons.js |
Temporary Claude icon (until PR #1114 merged) | Temporary |
claude_token_usage.py |
Alt: reads stats-cache.json | No |
claude_token_usage_db.py |
Alt: reads from PostgreSQL | No |
- No data showing? Check if
ccusageworks:npx ccusage daily --json - Cache file missing? Check cron logs, ensure
collect_claude_tokens.pyruns - Icon not showing? Ensure Custom Brand Icons is installed and PR #1114 is merged