Newbie help, nodejs put to change state [solved]

Hi All,
I am pretty new to nodejs and openhab and I can’t seem to get it to change the state of a switch. I am sure I am doing something stupid and wrong, please save me.

var http = require('http');

function dumpError(err) {
  if (typeof err === 'object') {
    if (err.message) {
        console.log('\nMessage: ' + err.message);
    if (err.stack) {
  } else {
    console.log('dumpError :: argument is not an object');

function setStateZWaveSwitch(itemName, state) {
    return new Promise(function(resolve, reject) {
        try {
			"host": '',
			"method": 'PUT',
			"port": 8080,
            "path": `/rest/items/${itemName}/state`,
            "header": { "Content-Type": "text/plain" },
            "body": `${state}`

            }, function(response) {
                var buf = '';
                //another chunk of data has been recieved, so append
                response.on('data', function(chunk) {
                    buf += chunk;
                //the whole response has been recieved, so we just print it out here
                response.on('end', function() {
            }).on('error', function(e) {
        } catch(e) {

setStateZWaveSwitch('zwave_device_3afa7b64_node2_switch_binary', 'OFF').then(function(res) {
}, function(e) {

it responds with:

“error”: {
“message”: "State could not be parsed: ",
“http-code”: 400

I don’t know Node.js so I can’t tell you what is wrong or how to fix it, but the error message indicates that the HTTP PUT is not parsable.

400 indicates that the “Item state is null” so I would double check that your State is being passed and encoded correctly.

Thanks for your input, I can however do this:

curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “ON” “

and it works

I am trying to do that exact same thing but in nodejs. Anyone have any ideas?

You might have better luck on a node.js forum. The percentage of Node users on this forum is fairly low.

If I were debugging this, I would set up a pcap and compare the raw http messages for both to see what is different.

I figured it out, posting correct function call just in the event it helps another.

function setZwaveItemState(itemName, state) {
    return new Promise(function(resolve, reject) {
        try {
            if (itemName === undefined)
                reject(Error('setZwaveItemState: Passed itemName is undefined'));

            if (state === undefined)
                reject(Error('setZwaveItemState: Passed state is undefined'));

                method: 'PUT',
                uri: `${itemName}/state`,
                header: { "Content-type": "text/plain" },
                body: state
            function (error, response, body) {
                if (error) {
                } else if (response.statusCode == 400) {
                    reject(Error(`setZwaveItemState: Item not found ${itemName}`));
                } else if (response.statusCode == 404) {
                    reject(Error(`setZwaveItemState: Passed state not parseable ${state}`));
        } catch(e) {

thanks also to @rlkoshak

  • Brian