Item state as a number for a MQTT message

I`m trying to send an item state (Temperature) as a MQTT message.

But my script (javascript) is not working.

var TOutdoor = itemRegistry.getItem("Outdoorsensor_Temperatur").getState();
actions.get("mqtt", "mqtt:broker:VenusOS").publishMQTT("W/b827xxxxx/temperature/1/Temperature", '{"value": TOutdoor }');

It must probably be a number.
Can anyone help? Thanks!

This is working:

var TOutdoor = itemRegistry.getItem("Outdoorsensor_Temperatur").getState();
actions.get("mqtt", "mqtt:broker:VenusOS").publishMQTT("W/b827xxxxx/temperature/1/Temperature", '{"value": 20 }');

I am no Expert in JavaScript nor MQTT.

Do you know what is in your TOutdoor variable? Maybe you can print it to test.

I think there is a “getStateAs” Method you could try something like

var TOutdoor = itemRegistry.getItem("Outdoorsensor_Temperatur").getStateAs(DecimalType);

Which JavaScript? ECMAScript 5.1 I’m guessing.

There is a dictionary with all the Item’s current states. That’s more convenient than using the itemRegistry.

But your problem is you’ve not properly formatted the message you are publishing. You are literally publishing {"value": TOutdoor }. Obviously that’s nonsense to the receiver. You want the TOutdoor part to be the value of the the TOutdoor variable. To do that it needs to be outside the quotes.

var TOutdoor = items['Outdoorsensor_Temperatur'];
actions.get('mqtt', 'mqtt:broker:VenusOS').publishMQTT('W/XXXX/temperature/1/Temperature', '{"value": ' + TOutdoor + ' }');
1 Like

Thank you for the answer!
Yes it ist ECMAScript 5.1

Sadly it’s not working.

Message in MQTT fx:


Unexpected character (‘°’ (code 176)): was expecting comma to separate OBJECT entries
at [Source:; line: 1, column: 16]

Then your Item is a Number:Temperature Item and it’s carrying units of measurement. You’ll have to strip those off before publishing.

Is there a reason you are using the action instead of an MQTT Thing for this? MQTT channels can handle this conversion for you.

Ah ok, thank you.

There is no reason not to use a Thing. Maybe i try this.