MQTT: How to code a sensor?

Given this in MQTT…

How would I configure the MQTT sensor in HA?
I have been trying for a while now and currently have this:

  - platform: mqtt
    name: "Total Car Charger Energy"
    state_topic: "energy/sensor/car_charger_energy"
    value_template: '{{ value }}'
    json_attributes_topic: "energy/sensor/car_charger_energy/history"
    json_attributes_template: >
     {  "Jan": {{value_json.Jan}},
        "Feb": {{value_json.Feb}},
        "Mar": {{value_json.Mar}},
        "Apr": {{value_json.Apr}},
        "May": {{value_json.May}},
        "Jun": {{value_json.Jun}},
        "Jul": {{value_json.Jul}},
        "Aug": {{value_json.Aug}},
        "Sep": {{value_json.Sep}},
        "Oct": {{value_json.Oct}},
        "Nov": {{value_json.Nov}},
        "Dec": {{value_json.Dec}} }

but all I get is…

Try this:

  - platform: mqtt
    name: "Total Car Charger Energy"
    state_topic: "energy/sensor/car_charger_energy"
    json_attributes_topic: "energy/sensor/car_charger_energy/history"

After modifying its configuration, execute Check Configuration and ensure it passes then execute Reload Manually Configured MQTT Entities (or just restart Home Assistant).

It should, at the very least, report the car charger’s energy value whenever it’s published to the MQTT broker by the car charger. If it fails to do that then I suggest you confirm the MQTT integration is properly installed and configured.

Thank you! I actually had almost your code as I currently have the json_attributes_template commented out. I will now remove the value_template" '{{ value }}'
My mistake was in assuming it would populate with the current values in MQTT but just now discovered that I need to publish something to MQTT and then HA is updated.

  - platform: mqtt
    name: "Total Car Charger Energy"
    force_update: true
    state_topic: "energy/sensor/car_charger_energy"
    value_template: '{{ value }}'
    json_attributes_topic: "energy/sensor/car_charger_energy/history"
#    json_attributes_template: >
#     {  "Jan": {{value_json.Jan}},
#        "Feb": {{value_json.Feb}},
#        "Mar": {{value_json.Mar}},
#        "Apr": {{value_json.Apr}},
#        "May": {{value_json.May}},
#        "Jun": {{value_json.Jun}},
#        "Jul": {{value_json.Jul}},
#        "Aug": {{value_json.Aug}},
#        "Sep": {{value_json.Sep}},
#        "Oct": {{value_json.Oct}},
#        "Nov": {{value_json.Nov}},
#        "Dec": {{value_json.Dec}} }

Is there some configuration I can do to retain the values in the HA sensor without requiring an MQTT re-publish?

The MQTT Sensor simply reports the payload that was published to the topic it’s monitoring.

If whatever device that’s publishing the payload does so as a “retained message” then the MQTT Broker stores the payload. The advantage of this is that whenever Home Assistant is restarted, the moment it re-connects to the MQTT Broker, it receives the stored payload.

If whatever device that’s publishing the payload does not specify it as a “retained message” then the MQTT Broker does not store the payload. Whenever Home Assistant is restarted, the moment it re-connects to the MQTT Broker, it receives no payload (and the corresponding sensor’s value will be unknown). It will only receive a payload when the device publishes it.

Got it! I am the one (or rather HA is) that is publishing to the sensor. I’ve added retain: true to the automation.

Part 2:
I now want this automation that’s triggered by sensor.car_charging_monthly_energy_cost at 23:59 on the last day of the month to also write the value to the corresponding month in the history attributes but that syntax has me baffled at the moment!

alias: Car Charging Monthly Energy Cost - Update
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.car_charging_monthly_energy_cost
condition: []
action:
  - service: mqtt.publish
    data:
      topic: energy/sensor/car_charger_energy
      payload_template: '{{ states("sensor.monthly_energy_cost_car_charger") }}'
      retain: true
mode: single
  - trigger:
    - platform: template
      value_template: "{{ (now() + timedelta(days=1)).day == 1 and now().hour == 23 and now().minute == 59 }}"
    sensor:
      - name: "Car Charging Monthly Energy Cost"
        state: '{{ states("sensor.monthly_energy_cost_car_charger") }}'

I don’t think this is quite right as I need to update the json element…

service: mqtt.publish
data:
  topic: energy/sensor/car_charger_energy/history
  payload_template: '{{ as_timestamp(now()) | timestamp_custom('%b') }}:{{ states("sensor.monthly_energy_cost_car_charger") }}'

Does this look correct?

service: mqtt.publish
data:
  topic: energy/sensor/car_charger_energy/history
  payload: >-
    {
      "{{ as_timestamp(now()) | timestamp_custom('%b') }}": {{ states("sensor.monthly_energy_cost_car_charger") }}
    }

You should split “Part 2” into a separate forum topic.

Will do! Thanks.