MQTT sensor state changes not being logged to logbook

I have two MQTT sensors, closest_aircraft and visible aircraft which are grouped in the same device but which are behaving differently with regard to the logbook. There are frequent state changes to both, but only changes to visible aircraft are being logged in the logbook.

Any suggestions for how to ensure that both are logged? Could this be an issue of template construction?

Here are the configurations:

sensor:
  - name: "Closest Aircraft"
    state_topic: "flights/closest_aircraft"
    value_template: >
      {% set first_key = value_json.keys() | list | first %}
      {{ value_json[first_key]['distance_value'] | float }}
    unit_of_measurement: "mi"
    device_class: distance
    unique_id: "flights_closest_aircraft"
    icon: "mdi:airplane"
    json_attributes_topic: "flights/closest_aircraft"
    json_attributes_template: >
      {% set first_key = value_json.keys() | list | first %}
      {{ value_json[first_key] | tojson }}
    device:
      identifiers: ["flightrack_device"]  # Shared ID for grouping
      manufacturer: "RS"
      model: "Flights"
      name: "Flighttrack"
  - name: "Visible Aircraft"
    state_topic: "flights/visible"
    value_template: "{{ value_json['visible_aircraft'] }}"
    icon: "mdi:radar"
    unique_id: "flights_visible_aircraft"
    json_attributes_topic: "flights/visible"
    json_attributes_template: "{{ value_json | tojson }}"
    device:
      identifiers: ["flightrack_device"]
      manufacturer: "RS"
      model: "Flights"
      name: "Flighttrack"

Here’s what the device view in the UI looks like:

And here is history showing the frequent state changes;

TLDR: Logbook updates if no unit of measurement is specified in MQTT sensor configuration.

Any advice much appreciated. I’d like the logbook to update with any state change of either entity.

I’ve changed the configuration to MQTT device discovery using the new method introduced in 2024.11. Along the way, I discovered:

  • If a unit of measurement is defined for the state of a sensor, the logbook does not update when the state changes.
  • If a unit of measurement is not defined for the state of a sensor in the discovery configuration, the logbook does not update when the state changes.
  • History is tracked identically whether unit of measurement defined or not.

Unit of measurement not defined; Logbook updating:

{
  "device": {
    "identifiers": "erqY6E311cb79pVKv2vugQ",
    "name": "Flights",
    "manufacturer": "RS",
    "model": "Flights",
    "model_id": "",
    "sw_version": "beta",
    "configuration_url": "http://0.0.0.0:47474/"
  },
  "origin": {
    "name":"Flights",
    "sw": "beta",
    "support_url": "https://github.com/ronschaeffer/flights"
  },
  "components": {
    "closest_aircraft": {
      "platform": "sensor",
      "name": "Closest Aircraft",
      "icon": "mdi:airplane",
      "state_topic": "dev/flights/closest",
      "value_template": "{% set first_key = value_json.keys() | list | first %}{{ value_json[first_key]['distance_value'] | float }}",
      "json_attributes_template": "{% set first_key = value_json.keys() | list | first %}{{ value_json[first_key] | tojson }}",
      "unique_id":"closest_aircraft_mG5KYdK6AhfHPmssHNAMvx"
    },
    "visible_aircraft": {
      "platform": "sensor",
      "value_template": "{{ value_json['visible_aircraft'] }}",
      "name": "Visible Aircraft",
      "icon": "mdi:radar",
      "state_topic": "dev/flights/visible",
      "json_attributes_template": "{{ value_json | tojson }}",
      "unique_id":"visible_aircraft_t93WktQ8yiw7h25QzeNwc6W"
    }
  }
}

Unit of measurement defined; Logbook not updating:

{
  "device": {
    "identifiers": "erqY6E311cb79pVKv2vugQ",
    "name": "Flights",
    "manufacturer": "RS",
    "model": "Flights",
    "model_id": "",
    "sw_version": "beta",
    "hw_version": "",
    "serial_number": "",
    "configuration_url": "http://0.0.0.0:47474/"
  },
  "origin": {
    "name":"Flights",
    "sw": "beta",
    "support_url": "https://github.com/ronschaeffer/flights"
  },
  "components": {
    "closest_aircraft": {
      "platform": "sensor",
      "unit_of_measurement": "mi",
      "name": "Closest Aircraft",
      "icon": "mdi:airplane",
      "state_topic": "dev/flights/closest",
      "value_template": "{% set first_key = value_json.keys() | list | first %}{{ value_json[first_key]['distance_value'] | float }}",
      "json_attributes_template": "{% set first_key = value_json.keys() | list | first %}{{ value_json[first_key] | tojson }}",
      "unique_id":"closest_aircraft_mG5KYdK6AhfHPmssHNAMvx"
    },
    "visible_aircraft": {
      "platform": "sensor",
      "unit_of_measurement": "planes",
      "value_template": "{{ value_json['visible_aircraft'] }}",
      "name": "Visible Aircraft",
      "icon": "mdi:radar",
      "state_topic": "dev/flights/visible",
      "json_attributes_template": "{{ value_json | tojson }}",
      "unique_id":"visible_aircraft_t93WktQ8yiw7h25QzeNwc6W"
    }
  }
}