Convert string to int for history graph presentation in lovelace

I have built the superhouse.tv Air Quality Sensor which produces the following MQTT output:

2c703
AE1P0 = 5
AE2P5 = 8
AE10P0 = 9
PPD0P3 = 1131
PPD0P5 = 293
PPD1P0 = 48
PPD2P5 = 4
PPD5P0 = 2
PPD10P0 = 2
SENSOR = {"PMS5003":{"CF1":5,"CF1":8,"CF1":9,"PM1":5,"PM2.5":8,"PM10":9,"PB0.3":1131,"PB0.5":1131,"PB1":48,"PB2.5":4,"PB5":2,"PB10":2}}

I have the following in configuration.yaml to extract the data:

sensor:
# AQS
  - platform: mqtt
    unique_id: AE1P0
    name: "PM 1.0"
    state_topic: "tele/12c703/AE1P0"
    qos: 1
  - platform: mqtt
    unique_id: AE2P5
    name: "PM 2.5"
    state_topic: "tele/12c703/AE2P5"
    qos: 1
  - platform: mqtt
    unique_id: AE10P0
    name: "PM 10.0"
    state_topic: "tele/12c703/AE10P0"
    qos: 1
  - platform: mqtt
    unique_id: PPD0P3
    name: "PPD 0.3"
    state_topic: "tele/12c703/PPD0P3"
    qos: 1
  - platform: mqtt
    unique_id: PPD0P5
    name: "PPD 0.5"
    state_topic: "tele/12c703/PPD0P5"
    qos: 1
  - platform: mqtt
    unique_id: PPD1P0
    name: "PPD 1.0"
    state_topic: "tele/12c703/PPD1P0"
    qos: 1
  - platform: mqtt
    unique_id: PPD2P5
    name: "PPD 2.5"
    state_topic: "tele/12c703/PPD2P5"
    qos: 1
  - platform: mqtt
    unique_id: PPD5P0
    name: "PPD 5.0"
    state_topic: "tele/12c703/PPD5P0"
    qos: 1
  - platform: mqtt
    unique_id: PPD10P0
    name: "PPD 10.0"
    state_topic: "tele/12c703/PPD10P0"
    qos: 1

Which presents the following in Lovelace:
image
PPT

However this data is of type string and does not present correctly in a history graph:
image

So I tried to extract the data with the following:

  - platform: mqtt
    name: "PM1"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PM1 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PM2.5"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PM2.5 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PM10"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PM10 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB0_3"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB0.3 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB0_5"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB0.5 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB1"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB1 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB2_5"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB2.5 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB5"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB5 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB10"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB10 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"

However I’m no java programmer and the period in some of the MQTT variable names causes me trouble. For example the period before the 5 in this value_json.PMS5003.PB2.5 makes PB2.5 unknown. Also I’m not sure the int command in the value_template is creating a variable of type integer properly as I still see a colour graph rather than line graph when I plot these new values in Lovelace.

What is the best way to extract this data and present as integers? Should I pass the MQTT messages to NodeRed and write a function that passes them back as integers in a new MQTT message?

Thanks
Rob

Try using bracket notation instead for any key that contains a period:

 value_template: "{{ value_json.PMS5003['PB2.5'] | int }}" 
or 
 value_template: "{{ value_json['PMS5003']['PB2.5'] | int }}"

ok thanks.

No, doesn’t seem to work.

  - platform: mqtt
    name: "PB0_3"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json[PMS5003][PB0.3] | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB0_5"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json[PMS5003][PB0.5] | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
  - platform: mqtt
    name: "PB1"
    state_topic: "tele/12c703/SENSOR"
    value_template: "{{ value_json.PMS5003.PB1 | int }}"
    json_attributes_topic: "tele/12c703/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"

Thanks
Rob

Try to add

unit_of_measurement

You forgot to enlose the keys in brackets in single qoutes:

value_template: "{{ value_json.['PMS5003']['PB0.5'] | int }}" 

There’s a dot (period) that shouldn’t be there. Use:

value_template: "{{ value_json['PMS5003']['PB0.5'] | int }}"

1 Like

Thanks, I missed that while editing Robert’s reply.

Fantastic, that worked and the unit_of_measurement created the line graph.

Thanks

1 Like

I came across this post a little late! But having the same issue it was most helpful, here is my mqtt.yaml entry for anyone else who finds it:

I’m not sure what the CF value stands for still…


sensor:
    ### SUPERHOUSE AQS SENSOR (LOUNGE)  ###
    # https://www.superhouse.tv/38-diy-air-quality-sensor-part-1-basic-model/
    #CF
  - name: "Lounge AQS CF1"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['CF1'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: ""
  - name: "Lounge AQS CF2.5"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['CF2.5'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: ""
  - name: "Lounge AQS CF10"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['CF10'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: ""
    # The “PMx” values are micrograms per cubic meter.
  - name: "Lounge AQS PM1"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PM1'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ug/m3"
  - name: "Lounge AQS PM2.5"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PM2.5'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ug/m3"
  - name: "Lounge AQS PM10"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PM10'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ug/m3"
    # The “PBx” values are particles per deciliter.
  - name: "Lounge AQS PB0.3"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB0.3'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"    
  - name: "Lounge AQS PB0.5"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB0.5'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"
  - name: "Lounge AQS PB1"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB1'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"
  - name: "Lounge AQS PB2.5"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB2.5'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"
  - name: "Lounge AQS PB5"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB5'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"
  - name: "Lounge AQS PB10"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['PB10'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: "ppd"
    # UK Air Quality Index
  - name: "Lounge AQS UKAQI"
    state_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    value_template: "{{ value_json['PMS5003']['UKAQI'] | int }}"
    json_attributes_topic: "house/lounge/pms5003/edbbe9/SENSOR"
    json_attributes_template: "{{ value_json.PMS5003 | tojson }}"
    unit_of_measurement: ""

This is what chatgpt says

In air quality measurement, CF stands for Correction Factor. It is used to adjust raw data readings from particle sensors to improve accuracy. Some particle sensors provide readings that are labeled with a “CF” to indicate that the values have been corrected or adjusted based on specific environmental conditions or sensor calibration.

For example, CF=1 may refer to factory calibration for certain standard particles, while other CF values might be used to adjust the sensor readings for different types of particulate matter (PM) or environmental factors. This helps ensure that the sensor’s readings align more closely with actual air quality levels.

1 Like