No history for my template sensor over MQTT

I’m using ESPAltherma to monitor my heat pump. My ESP device publish a topic over MQTT, which I then use in Home Assistant. Following the guidelines of the ESPAltherma documentation, I’ve integrated the data into Home Assistant by adding a template. Everything works as expected, except for some entities which produce no history. These entities all produce a string as value, for example the “Operation Mode” produces values like “Heating”, “Fan Only”, “DHW”…

Here I attach some useful material

The relevant parts of my configuration.yaml:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

template:
  - sensor:
# ESPAltherma
# This sensor has no history
    - name: "Operation mode"
      unique_id: "29025128-c5f9-4228-ad26-3e91f2ac333d"
      state: "{{ state_attr('sensor.althermasensors','Operation Mode') }}"
      state_class: measurement

# This sensor has history
    - name: "Outdoor air temperature"
      unique_id: "19e9bc5b-afba-4b97-9096-e5b070c47a96"
      state: "{{ state_attr('sensor.althermasensors','Outdoor air temp.') }}"
      unit_of_measurement: "°C"
      state_class: measurement
      device_class: temperature

# This sensor has history
    - name: "INV primary current"
      unique_id: "95cba8c0-f98d-4c28-bdb1-417ed95441ad"
      state: "{{ state_attr('sensor.althermasensors','INV primary current (A)') }}"
      unit_of_measurement: "A"
      state_class: measurement
      device_class: current

The raw JSON as received via MQTT

{
    "Sensor Data Qty": 10,
    "INV compressor Qty": 1,
    "STD compressor Qty": 0,
    "Fan Data Qty": 2,
    "Expansion Valve Data Qty": 2,
    "4 Way Valve Data Qty": 1,
    "Crank Case Heater Qty": 1,
    "Solenoid valve Qty": 2,
    "Max. connectable indoor units": 4,
    "Connected Indoor Unit Qty": 1,
    "O/U MPU ID (xx)": 71,
    "O/U MPU ID (yy)": 2,
    "O/U capacity (kW)": 8,
    "Operation Mode": "Fan Only",
    "Thermostat ON/OFF": "OFF",
    "Restart standby": "OFF",
    "Startup Control": "OFF",
    "Defrost Operation": "OFF",
    "Oil Return Operation": "OFF",
    "Pressure equalizing operation": "OFF",
    "Demand Signal": "OFF",
    "Low noise control": "OFF",
    "Error type": "Normal",
    "Error Code": " 0",
    "Target Evap. Temp.": "---",
    "Target Cond. Temp.": 20,
    "Discharge Temp. Drop": "OFF",
    "Comp. INV Current Drop": "OFF",
    "HP Drop Control": "OFF",
    "LP Drop Control": "OFF",
    "Fin Temp. Drop Control": "OFF",
    "Other Drop Control": "OFF",
    "Not in use": "Conv 311 not avail.",
    "Outdoor air temp.": 13.6,
    "O/U Heat Exch. Temp.": 13,
    "Discharge pipe temp.": 19.6,
    "Suction pipe temp.": 20.2,
    "O/U Heat Exch. Mid-temp.": 13.2,
    "Liquid pipe temp.": 16.3,
    "INV fin temp.": 17.2,
    "Pressure": 11,
    "INV primary current (A)": 0,
    "INV secondary current (A)": 0,
    "Voltage (V)": "-26880",
    "Brine Flow Switch": "OFF",
    "Brine inlet temp.": 0,
    "Brine outlet temp.": 0,
    "Refrig. temp. evap. In": 0,
    "Refrig. temp. evap.Out": 0,
    "INV frequency (rps)": 0,
    "Fan 1 (10 rpm)": 0,
    "Expansion valve 1 (pls)": 0,
    "Expansion valve 2 (pls)": 0,
    "4 Way Valve 1": "ON",
    "4 Way Valve 2": "OFF",
    "4 Way Valve 3": "OFF",
    "4 Way Valve 4": "OFF",
    "4 Way Valve 5": "OFF",
    "Crank case heater 1": "ON",
    "Crank case heater 2": "OFF",
    "Crank case heater 3": "OFF",
    "Crank case heater 4": "OFF",
    "Crank case heater 5": "OFF",
    "Solenoid Valve 1": "OFF",
    "Solenoid Valve 2": "OFF",
    "Solenoid Valve 3": "OFF",
    "Solenoid Valve 4": "OFF",
    "Solenoid Valve 5": "OFF",
    "Indoor Unit Address": 0,
    "I/U operation mode": "Stop",
    "Freeze Protection": "OFF",
    "Silent Mode": "OFF",
    "Freeze Protection for water piping": "OFF",
    "DHW setpoint": 49,
    "LW setpoint (main)": 15,
    "Water flow switch": "OFF",
    "Thermal protector (Q1L) BUH": "OFF",
    "Thermal protector BSH": "ON",
    "Benefit kWh rate power supply": "OFF",
    "Bivalent Operation": "OFF",
    "2way valve(On:Heat_Off:Cool)": "ON",
    "3way valve(On:DHW_Off:Space)": "OFF",
    "BSH": "OFF",
    "BUH Step1": "OFF",
    "BUH Step2": "OFF",
    "BPH": "OFF",
    "Water pump operation": "OFF",
    "Data Enable/Disable": "ON",
    "Outlet Water Heat Exch. Temp. (R1T)": 39.7,
    "Outlet Water BUH Temp. (R2T)": 40.9,
    "Refrig. Temp. liquid side (R3T)": 32.5,
    "Inlet water temp.(R4T)": 37.6,
    "DHW tank temp. (R5T)": 48.1,
    "RT Temp.": 0,
    "Outdoor Ambient or Ext Sensor": 0.5,
    "M5VIN": "4.87V",
    "M5AmpIn": "84.375mA",
    "M5BatV": "4.15V",
    "M5BatCur": "0mA",
    "M5BatPwr": "0mW",
    "WifiRSSI": "-68dBm",
    "FreeMem": "212540"
}

The view of the “Operation mode” (note the “No statistic found”) and of the “Outdoor air temperature”, which works as expected

What is happening? What am I missing to save the history?

Thank you all!

Welcome to HA! :slightly_smiling_face:
Did you try it without the state_class?

Yes, that was it! Thank you very much