Publish OpenAI Codex usage limits to Home Assistant over MQTT.
Codex Home Assistant MQTT Bridge reads the same Codex usage information used by the Codex client, normalizes it, and publishes Home Assistant MQTT Discovery entities for the 5-hour and weekly usage windows.
It is designed to be lightweight: there are no npm package dependencies, and the included Windows helper scripts can run the bridge silently in the background at sign-in.
- Publishes Codex usage to Home Assistant via MQTT.
- Creates Home Assistant sensors automatically with MQTT Discovery.
- Tracks 5-hour usage and remaining percentage.
- Tracks weekly usage and remaining percentage.
- Publishes retained state and availability topics for reliable Home Assistant restarts.
- Publishes reset times:
Codex 5h Reset:16:49Codex Weekly Reset:12/05 - 09:01
- Publishes plan, credits, and limit status.
- Runs without npm dependencies.
- Includes Windows startup helpers that run silently in the background.
Codex usage shown on a small Home Assistant dashboard display:
Home Assistant MQTT device and sensor entities:
The bridge publishes these sensors:
| Sensor | Example |
|---|---|
Codex 5h Used |
49% |
Codex 5h Remaining |
51% |
Codex 5h Reset |
16:49 |
Codex Weekly Used |
8% |
Codex Weekly Remaining |
92% |
Codex Weekly Reset |
12/05 - 09:01 |
Codex Credits |
0 credits |
Codex Plan |
plus |
Codex Limit Status |
OK |
All sensor values come from the retained JSON state payload on codex/usage/state, with bridge availability published on codex/usage/availability.
- The bridge reads Codex authentication data from your local Codex configuration directory, or from an optional access token fallback.
- It requests Codex usage information from the Codex backend usage endpoint.
- It flattens the response into simple values suitable for MQTT state payloads.
- It publishes MQTT Discovery configs so Home Assistant creates the sensors automatically.
- It periodically republishes the current usage state and availability.
Default MQTT topics:
homeassistant/sensor/codex_usage/...
codex/usage/state
codex/usage/availability
- Windows, macOS, or Linux with Node.js 20 or newer.
- Home Assistant with MQTT enabled.
- A working MQTT broker, such as the Mosquitto add-on.
- A valid Codex login on the machine running the bridge.
The Windows helper scripts can also use the Node.js runtime bundled with the Codex desktop app when it is available.
- Copy
.env.exampleto.env. - Edit
.envwith your MQTT settings. - Double-click
start.batto test the bridge. - Double-click
install-startup-task.batto run it silently when you sign in to Windows.
Example .env:
MQTT_URL=mqtt://192.168.1.50:1883
MQTT_USERNAME=ha_demo_user
MQTT_PASSWORD=your-password
POLL_SECONDS=60Do not commit your .env file. It contains your MQTT password and may contain Codex-related credentials.
If Node.js is installed and available in your terminal:
node src/index.jsOr through npm:
npm startIf you are using the Windows helper:
.\run.ps1To create a Windows startup shortcut:
powershell -NoProfile -ExecutionPolicy Bypass -File .\install-startup-task.ps1Or double-click:
install-startup-task.bat
This creates a shortcut in the user's Windows Startup folder and starts the bridge immediately. It does not keep a terminal window open.
To remove the startup shortcut:
powershell -NoProfile -ExecutionPolicy Bypass -File .\uninstall-startup-task.ps1When installed as a silent Windows startup app, logs are written to:
logs/bridge.log
| Variable | Default | Description |
|---|---|---|
MQTT_URL |
mqtt://homeassistant.local:1883 |
MQTT broker URL. The built-in MQTT client supports mqtt://. |
MQTT_USERNAME |
empty | MQTT username. |
MQTT_PASSWORD |
empty | MQTT password. |
CODEX_HOME |
~/.codex |
Codex config/auth directory. |
CODEX_ACCESS_TOKEN |
empty | Optional bearer token fallback. |
CHATGPT_ACCOUNT_ID |
empty | Optional account ID override when needed by the Codex backend request. |
CODEX_BACKEND_URL |
https://chatgpt.com/backend-api/wham/usage |
Optional backend usage endpoint override. |
CODEX_REFRESH_TOKEN_URL |
https://auth.openai.com/oauth/token |
Optional token refresh endpoint override. |
POLL_SECONDS |
60 |
How often to publish usage updates. |
MQTT_BASE_TOPIC |
codex/usage |
MQTT state/availability topic prefix. |
HA_DISCOVERY_PREFIX |
homeassistant |
Home Assistant MQTT discovery prefix. |
DEVICE_ID |
codex_usage |
Home Assistant device identifier. |
DEVICE_NAME |
Codex Usage |
Home Assistant device name. |
5-hour usage gauge:
type: gauge
entity: sensor.codex_5h_used
name: Codex 5h
min: 0
max: 100
severity:
green: 0
yellow: 70
red: 90Weekly usage gauge:
type: gauge
entity: sensor.codex_weekly_used
name: Codex Weekly
min: 0
max: 100
severity:
green: 0
yellow: 70
red: 90.
├── .env.example
├── README.md
├── package.json
├── start.bat
├── run.ps1
├── run-service.ps1
├── run-hidden.vbs
├── install-startup-task.bat
├── install-startup-task.ps1
├── uninstall-startup-task.ps1
├── docs/
│ └── images/
└── src/
├── auth.js
├── codexUsage.js
├── config.js
├── index.js
├── mqttHa.js
└── simpleMqtt.js
src/index.js— bridge entry point and polling loop.src/config.js— environment/config loading.src/auth.js— Codex authentication handling.src/codexUsage.js— Codex usage request and normalization.src/mqttHa.js— Home Assistant MQTT Discovery and state publishing.src/simpleMqtt.js— minimal MQTT client implementation.start.bat,run.ps1,run-service.ps1,run-hidden.vbs— Windows run helpers.install-startup-task.ps1/uninstall-startup-task.ps1— Windows startup shortcut management.
This project has no npm dependencies. To validate JavaScript syntax:
npm run checkThe check script runs node --check against the source files.
- Confirm MQTT is enabled in Home Assistant.
- Confirm your MQTT broker URL, username, and password in
.env. - Check that Home Assistant MQTT Discovery is enabled.
- Check the bridge logs for MQTT connection errors.
- Confirm Codex is logged in on the same machine running the bridge.
- Confirm
CODEX_HOMEpoints to the correct Codex config/auth directory if you use a non-default location. - Run the bridge manually with
node src/index.jsto see foreground logs.
- Re-run
install-startup-task.batorinstall-startup-task.ps1. - Check
logs/bridge.login the repository directory. - Confirm Node.js is available, or that the Codex desktop app bundled Node.js runtime can be found by the helper scripts.
- Do not commit
.env. - Do not share
CODEX_ACCESS_TOKENif you use it. - The bridge does not publish your Codex token or MQTT password to Home Assistant.
- MQTT state payloads contain usage/limit information only, not authentication headers, refresh tokens, or raw credential objects.
- If OpenAI changes the Codex backend endpoint or response shape, the bridge may need an update.
This project uses the Codex backend usage endpoint used by Codex itself. It is not a separately documented public API.
Because of that, endpoint paths, authentication behavior, or response formats may change without notice. If that happens, update the bridge before relying on it for automations or monitoring.

