Skip to content

ESPHome Native API Integration

Independent projects

PHOS is not related to, affiliated with, or maintained by ESPHome. ESPHome is an independent project; this guide describes an integration between the two projects, not an official partnership or endorsement.

PHOS can connect directly to a configured ESPHome device through the encrypted ESPHome Native API. It reads supported sensor entities over that connection; it does not need an MQTT broker. This is separate from ESPHome over MQTT, which sends MQTT messages through a broker. It is also separate from the PHOS REST API: Native API is the direct ESPHome device connection, not an HTTP API exposed by PHOS.

Configure the complete supported PHOS-side ESPHome workflow in Web Admin: add the device, enter its address, port and write-only encryption key, map supported entities, set freshness, save, and authorize measurements. You do not need to write or edit PHOS YAML. The PHOS YAML example below shows an illustrative result of those Web Admin settings; it is for reference, not a file you need to create. The ESPHome device itself still needs its own ESPHome configuration.

ESPHome is firmware and tooling for network-connected devices such as an ESP32. Its Native API is a direct client/server protocol. PHOS is the API client and the ESPHome device runs the API server. PHOS validates received readings and adds eligible, authorized measurements to its physical context. An AI agent can retrieve that context through MCP when its PHOS identity has permission. The agent is not called automatically when a sensor changes.

This guide covers the user workflow. The technical Native API reference documents the configuration schema and implementation boundaries. PHOS does not use this integration to call ESPHome switches, lights, climate controls, buttons, or other device actions.

Connection flow

flowchart TD
  device[ESPHome device<br/>ESP32 sensors] <-->|Encrypted ESPHome Native API| provider[PHOS ESPHome provider]
  subgraph phos[PHOS]
    direction LR
    provider -->|Validated mapped observations| context[Physical Context]
    context --> server[PHOS MCP Server]
  end
  server <-->|MCP| client[MCP client]
  agent[AI Agent] --> client

  classDef hardware fill:#334155,stroke:#94A3B8,color:#fff
  classDef phosNode fill:#0F4C5C,stroke:#49E7FF,color:#fff
  classDef agent fill:#5B21B6,stroke:#C4B5FD,color:#fff
  classDef protocol fill:#312E81,stroke:#818CF8,color:#fff
  class device hardware
  class provider,context,server phosNode
  class client protocol
  class agent agent
  linkStyle 3 stroke:#818CF8,stroke-width:2px
  style phos fill:transparent,stroke:#49E7FF,stroke-width:3px,stroke-dasharray:6 4,color:#49E7FF

The ESPHome device and AI agent are outside the PHOS boundary. The device publishes entity state over its encrypted Native API connection; PHOS maps and validates the state before adding it to physical context. An MCP client then reads PHOS context under the agent identity's permissions. No MQTT broker is part of this path.

Before you begin

  • PHOS is running with its optional ESPHome dependency installed. Maintainer setup details are in the technical Native API reference.
  • An ESPHome device is online and reachable from PHOS over the network.
  • The device has the ESPHome Native API enabled on the port PHOS will use (default 6053).
  • The device exposes a numeric sensor or binary sensor that PHOS supports.
  • You have that device's Native API encryption key. PHOS requires encrypted Native API authentication; it does not support a plaintext connection.
  • You can sign in to PHOS Web Admin and authorize the eventual mapping.

No MQTT service, MQTT broker, or ESPHome discovery proposal is needed. Add the device by its host name or IP address in Web Admin. The connection must be routable from PHOS; devices do not have to rely on mDNS discovery.

ESPHome device example

This example publishes numeric temperature and humidity entities from an ESP32 with a DHT22. It uses ESPHome's direct API and contains no MQTT configuration. The values are examples; choose a GPIO pin and sensor wiring appropriate for the board. Store Wi-Fi and API encryption secrets in the ESPHome secret store.

esphome:
  name: room-sensor
  friendly_name: Room Sensor

esp32:
  board: esp32dev

logger:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

api:
  port: 6053
  encryption:
    key: !secret api_encryption_key

sensor:
  - platform: dht
    pin: GPIO4
    model: DHT22
    update_interval: 30s
    temperature:
      name: Room temperature
      id: room_temperature
      unit_of_measurement: '°C'
    humidity:
      name: Room relative humidity
      id: room_humidity
      unit_of_measurement: '%'

ESPHome's API encryption key is a 32-byte Base64 key. Generate a unique key for this device, keep it in ESPHome's secrets, and enter the same value in PHOS's write-only Encryption key field. Do not copy the key into PHOS YAML or share it between devices. The API component uses port 6053 by default; the explicit port above makes the example and PHOS settings easy to compare.

The example follows the official ESPHome Native API documentation, DHT sensor documentation, and Wi-Fi secrets example. It has been checked against those documented options and the PHOS configuration contract; it has not been compiled, flashed, or physically tested.

Add and configure the device in PHOS

Use the Connectivity → ESPHome page in Web Admin:

  1. Select Add ESPHome device and enter a device name, optional zone, reachable host name or IP, API port, and encryption key. The key field is write-only. PHOS stores it through its secret service; it is not shown again or written to the runtime YAML configuration.
  2. The device is added disabled and with no authorized measurements. Use its Enable device action to start the long-lived connection. The row reports the runtime state; enabling does not by itself mean the device is connected.
  3. Use Test connection to make a one-time encrypted connection. PHOS reads sanitized device information and a bounded entity inventory, then disconnects. A successful test does not enable the device or prove its long-lived provider is connected.
  4. When the enabled provider is connected, open the device's Measurements table. Each supported sensor or binary sensor can be mapped to a PHOS measurement. The Native API entity key and type bind the mapping to that entity. Give it a readable measurement name and select its semantic quantity.
  5. For numeric sensors, PHOS derives the canonical output unit from the selected quantity. The sensor should report a value in the corresponding unit; PHOS does not convert an arbitrary source unit. For binary sensors, choose the PHOS meaning for the source's true and false states.
  6. Set a freshness limit appropriate to the device's update interval. New mappings default to 120 seconds; the accepted range is 1 to 86,400 seconds. Save selected mappings. Saving creates or edits mappings but leaves them unauthorized.
  7. Use the separate Authorize action for each eligible measurement. PHOS checks that the device is enabled, connected, and currently reports the selected entity. Authorization is persisted separately from mapping edits.

Saving configuration applies ESPHome changes through the running PHOS lifecycle; a full PHOS restart or configuration reload is not required. The device must remain enabled and connected for new states to arrive. Use Refresh device status in its row to read current status.

The following illustrative, secret-free PHOS configuration is the persisted shape produced by those Web Admin settings. It is included to show what PHOS saves; Web Admin is the normal configuration interface. Never put the encryption key itself in this file.

esphome:
  enabled: true
  devices:
    - id: room-sensor
      display_name: Room sensor
      zone: living-room
      enabled: true
      host: 192.0.2.10
      port: 6053
      encryption_key_ref: esphome.room_sensor_key
      timeout_seconds: 10
      retry_initial_seconds: 1
      retry_max_seconds: 30
      mappings:
        - id: room-temperature
          entity_key: 1001 # Example only; use the key reported by this device.
          entity_type: numeric_sensor
          display_name: Room temperature
          quantity: temperature_c
          unit: c
          enabled: false
          stale_after_seconds: 120
          value_map: null
        - id: room-humidity
          entity_key: 1002 # Example only; use the key reported by this device.
          entity_type: numeric_sensor
          display_name: Room relative humidity
          quantity: humidity_percent
          unit: '%rh'
          enabled: false
          stale_after_seconds: 120
          value_map: null

The two entity keys above are illustrative integers, not IDs promised by the example firmware. Use the keys in PHOS's current entity inventory. Mapping enabled: false means saved but not authorized. The reference schema, mapping constraints, and backwards-compatible optional zone are described in the technical reference.

Supported entities and mappings

PHOS reads only numeric sensor and binary sensor entities. It may show other Native API entity descriptions in administration inventory, but does not map them to physical observations or call their device actions.

ESPHome Native API entity PHOS observation Mapping in Web Admin Availability and freshness Limits
Numeric Sensor A numeric physical measurement Select a supported semantic quantity; PHOS assigns its canonical unit Requires the connected provider and incoming numeric states; age is measured at PHOS receipt against the mapping's freshness limit. Value must use the selected quantity's unit; the Native API mapping does not convert arbitrary source units.
BinarySensor A semantic state such as motion or contact Select a supported binary quantity and map true and false to PHOS meanings Requires the connected provider and an accepted state update; it uses the same PHOS receipt-time freshness policy. Only supported state meanings are accepted; raw text is not exposed as an observation.
Switch, light, button, climate, cover, text, or other entity None Inventory only; not eligible for mapping Does not become an observation through this provider. PHOS does not issue ESPHome control or service calls.

Numeric quantities available to mappings include temperature and AHT21 temperature (c), humidity (%rh), pressure (hpa), eCO₂/CO₂ (ppm), TVOC/VOC (ppb), AQI (index), illuminance (lx), distance (m), particulate concentrations (ug/m3), acceleration and vibration (m/s2), and angular velocity (deg/s). The quantity/unit selection is validated by PHOS; see the complete mapping reference.

Binary meanings include presence (present / absent), occupancy (occupied / unoccupied), motion (moving / stationary), contact (open / closed), and proximity (near / far). An additional unknown state is supported for each. The mapping editor assigns PHOS meanings to the Native API boolean identities; it does not accept arbitrary payload identifiers.

Mappings are specific to an entity key and type. Saving a mapping does not authorize it. Authorization can be granted only while the matching entity is available in the authenticated device inventory; revoking it removes that source from authorized context without deleting the saved mapping.

Check the connection and observation

The configured device row shows connecting, connected, unavailable, or disabled as applicable. Test connection checks the encrypted API login, device information, and entity listing in a short-lived diagnostic connection. The runtime connection state is shown separately. In the mapping editor, supported entities appear from the live inventory. Measurements show a value only for an authorized, available, fresh observation; otherwise PHOS displays no current value.

ESPHome state callbacks do not supply a trusted measurement timestamp to PHOS. Freshness is based on when PHOS received the state and the mapping's configured freshness limit. After a successful connection, PHOS briefly withholds initial state callbacks while the connection settles. After a disconnect, the provider retries with a bounded backoff. A stale or unavailable reading is not shown as a current value.

Check Expected result
ESPHome network Device is online and its configured Wi-Fi/Ethernet connection is usable.
Native API The device configuration includes api: and the configured API port is reachable from PHOS.
Encryption The API encryption key in ESPHome matches the write-only PHOS key for that device.
PHOS test Test connection succeeds; this is a one-time check and not runtime status.
Runtime provider The enabled device row reports connected; supported numeric and binary entities appear in its editor inventory.
Mapping The saved row shows the intended measurement name, quantity, and authorization state.
Live state A mapped, authorized, fresh value appears in Measurements and in authorized MCP physical context.

Read it through MCP

An AI agent with read_physical_context permission can call phos_get_physical_context or read phos://physical-context. A fresh ESPHome-origin observation appears in external_observations, separate from PHOS's built-in environmental readings. For example:

{
  "external_observations": [
    {
      "provider": { "id": "room-sensor", "type": "esphome" },
      "device_name": "Room sensor",
      "display_name": "Room temperature",
      "quantity": "temperature_c",
      "value": 21.5,
      "unit": "c",
      "status": "available",
      "available": true,
      "fresh": true,
      "provenance": ["esphome-native-api"],
      "authorization": "authorized"
    }
  ]
}

This is an illustrative subset; the response also carries source identity, entity identity, zone, receipt time, age, and other status metadata. A stale or unavailable entry has no current measurement value. MCP authentication and the agent's read_physical_context permission are separate from ESPHome's Native API encryption. Receiving a sensor update does not invoke an AI model; the agent decides whether and how to use a context it is permitted to read. See Connect PHOS to an AI agent for the agent workflow.

Troubleshooting

Symptom Checks and supported action
Device is unreachable Confirm the host/IP and port in Advanced device settings, that the device is online, that the Native API is enabled, and that routing/firewall rules permit PHOS to reach it. Port 6053 is ESPHome's default unless changed.
Test connection fails Check the Native API encryption key on both sides, the configured address and port, and whether the PHOS environment has the ESPHome optional dependency. Web Admin shows a sanitized success/failure result; it does not expose the library's raw exception.
Test succeeds but runtime is not connected Check that the device is enabled. A one-time test disconnects after inventory retrieval and does not establish runtime state. Refresh the device status and check the row's operational state.
Sensor is missing from the editor Confirm the entity is a numeric Sensor or BinarySensor and that the enabled PHOS provider is connected. Other entity types are inventory-only.
Mapping is not accepted Check the entity key/type, choose a supported PHOS quantity, use the expected physical unit, and for a binary entity select meanings for both source states. Save changes, then authorize separately.
No observation appears Confirm the mapping is saved and authorized, the device remains enabled and connected, and the entity is still in its inventory. A successful connection alone does not authorize or create an observation.
Observation becomes stale Confirm ESPHome continues sending updates and set the mapping freshness limit to suit its actual update interval. PHOS measures age from receipt time; it does not rely on an ESPHome device timestamp.
Connection is interrupted Restore network/API reachability and refresh status. The provider attempts reconnection with bounded backoff; wait for its status to return to connected and for a new state update.
Agent cannot read the measurement Check that the mapping remains authorized and the agent's PHOS identity has read_physical_context. MCP permission is independent of Native API key validity.

Do not disable API encryption or expose the device API to the public Internet as a troubleshooting shortcut.

Security and privacy

  • Keep the ESPHome API endpoint on a trusted, access-controlled network. Permit PHOS to reach the configured host and port; do not expose it directly to the public Internet.
  • Use API encryption and a unique key per device. The same key must be configured in ESPHome and entered through PHOS's write-only onboarding or key replacement field.
  • PHOS stores the key through its secret service. The secret is not part of phos.yaml, Web Admin read responses, MCP context, or diagnostic output.
  • Add only devices and entities you intend PHOS to observe. Mapping and authorization are explicit; discovery of an entity does not authorize it.
  • Use separate PHOS MCP identities and grant only the needed profile. ESPHome encryption authenticates the device connection; PHOS MCP permissions govern which external agent identities can read the resulting physical context.

See Web Admin device management for account security, and the ESPHome Native API technical reference for secret and provider lifecycle details.