Skip to content

External Devices — MQTT & ESPHome

PHOS can receive supported observations from sensors connected through a compatible controller and from networked devices that publish readings to MQTT. External sensors can be placed where they are useful and can add configured temperature, humidity, pressure, air-quality, light, distance, particulate, motion, presence, occupancy, contact, or proximity observations. What PHOS can accept depends on its supported quantity list and the mapping you configure.

Independent projects

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

An authorized AI agent can read accepted external observations through PHOS's MCP physical context. PHOS validates and labels the observation; the agent decides what it means or whether to request a supported PHOS action.

How MQTT connects a device to PHOS

An external device is the publisher: it sends a message to a named topic at an MQTT broker. PHOS is an MQTT subscriber: its optional MQTT provider connects to that existing broker and listens to the exact topics in configured source mappings. PHOS does not run a broker, and this observation path does not provide remote actuator commands.

A message has a topic and a payload. A scalar mapping accepts a UTF-8 numeric payload such as 21.5; a JSON mapping reads one configured object field; an enumerated mapping translates exact text values through an explicit map. Every source mapping also defines its quantity and compatible unit. For full supported fields, accepted units, security boundaries, validation and diagnostics, see the MQTT technical reference.

The broker can require a username, password and TLS. Use a distinct client ID for PHOS and each device. Give PHOS read access only to the configured state and availability topics. Do not expose an unauthenticated broker to the Internet. ESPHome and PHOS credential examples below use secret references/placeholders; never commit real credentials.

For a direct encrypted device connection that does not use a broker, see the ESPHome Native API guide. The Native API has its own device, entity-mapping, and authorization workflow.

Before configuring the example, have an operational PHOS runtime with its MQTT optional dependency available, an MQTT-capable device and supported sensor, an accessible broker, network access from both devices to that broker, and administrator access to PHOS configuration.

flowchart TD
  dht[DHT11 temperature sensor] --> esp32[ESP32 running ESPHome<br/>MQTT publisher]
  esp32 -->|Discovery metadata and sensor readings| broker[MQTT broker]
  subgraph phos[PHOS]
    subscriber[MQTT subscriber]
    review[Web Admin mapping review]
    context[Authorized physical context]
    mcp[PHOS MCP server]
    subscriber -->|Candidate for review| review
    review -->|Approved mapping| subscriber
    subscriber -->|Accepted fresh observation| context
    context --> mcp
  end
  broker -->|PHOS subscription| subscriber
  agent[AI Agent] -->|MCP read| mcp

  classDef hardware fill:#334155,stroke:#94A3B8,color:#fff
  classDef service fill:#78350F,stroke:#FBBF24,color:#fff
  classDef phosNode fill:#0F4C5C,stroke:#49E7FF,color:#fff
  classDef agent fill:#5B21B6,stroke:#C4B5FD,color:#fff
  class dht,esp32 hardware
  class broker service
  class subscriber,review,context,mcp phosNode
  class agent agent
  linkStyle 7 stroke:#818CF8,stroke-width:2px
  style phos fill:transparent,stroke:#49E7FF,stroke-width:3px,stroke-dasharray:6 4,color:#49E7FF

The ESP32 publishes the DHT11 reading and ESPHome discovery metadata to the broker. PHOS subscribes to the broker; the Web Admin presents a discovered candidate so you can review its mapping and approve it. Approval is required before PHOS exposes the accepted reading through its physical context.

ESPHome temperature example

ESPHome is one way to configure a network-connected microcontroller and sensor. The device publishes through MQTT; PHOS then ingests the mapped message. This example uses an ESP32 with a DHT11 temperature sensor. It is source-reviewed against ESPHome's current MQTT component and DHT sensor documentation, but it has not been compiled, flashed, or physically tested here.

Configure the ESPHome device to publish a scalar on the same topic PHOS will subscribe to. The state_topic is explicit, and retain: false matters because ESPHome retains component state by default while PHOS withholds retained measurement messages rather than presenting an old broker value as a new reading. The per-entity MQTT options are documented by the ESPHome MQTT component.

esphome:
  name: room-sensor
  friendly_name: Room Sensor

esp32:
  board: esp32dev

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

logger:

mqtt:
  broker: mqtt.example.com
  port: 8883
  tls: true
  username: !secret mqtt_username
  password: !secret mqtt_password
  topic_prefix: phos/room-sensor
  discovery: true
  discovery_prefix: homeassistant
  discovery_retain: true

sensor:
  - platform: dht
    pin: GPIO4
    model: DHT11
    update_interval: 30s
    temperature:
      name: Room temperature
      id: room_temperature
      device_class: temperature
      state_class: measurement
      state_topic: phos/room-sensor/temperature
      retain: false

This device YAML is for the ESPHome device; it is separate from PHOS's own configuration. ESPHome MQTT discovery metadata is retained so PHOS can list the candidate when it connects, while retain: false keeps sensor state messages non-retained so a new reading must arrive after approval.

The example assumes the broker has a certificate trusted by ESPHome's ESP32 TLS support and that PHOS is configured to verify that broker's certificate with the corresponding CA. ESPHome supports MQTT credentials and TLS on ESP32; configure the broker, authentication and certificate according to its deployment. The topic and non-retained state behavior are explicit so they do not rely on ESPHome's default entity topic or retained-state setting.

The sensor should publish a numeric scalar, for example 21.5, to phos/room-sensor/temperature. The exact value and publishing interval depend on the sensor and device runtime.

Configure PHOS

You can configure the PHOS MQTT broker and review, map, and approve a supported ESPHome discovery candidate in Web Admin; you do not need to write or edit a PHOS YAML file.

For this example, first configure the broker and discovery in Connectivity → MQTT. Enable MQTT, add or edit the broker, and enter its hostname, port, a PHOS client ID distinct from the ESP32's, credentials in the write-only fields, and the required TLS settings. Enable discovery and use the same discovery prefix configured on the ESPHome device (homeassistant in the example). Save the broker, then use Save & Apply MQTT enablement. Broker changes apply dynamically. Test Connection checks only whether PHOS can complete an MQTT broker handshake; it does not confirm that the ESP32 is publishing sensor data.

Next open Connectivity → MQTT Discovery. Expand the candidate for the Room temperature sensor and review its device, state topic, numeric payload, temperature quantity, and unit. Adjust the mapping and freshness settings if needed. Use Save changes to store an edited proposal, then Approve it as a separate action. Discovery does not authorize a sensor automatically. Wait for a new non-retained sensor reading after approval before expecting a current value in PHOS physical context.

This no-YAML workflow applies when the device publishes supported MQTT Discovery metadata. The fields below are an example of the ESPHome device configuration; they configure the ESP32 and are not PHOS configuration.

The saved PHOS configuration contains the reviewed and approved mapping. The following is a simplified illustration of the broker record as represented in PHOS configuration after Web Admin review. PHOS generates the candidate ID and revision, and saves additional approval metadata; this excerpt is explanatory, not a file to create or copy into PHOS:

# Illustrative PHOS configuration excerpt produced by Web Admin review.
mqtt:
  brokers:
    - id: mqtt-room
      discovery_approvals:
        - mapping:
            id: discovery-<generated-candidate-id>
            topic: phos/room-sensor/temperature
            quantity: temperature_c
            format: scalar
            unit: c
            stale_after_seconds: 120

For devices that do not publish supported discovery metadata, manual source mapping is still configured through the canonical MQTT configuration workflow described in the MQTT technical reference.

The device's discovery metadata proposes the topic and measurement semantics; review them in Web Admin before approval. The MQTT broker form stores credentials through PHOS's protected, write-only credential handling. For a private CA, configure both clients to trust it. The canonical reference covers the complete broker and manual mapping schemas, TLS validation, and configuration lifecycle.

Follow the observation to an AI agent

The diagram above shows the roles and boundary: the sensor, ESPHome device, and broker are external; the MQTT subscriber, mapping review, authorized context, and MCP server are PHOS components; the AI agent remains outside PHOS. The MCP read is subject to the agent identity's read_physical_context permission. A compatible agent can read the phos://physical-context resource or call phos_get_physical_context; current MQTT readings appear in its external_observations array when accepted and authorized.

For example, the entry can contain quantity: "temperature_c", a numeric value, unit: "c", status: "available", fresh: true, provenance: ["mqtt"], and the configured source identity. The precise row also includes availability, age, provider, optional device/entity identity, zone, and authorization metadata. A stale or unavailable source is not shown with a current measurement value. A representative excerpt is:

{
  "source_id": "mqtt-room/room-temperature",
  "provider": { "id": "mqtt-room", "type": "mqtt" },
  "quantity": "temperature_c",
  "value": 21.5,
  "unit": "c",
  "status": "available",
  "available": true,
  "fresh": true,
  "provenance": ["mqtt"],
  "authorization": "authorized"
}

This excerpt omits other returned fields such as receipt time, age, zone, and optional device/entity identity. A stale or unavailable source is not shown with a current measurement value. External MQTT observations are current-value context; they are not added to native measurement history.

MCP resource reads are authenticated and permission-checked. A resource-change notification, when used by a compatible MCP client, is only a cue to read again; it is not a measurement payload and does not automatically invoke an AI model. The agent decides whether to inspect the new context or act on it. See the AI-agent integration overview for agent access and permissions.

Check that the observation is working

Use each stage to narrow down where the reading stops:

  1. ESPHome network: confirm the device reports that Wi-Fi is connected in its normal ESPHome logs or management client.
  2. Broker connection: confirm the ESPHome device and PHOS can reach the same broker listener with their own client IDs and credentials. Test Connection in PHOS checks only PHOS-to-broker connectivity.
  3. Topic and payload: use an authorized MQTT client to observe phos/room-sensor/temperature. Expect a non-retained numeric payload, not a JSON object, for this example. Confirm the topic exactly matches the PHOS mapping.
  4. PHOS mapping: verify MQTT is enabled and the configured source uses the matching topic, temperature_c, scalar, and unit c. Check MQTT provider status in Web Admin. A connected broker alone does not prove a sensor message was accepted.
  5. Availability and freshness: publish new readings within the configured stale_after_seconds interval. If an availability topic is configured, confirm its payload matches the mapped available/unavailable strings. A missing or stale value is not replaced with zero.
  6. MCP access: read phos://physical-context as an identity allowed to read_physical_context. Check external_observations for the MQTT source and its status. An unapproved/unconfigured source, rejected payload, stale source, or unauthorized MCP identity will not provide a fresh value.

Common causes include a topic mismatch, retained sensor state, JSON where a scalar is mapped (or the reverse), an unsupported quantity/unit, broker ACL or credential errors, and an update interval longer than the freshness limit. For source-level diagnostics, rejection rules, discovery behavior and recovery, see MQTT troubleshooting.

Scope and evidence

PHOS's MQTT provider supports configured numeric and enumerated observations; it does not expose arbitrary MQTT payloads, camera images, audio streams, or remote device commands. MQTT readings reach MCP only after they match a configured or reviewed source mapping, pass validation and remain authorized. No Home Assistant service is required by this example.

Existing automated tests cover deterministic MQTT configuration, parsing, retained-message rejection, availability, staleness and physical-context projection. This document-only task did not connect to a live broker, compile or flash ESPHome firmware, or test a physical sensor. The example is not an official compatibility certification. For lower-level mapping and provider details, use the MQTT technical reference.