Msg.payload missing state element

Hello everyone.

I'm new here but loving the software already.

I'm trying to get Node-Red to report node state back to the Alexa app. I think i have most of it done but struggling with a debug error that says " msg.payload missing state element!""

Here is my flow:

The problem seems to be with my change command. I'm changing the value from my fibaro home controller which reports (true/false) to Alexas On/Off commands.

In the change settings i have the True/False set as a boolean and replaced with Json:
{
"acknowledge": true,
"payload": {
"state": {
"power": "ON"
}
}
}

It seems to output correctly in the debug but also shows the error and doesn't actually change the app at all so something is not quite right.

Regards
Mathew

Hi
Couple of things
What's "the Alexa app"? - your flow doesn't seem to send anything out :slight_smile:

What message ends up going into your change node

What's actually in the change node?

Hi There

The alexa app is an iphone app where you control all of your devices, but it shows state also.

The message going into the change node is a simple true/false depending on if the light is on or off. Obviously this needs to be change to a ON/OFF for alexa to understand.

Here is the change node.

The full code of the JSON change is:

{
	"acknowledge":true,
	"payload" : {
	   "state" : {
		   "power": "ON"
	   }
	}
}

I assume i'm doing something wrong here.

Thanks!

If you use "complete msg object" in the debug node, your message will show (currently you will only see msg.payload which is misleading):

{
 "payload":{
	"acknowledge":true,
	"payload" : {
	   "state" : {
		   "power": "ON"
	   }
	}
 }
}

Because you are changing the content of the payload, not payload itself.

See example:

Easier solution would be to use a function node.

in a function node:

m = msg.payload
s = m ? "ON" : "OFF"

msg = {acknowledge:true,payload:{state:{power:s}}}

return msg

Excellent! Thanks you!
I think it's working, sort of as my Alexa app says that the light is currently on...However it's always on and wont detect the off change.

Here is my current flow.

I have to use a change rule to change the boolean true/false into ON/OFF for alexa.

However, when i turn the light on/off the debug shows that the power state ON, It doesn't seem to detect the OFF state change.

(I have turned the light off in this picture but the state still sends ON)

Thanks for your help, changing the debug message helped loads!

You can replace the change node with the function node altogether. But yes sorry I made the assumption that msg.payload is either true or false, which this test is looking for:

s = m ? "ON" : "OFF"

It is a shorthand version of if else and, ie: if msg.payload is true then "ON"
What is the incoming payload ?

Ok, I have removed the change node now and its hit and miss. Sometimes it works, sometimes it doesn't.

I think this is because the incoming message is a boolean true/false, not a string.
Is there a way to pick up on the boolean instead?

Thanks!

Edit, I can confirm this as the info under my TKB (hallway) node shows triggered when i turn the light on/off. However with your function script after the node it changes it to [object object] which when i look online means it's expecting a string but getting something else (boolean)

Can you post the output of the TKB node of both states ? (Complete msg object in the debug node)

No problem, Here is the output.

First one is turning the switch on, second is turning the switch off.

TKB Output

Are you sure these are the only messages that you receive from the mqtt/TKB node ?
Because it sounds like you receive additional/different messages and the test would fail.

You could change the function node to capture only true/false

m = msg.payload

if(m === true){
    msg = {acknowledge:true,payload:{state:{power:"ON"}}}
} 
else if (m === false){

    msg = {acknowledge:true,payload:{state:{power:"OFF"}}}
}
else{
    msg = null
}

return msg
1 Like