PROCESS

Here is the complete, chronological workflow of how your virtual POS device interacts with Home Assistant via MQTT. This order covers everything from establishing the connection to handling active data and disconnecting.

Step 1: Establish Connection & Configure the "Last Will"

Before your app sends any data, it connects to the MQTT broker. During this initial handshake, it registers a Last Will message. This message acts as an insurance policy if your app crashes.

MQTT Configuration:

Step 2: Register the Entities (Publish Once)

Once connected, your app must tell Home Assistant how to build the device and its 3 entities. You send a strict JSON format string to unique discovery configuration topics.

Cycle A. Total Quantity Sold Entity

{"name": "Apple Total Qty Sold", "unique_id": "pos_apple_qty_001", "state_topic": "pos/apple/qty/state", "availability_topic": "pos/device/availability", "payload_available": "online", "payload_not_available": "offline", "unit_of_measurement": "units", "state_class": "measurement", "device": {"identifiers": ["unique_pos_machine_id"], "name": "Point of Sale", "model": "POS Terminal v1", "manufacturer": "Custom App"}}

Cycle B. Total Amount Sold Entity

{"name": "Apple Total Amt Sold", "unique_id": "pos_apple_amount_001", "state_topic": "pos/apple/amount/state", "availability_topic": "pos/device/availability", "payload_available": "online", "payload_not_available": "offline", "unit_of_measurement": "$", "state_class": "measurement", "device": {"identifiers": ["unique_pos_machine_id"], "name": "Point of Sale", "model": "POS Terminal v1", "manufacturer": "Custom App"}}

Cycle C. Price Per Unit Slider Entity

Because the price can be adjusted from Home Assistant, your app must immediately subscribe to the command topic specified in your slider's config.

MQTT Action: Subscribe to:

Step 4: Broadcast "Online" Status

At this point, Home Assistant has built the device card, but all entities are greyed out as “Unavailable”. Your app must broadcast that it is officially active.

MQTT Action: Publish with Retain: True.

Result: The entities wake up in the Home Assistant UI and are ready for data.

Step 5: Live Operation (Data Traffic Loop)

This is the day-to-day loop while your app is running. Data is exchanged using lightweight Plain Text payloads.

EXAMPLE

A. When Sales Occur (App -> Home Assistant)

Your app publishes numerical changes to the state topics (Retain: True or False).

B. When the Price is Adjusted in HA (Home Assistant -> App)

A user changes the slider to $2.50 in the HA dashboard. Home Assistant sends a message down to your subscription.

C. Disconnection or Crash (End of Life)

Scenario A: The App Crashes or Loses Internet Unexpectedly

The MQTT broker notices your app dropped off. The broker instantly acts on the policy created in Step 1 and broadcasts the word offline to pos/device/availability.

Scenario B: You Want to Delete the Device From HA Forever

If you decide to retire this project, you instruct your app (or MQTTX) to wipe the configurations out of Home Assistant's database by publishing empty messages.

homeassistant/sensor/pos_device/apple_qty/config
homeassistant/sensor/pos_device/apple_amount/config
homeassistant/number/pos_device/apple_price/config