How do I create an object to pass to alexa.remote2

I am trying to control a device and create a generic function that can take a topic with the device name and a payload with ON or OFF but I can't seem to figure out the code for this. I am trying to pass this to an alex.remote2 custom routine node set to custom and msg.payload as the data input.

I have the following in the function:

var state = msg.payload.toUpperCase();
var device = msg.topic;
msg = {};

if(state==="ON")
{
    msg.payload={type: "smarthome", payload: {entity: [device], action: "turnOn"}};
    node.status({fill:"green",shape:"ring",text:"ON"});
}
else
{
    msg.payload={type: "smarthome", payload: {entity: [device], action: "turnOff"}};
    node.status({fill:"red",shape:"ring",text:"OFF"});
}
return msg;

I get the following in the debug output:

invalid sequence node: "{"type":"smarthome","payload":{"entity":["Front room fan"],"action":"turnOff"}}" expected layout: "{"entity":"","action":""}"

This is what I output from the function:

{"payload":{"type":"smarthome","payload":{"entity":["Front room fan"],"action":"turnOn"}},"_msgid":"c028a4cd.8e16d8"}

Any ideas where I am going wrong?

You are sending ["Front room fan"] should this be "Front room fan" ?

That was it. I had surrounded the variable name with but it was not needed. The node now recognises the commands. Thanks for pointing it out.

1 Like

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