MQTT Json parsing wrong values

Not sure if this is a bug or I am just not seeing where the typo is…

Through MQTT from my device to hass I am sending:

{"tCels":"22.00","tFahr":"71.60","humidity":"53.00","hIndex":"21.64"}

configuration.yaml:

sensor:
- platform: mqtt
  state_topic: "office/multi1"
  name: "Office TemperatureC"
  device_class: temperature
  unit_of_measurement: "°C"
  value_template: "{{ value_json['tCels'] | round(1) }}"

- platform: mqtt
  state_topic: "office/multi1"
  name: "Office TemperatureF"
  device_class: temperature
  unit_of_measurement: "°F"
  value_template: "{{ value_json['tFahr'] | round(1) }}"

- platform: mqtt
  name: 'Office Humidity'
  state_topic: 'office/multi1'
  device_class: humidity
  unit_of_measurement: '%'
  value_template: '{{ value_json.humidity }}'

- platform: mqtt
  name: 'Office Heat Index'
  state_topic: 'office/multi1'
  value_template: '{{ value_json.hIndex }}'

But, the states update and no matter which order I put the Fahrenheit and Celsius, they both end up being Fahrenheit.

sensor.office_heat_index	21.64	friendly_name: Office Heat Index
sensor.office_humidity		53.00	friendly_name: Office Humidity
									unit_of_measurement: %
sensor.office_temperaturec	71.6	friendly_name: Office TemperatureC
									unit_of_measurement: °F
sensor.office_temperaturef	71.6	friendly_name: Office TemperatureF
									unit_of_measurement: °F

Can anyone help decipher where the issue is?

Not sure, but I believe the system automatically converts temperature values to the unit_system you define in your configuration.yaml - i.e., metric or imperial.

I couldn’t see anything wrong, so I tried this in a test machine and both temperature sensors were filled in with the centigrade values. I am on metric measurements so pnbruckner may be on to something.

I was able to get the correct values inserted by removing the unit_of measurement: line from both of them, but this hardly seems a solution.

It looks like a bug to me.

Edit:

If I send in

$ mosquitto_pub -t office/multi1 -m '{"tCels":"22.00","tFahr":"100","humidity":"53.00","hIndex":"21.64"}'

I get 37.8 for the Fahrenheit reading, so it looks like pnbruckner is correct and HA is converting the value to centigrade after it has been received.

That one looks like a cross-posting

https://github.com/home-assistant/home-assistant/issues/14425

See MQTT Json data parse with temperature · Issue #14425 · home-assistant/core · GitHub

This isn’t a solution, it’s an excuse. If the user is specifically transmitting data, why auto-convert it without their knowledge. Or, at least include a flag within sensor function to disable this from happening.

You can also create a PR if you don’t like something.

I tend to agree with @fabaff on this one. If you want to have it ignore the system unit, just make a custom component. 99.9% of the user base is going to want ºC or ºF, not both. I’d like anyone to find me a weather or thermostat that allows users to configure temperatures on an individual basis. They are all ‘one ºunit to control them all’. That’s not an excuse to make home assistant that way but it begs the question, why do you want to have both units?

Simply user customization. If the user is specifying ºC then show them ºC, and same for ºF. If unspecific, then default to system settings.

In my specific case I am using it for validation, developing of technology for a broader audience. I am working on a code base for MQTT sensors that which code can be shared with the community. Allow individuals to use my code as a plug-in-play but not limit their temperature choice. I cannot validate that I am receiving the other temperature data correctly because HA is “auto-correcting” and representing values I didn’t specifically call out.

If this auto-correction is going to be standard for all instances, please note this in the mqtt.sensor documentation.

I have changed “ºC” to just “º” for my purposes, it presents my data accurately however depicting degrees rotation.

I don’t think this is limited to just mqtt sensors. From what I gather, its all units everywhere determined by the optional parameter in the basic home assistant setup: unit_system.

For testing purposes you could always use debug statements.