# Converting a message string to a message object

**URL:** <https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256>\
**Category:** General\
**Created:** [9 December 2020 16:28 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256 "2020-12-09T16:28:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zero](https://avatars.discourse-cdn.com/v4/letter/z/a88e57/32.png) [@zero](https://discourse.nodered.org/u/zero)\
**Post date:** [9 December 2020 16:28 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/1 "2020-12-09T16:28:17Z")

</div>

Hi,

i´m having some trouble in converting a message string to a message object. In my case, i would like to parse my data into my influxdb. This works fine for my co2 sensor which is providing its data as payload object like this:

![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/8806adbceb06ad8dd2145b52623d02deecc9ebd2.png)

My problem is this output from my temperature sensor. I can´t find a way to convert the output to a message object like the other sensor.

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a2caebf0dd7b5f84e2c4115eaa9b6c20d63a062.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 December 2020 17:06 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/2 "2020-12-09T17:06:16Z")

</div>

> [@zero](#):
>
> My problem is this output from my temperature sensor

This is easy enough to parse but first, are you in control of this format (e.g. did you design the thing that sends this data & can you change it)?

---

<div class="post-metadata">

**Author:** ![zero](https://avatars.discourse-cdn.com/v4/letter/z/a88e57/32.png) [@zero](https://discourse.nodered.org/u/zero)\
**Post date:** [9 December 2020 17:08 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/3 "2020-12-09T17:08:44Z")

</div>

I´m using some example code i found on github:

```auto
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This program reads out IST HYT sensors(HYT-221, HYT-271, HYT-939)
#
# License: Public Domain/CC0
# Original source: https://github.com/joppiesaus/python-hyt/
#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Humidity:
# 0x0000 - 0x3FFFF hex
# 0 - 16383 dec
# 0 - 100 % RH
#
# Temperature:
# 0x0000 - 0x3FFF hex
# 0 - 16383 dec
# -40 - 125 Â°C
# 233.15 - 298.15 Â°K
# ??? - ??? Â°F
#
# | byte 0 | byte 1 | byte 2 | byte 3 |  
# |---------------------|---------------------|
# | Humidity | Temperature |
# |---------------------|---------------------|
# | 2 bit | 14 bit | 14 bit | 2 bit |
# |-------|-------------|-------------|-------|
# | state | data | data | dummy |
# |
# +-----------------------+
# | bit 0 | bit 1 |
# | CMode bit | stale bit |
# +-----------------------+
#
# CMode bit: if 1 - sensor is in "command mode"
# Stale bit: if 1 - no new value has been created since the last reading
#
# RH = (100 / (2^14 - 1)) * RHraw
# T = (165 / (2^14 - 1)) * Traw - 40
#
# crappy ascii picture from top to see pinout:
# ---
# |~ ~|
# | O |
# |___|
# ||||
# |||+-SCL
# ||+--VDD
# |+---GND
# +----SDA
#
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import time
import smbus

addr = 0x28 # default address for hyt sensors
delay = 50.0 / 1000.0 # 50-60 ms delay. Without delay, it doesn't work.
# I should test that delay more.
bus = smbus.SMBus(1) # use /dev/i2c1

def read():
	bus.write_byte(addr, 0x00) # send some stuff
	time.sleep(delay) # wait a bit
	reading = bus.read_i2c_block_data(addr, 0x00, 4) # read the bytes
	# Mask the first two bits
	humidity = ((reading[0] & 0x3F) * 0x100 + reading[1]) * (100.0 / 16383.0)
	# Mask the last two bits, shift 2 bits to the right
	temperature = 165.0 / 16383.0 * ((reading[2] * 0x100 + (reading[3] & 0xFC)) >> 2) - 40
	return humidity, temperature

def readandprint():
	rh, t = read()
	print ("Humidity:", rh)
	print ("Temperature:", t)

if __name__ == " __main__":
	readandprint()
	

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 December 2020 17:18 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/4 "2020-12-09T17:18:48Z")

</div>

So, change it to output JSON & then you can simply access the object values like you do in the first example.

e.g...

```python
def readandprint():
	rh, t = read()
	print '{"Humidity":', rh, ', "Temperature":', t, '}'

```

---

<div class="post-metadata">

**Author:** ![zero](https://avatars.discourse-cdn.com/v4/letter/z/a88e57/32.png) [@zero](https://discourse.nodered.org/u/zero)\
**Post date:** [9 December 2020 17:29 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/5 "2020-12-09T17:29:02Z")

</div>

You saved my Day, thanks a lot 😀

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [23 December 2020 17:29 UTC](https://discourse.nodered.org/t/converting-a-message-string-to-a-message-object/37256/6 "2020-12-23T17:29:05Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
