[MQTT] Discovery msg and attributes problem

Hello.

Manually I can create customize attribute related to my sensor (sensor has been created by MQTT discovery msg) in configuration.yaml like below:

homeassistant:
  customize:
    sensor.heatmeter_kamheat_co_4_power_kw: ##<--- you forgot the : here
      notes: my some notes here

And next I can found the attribute eg. here:

or here:

Because I have a lots MQTT devices and much more entities then I want to achieve similar solution (assign attributes to entities) but exploited MQTT discovery msg. For this I found below documentation with section JSON_ATTRIBUTES_TOPIC_CONFIGURATION:

To do this I create below discovery MQTT msg (show only config payload JSON part below)

{
	"json_attributes_topic": "homeassistant/sensor/CWU_2/state",
	"json_attributes_template": "{{ value_json.meter }}",
	"name": "historical_total_m3",
	"state_topic": "homeassistant/sensor/CWU_2/state",
	"unique_id": "historical_total_m3_00348454_aptmbusna.xmq",
	"expire_after": "370",
	"value_template": "{{ value_json.historical_total_m3 }}",
	"unit_of_measurement": "m³",
	"state_class": "TOTAL_INCREASING",
	"device_class": "VOLUME",
	"device": {
		"serial_number": "00348454",
		"hw_version": "n/a",
		"manufacturer": "Apator",
		"identifiers": "00348454",
		"name": " CWU_2"
	},
	"origin": {
		"name": "wmbusmeters",
		"support_url": "https://github.com/wmbusmeters/wmbusmeters/",
		"sw_version": "wmbusmeters: 1.17.1-29-g3af7a4c 3af7a4c8a881c10498f1e14d950d4e502ab2fa7e"
	}
}

However cannot found attribute assign to the sensor:

My question is can be a problem that json_attributes_topic and state_topic are the same (I have also separated discovery msg for meter but sensor is disable in HA) ? How to resolve the problem? Choose meter for a test only as mentioned related entity is disabled in HA.
Additional in Home Assistant Core logs no errors found related to sensor historical_total_m3

Below example topic homeassistant/sensor/CWU_2/state JSON

media: water
meter: aptmbusna
name: CWU_2
id: 00348454.M=APA.V=15.T=07
current_volume_flow_lh: 0
historical_meter_date: '2004-07-01'
historical_total_m3: 0.056
meter_datetime: 2024-10-24 11:27
operating_battery_d: 1696
total_volume_m3: 0.47
events_active_type: OK
events_permanent_type: >-
  P_CLOCK_HAND_ERROR P_DETECTION_WITHOUT_CHANGES P_DETECTOR_ERROR
  P_M-BUS_DISCONNECTED P_PROCESSOR_RESET_DETECTED P_STRONG_LIGHT_DETECTED
fabrication_no: '00348454'
optical_diagnostic: '00010404'
oscilator_diagnostic: '0000'
power_supply_diagnostic: B40CBB
timestamp: '2024-10-24T09:27:03Z'
device: SERIAL_1
rssi_dbm: 0

That’s not valid JSON.

If it’s like this in your topic, value_json won’t work.
If it’s an actual JSON, just post it.

I’m sorry, copy from this:

Correct JSON is:

{
    "media":"water",
    "meter":"aptmbusna",
    "name":"",
    "id":"00348454",
    "current_volume_flow_lh":0,
    "historical_meter_date":"2004-07-01",
    "historical_total_m3":0.056,
    "meter_datetime":"2024-10-24 13:01",
    "operating_battery_d":1696,
    "total_volume_m3":0.47,
    "events_active_type":"OK",
    "events_permanent_type":"P_CLOCK_HAND_ERROR P_DETECTION_WITHOUT_CHANGES P_DETECTOR_ERROR P_M-BUS_DISCONNECTED P_PROCESSOR_RESET_DETECTED P_STRONG_LIGHT_DETECTED",
    "fabrication_no":"00348454",
    "optical_diagnostic":"00000404",
    "oscilator_diagnostic":"0000",
    "power_supply_diagnostic":"B40CBB",
    "timestamp":"2024-10-24T11:00:29Z"
}

See MQTT Sensor - Home Assistant

	"json_attributes_template": '{{ { "meter": value_json.meter } | tojson }}'

Thank you, I saw this but wrongly assume it was for nested JSON (as shown in documentation) what I don’t have in my data but it works:

BTW - I’m wondering can I send in discovery static constant string that will be displayed in attribute always. So for attribute no need JSON topic msg, only once send all constant attributes what I need in discovery msg?

You can use the same in your discovery topic, and you can replace value_json... by constants.

Thank you very much, working as you describe.