Function not passing message payload

I am new to Node-RED in Home Assistant and am in need of some help with a function. I created a flow that starts our Roomba vacuum when the wife and have left the house and been away for 15 minutes. The initial flow sets a scene that turns on all the lights (vacuum has a camera and it does better when it can see obstacles). This portion works as designed.

I am trying to get the flow to run a scene to turn off all the lights when the vacuum transitions from the state of 'cleaning' to 'docked'. I used ChatGPT to get me this far but the function is not passing anything to the scene. debug 7 shows that the Cleaning to docked state change node is passing 'docked' to the function. debug 8 shows no results when I force the vacuum to dock. As far as I can tell nothing if coming out of the function node.

Also if there is a better way to handle this looping and checking the vacuums state please let me know. Perhaps there is a simpler way to do this. Function is below. Version 4.0.9

const from = msg.data?.old_state?.state;
const to = msg.payload;

if (from === "cleaning" && to === "docked") {
    return msg.payload;  // Continue the flow
}


return null;  // Ignore other transitions

Hi, welcome to the forum.

To continue, return the msg (always return the msg)

If it still doesnt work, copy the output of the debug 7 to your response.
Also, make sure "debug 7" is set to output the "complete message"

NOTE: Please use the copy value button that appears when you hover over the debug message

For future reference: Writing Functions : Node-RED

Thanks Steve-Mcl for the documentation link.

I changed debug to the full message, and I will run this again. I am now wondering if it is passing the previous state to the function that would explain why it isn't running, it has nothing to compare to.

If you need to you can store previous state in context and later, grab it and compare to the incoming state.

I realise you are new to Node-RED and may not know what context is (or a whole bunch of other tips) so I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

My vacuum has another state between cleaning and docked, returning. You can forgo the function node and use a HA trigger state node instead.

To use the a function node the path for the previous state

msg.data.old_state.state

path for current

msg.data.new_state.state

Edit: If you use a mop or have large rooms where the robot returns to the dock to empty/clean itself, using returning and docked should avoid false triggers.

Thank you mikefila, I figured there was a cleaner way to handle this. You raised a good point about the vacuums cycle. This covers our entire first floor, it will run till the battery is low, docks, empties the bin, charges, and then finishes the rooms it did not get to.

This is the portion that starts the vacuum. If I do two state nodes one with 'cleaning' to 'returning' and 'docked' to 'cleaning' and pull the scene from this portion will that be more dynamic and set the scenes appropriately?

If it is always the same cleaning pattern, current area (sf/sm) cleaned > x (this number will probably vary even with the same routine) and current battery level > x added to the conditions would probably be enough.

Depending on the integration these can be a breakout sensors or in the vacuums main entity attributes. If you are not seeing one, check the disabled entities on the device page.

If they are separate entities, in the trigger state condition set it to entity id.

If the routine varies then it gets a lot more complicated.