With the code below, I succeeded to realize an own function node for a tp-link HS100 plug drectly with the tplink api. In this way, I can get and set the status of the plug and have no TCP Timeout-Error-Messages anymore. In the API-documentation, the access on the plug-object was described as ".relayState ⇒ boolean".
But now I want to realize the same thing for a tp-link HS110 plug meter (or a LB120 bulb) where the structure of the object is more complicated as I have to access (sub-)obects in the plug meter object.
In the API-documentation, this is described as e.g. ".getPowerState([sendOptions]) ⇒ Promise.<boolean, ResponseError>".
I tried a lot of days but didnt get it working.
Could anybody give me the code or an easy understandible code example on how to realize such a function call?
Help would be great.
const { Client } = global.get('My_tplink_API');
const client = new Client();
var relay_state = 0;
const plug = client.getPlug({ host: '192.168.XXX.XXX' });
//just get status
if (msg.payload === "getInfo")
{
const plug = client.getDevice({ host: '192.168.XXX.XXX' }).then((device)=>{
msg.payload = Number(device.relayState);
node.send(msg);
});
}
//plug has been activated
else if (msg.payload === true)
{
plug.setPowerState(true);
relay_state=1;
msg.payload = relay_state;
return msg;
} else if (msg.payload === false)
{
plug.setPowerState(false);
relay_state=0;
msg.payload = relay_state;
return msg;
}
msg.payload = relay_state;
return;