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 }}"

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

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

Thanks

1 Like