Need help: MQTT publish temperature and humidity in JSON format

I recently installed a KNX touchscreen with integrated temp/humid sensor to replace my DIY temperature/humidity sensor.

The old sensor published its values over MQTT for Openhab, and to log them into influxdb.
The JSON looks like this:

{"temperature":16.31, "humidity": 54.12}

To keep the logging in influxdb, i now want openhab to publish the values from the touchscreen to MQTT. The JSON can be split up. node-red can handle this.

I attached the KNX channel and the MQTT channel to the temperature and humidity item, but it now produces an invalid JSON string:

{"temperature":21.5 °C}

How can i tell openhab to remove the °C ?

Can i somehow make a rule to post both temperature and humidity values to MQTT in one JSON?

Thanks in advance for your help!

In general, to get help for something like this you pretty much always should post what you’ve tried and your current configs. The less information you post, the more generic the responses will be.

You are not using openHAB persistence to store these values in InfluxDB?

  • Remove the Temperature dimension from the Item definition.
  • Set a command transformation on the MQTT Channel to remove the units.
  • Set a script profile on the MQTT Channel link to remove the unit in the Item to Channel directions.
  • Use a rule and the publishMQTT Action to publish the message.

Yes, trigger the rule on changes to either Item. Build the JSON and use the publishMQTT Action to publish the message.

I don’t use the influxdb persistence of openhab, because i set up the node-red and influxdb before i started using Openhab :slight_smile:

The configuration of my MQTT channels look like this:

- id: HvacLivingHumid
    channelTypeUID: mqtt:number
    label: Living Humidity
    configuration:
      unit: "%"
      transformationPatternOut:
        - JSONPATH:$.humidity
      formatBeforePublish: '{"humidity":%s}'
      commandTopic: /home/living/hvac
      step: 0.1
      stateTopic: /home/living/hvac
      transformationPattern:
        - JSONPATH:$.humidity
- id: HvacLivingTemp
    channelTypeUID: mqtt:number
    label: Living Temperature
    description: ""
    configuration:
      commandTopic: /home/living/hvac
      step: 0.1
      unit: °C
      formatBeforePublish: '{"temperature":%s}'
      stateTopic: /home/living/hvac
      transformationPattern:
        - JSONPATH:$.temperature

The KNX screen is configured like this:

  - id: KNX_Touchscreen_temp
    channelTypeUID: knx:number
    label: Living Temperature
    description: ""
    configuration:
      ga: 0/0/35
  - id: KNX_touchscreen_living_humidity
    channelTypeUID: knx:number
    label: Living Humidity
    description: ""
    configuration:
      ga: 0/0/41

These 2 channels are linked to the items living_temperature and living_humidity.

The KNX channel is configured as “default’“ profile, the MQTT channel is configured as “Follow”.

label: Living Humidity
type: Number
icon: humidity
groupNames:
  - HvacLiving
tags:
  - Humidity
  - Measurement

label: Living Temperature
type: Number
icon: temperature
groupNames:
  - HvacLiving
tags:
  - Temperature
  - Measurement


So to post the values, i unlink the MQTT channel from both items, and use a rule to post the values of the items to the channel when they update?

When this all works i would like to add buttons on the KNX touchscreen to also control the temperature setpoints and the HVAC modes, but i would like to keep the MQTT interface too.
Do i just remove the units of it all?

That’s one option.

That’s another one option.

I would click on the “show advanced” checkbox for the MQTT Channel and set the “outgoing transformation” to a transformation that strips off the unit and creates the JSON you need. The outgoing transformation apples before the message is published. You can chain transformations together so a REGEX passed to a Junja transformation to build the JOSN might be sufficient. Though I’d just do it all in a JS Script transformation.