Skip to content

Retrieve sensor display precision from HA and re-format sensor values #85

@zehnm

Description

@zehnm

Reformat sensor values in the integration driver to achieve the same display format as in Home Assistant dashboards. Numeric values are currently displayed on the Remote exactly as retrieved from HA, for example, 0.789266666666667, but are shown as 0.79 on the HA dashboard.

Home Assistant Sensor Values

Home Assistant allows configuring the display precison of sensor values and the native type (str | int | float | date | datetime | Decimal | None): https://developers.home-assistant.io/docs/core/entity/sensor/

Unfortunately, most configuration values are note exposed in the entity data sent in the HA WebSocket API. The sensor values are always sent as unformated string values. For example:

{
    "entity_id": "sensor.shellyswitch25_98f4abf2c812_energy",
    "state": "0.789266666666667",
    "attributes": {
        "state_class": "total_increasing",
        "unit_of_measurement": "kWh",
        "device_class": "energy",
        "friendly_name": "shellyswitch25-98F4ABF2C812 Energie"
    },
    "last_changed": "2026-02-21T12:09:07.912976+00:00",
    "last_reported": "2026-02-21T12:09:07.912976+00:00",
    "last_updated": "2026-02-21T12:09:07.912976+00:00",
    "context": {
        "id": "01KJ01N1J8ARG2F8XWHQS01A5Y",
        "parent_id": null,
        "user_id": null
    }
}

Within HA, the values are nicely formatted:

Image

The display precision seems to have a default precision based on sensor type. It can also be configured to other formats:

Image

Integration Enhancement

  • Retrieve the display configuration from HA.
    For example after first connection and store in HA client session.
  • Reformat numeric values with the HA display precision as string value.
    HA already sends string values, which are passed to the remote. String values won't be re-formated anymore while displayed.

Luckily a WS command has been added to HA's WS AP to retrieve the display configuration: config/entity_registry/list_for_display. See home-assistant/core#87787 for more information.

Example request & response:

{"type":"config/entity_registry/list_for_display","id":27}
  {
    "id": 27,
    "type": "result",
    "success": true,
    "result": {
      "entity_categories": {
        "0": "config",
        "1": "diagnostic"
      },
      "entities": [
        {
          "ei": "sensor.shellyswitch25_98f4abf2c812_energy",
          "pl": "shelly",
          "lb": [],
          "di": "0236159c669305051a33ad6605a3142f",
          "hn": true,
          "en": "Energie",
          "dp": 2
        }
      ]
    }
  }
  • ei: entity id
  • pl: platform
  • hn: has entity name
  • en: entity name or original name
  • dp: display precision

Other possible fields retrieved from HA's source code:

    ("ai", "area_id", False),
    ("lb", "labels", True),
    ("di", "device_id", False),
    ("ic", "icon", False),
    ("tk", "translation_key", False),

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions