Can someone help me with this function node code?

Sorry if this isn't the right place but can anyone tell me why this code creates errors?

if (msg.payload === "on") msg.payload = {"service":"turn_on"}
if (msg.payload === "off") msg.payload = {"service":"turn_off"}
if (msg.payload === "play") msg.payload = {"service":"media_play"}
if (msg.payload === "pause") msg.payload = {"service":"media_pause"}
return msg

When I comment out two of the "if" lines then it runs perfectly. So, I assume I have a syntax issue?

I guess you are missing ";" - but as I am not a friend of function nodes - why are you not using a change node.

[{"id":"ea174b1fdc5e43af","type":"change","z":"cb05e14b.326a9","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"turn_on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"turn_off","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"play","fromt":"str","to":"media_play","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"pause","fromt":"str","to":"media_pause","tot":"str"},{"t":"move","p":"payload","pt":"msg","to":"payload.service","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":2120,"wires":[["7323fea9071775a8"]]}]

I ran you code but no issues. so would need more info like inputs to function

Your code would be better with if else statements and a null response if no match
e.g.

if (msg.payload === "on") msg.payload = {"service":"turn_on"}
else if (msg.payload === "off") msg.payload = {"service":"turn_off"}
else if (msg.payload === "play") msg.payload = {"service":"media_play"}
else if (msg.payload === "pause") msg.payload = {"service":"media_pause"}
else msg = null
return msg

Well, I just realized that the Play/Pause payload aren't working either alone as alexa says "device" doesn't support that.

Mostly because I don't know how change nodes work. I am so new to all of this still

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