0

getting a value from a JavaScript object, working in Node red for home automation, a service I'm using delivers data as an object but I only need a Boolean for MQTT.

{
    "command": "action.devices.commands.OnOff",
    "params": {
        "on": true,
        "online": true
    }
}

here is the object in question that the service outputs and I don't quite understand what key to use to return the value of "on". in the picture i tried what i think should have worked but it never seems to work.

the function node I have been trying to use as a filter

I have gotten errors such as

TypeError: Cannot read properties of undefined (reading 'on')

and other rigamaroo about how something cannot be a boolean

Phil
  • 157,677
  • 23
  • 242
  • 245
  • See how the debugger says `msg.payload` is a string? You need to parse it as JSON... `const state = JSON.parse(msg.payload).params.on` – Phil May 24 '23 at 02:22
  • whare do I put that, it still throws the same error – Layden Blackwell May 24 '23 at 02:28
  • Most likely you have a JSON node in your flow, remove it. And you should also be setting `msg.payload = state`, the `msg` should always be and object with a `payload` – hardillb May 24 '23 at 06:37

0 Answers0