How to test for unavailable switch

I have a smart plug that powers a lamp and. On occasions, it can be unplugged. I currently test for power below 1w to see if the lamp is off or on.

However, when unplugged, this fails as the plug is unavailable.

Obviously, this means that the lamp is off, so i woul like to add a test for the plug to test for on/off based on wether this is unavailable or not, then test for power wattage after if availBle.

I created a stae check node for unavailable text but this failed.

How do i create an unavaiable node with true or false outcomes?

This works for me:

Make a function node and test for an state/attribute that does not exist when it is unavailable, like below.

msg.test=true;
if (typeof global.get('homeassistant').homeAssistant.states["switch.lamp"].state != "undefined") {
  msg.test=false;
}
return msg;