Optional "parameters" for a subflow?

#1

I'm early into my adventure with Node-RED. I have a pattern that I expect to be repeating quite a lot in my flows. I've figured out that I can turn it into a subflow. However, I want to be able to pass "parameters" to it to adjust the behavior and I'm unsure of the best way to accomplish this.

So here's my subflow, which shows a pattern I will use in many places for a bunch of my motion-triggered lights:
image

The input will be a message triggered by a motion detector state change in Home Assistant. Outside this subflow, I want to be able to (for each instance in which I use it) specify:

  1. Which entity ID the "Already on?" node should check
  2. How long the timer node should wait (but default it to 30 minutes if no value is provided)

Basically in coding world this is a function with two parameters, the first one required and the second one optional. What's the right way to set this up? I've been researching variables and context...little lost. Should I be adding message properties before calling the subflow, and then reference those within the nodes' properties? Is that possible?

I'm still getting familiar with the terminology so please speak slowly :slight_smile: Thanks for the help.

#2

You already look to be part way there. I would recommend adding additional properties to the messages (change nodes are good for this) before they enter the subflow and have the first node inside the subflow be a switch node that can determine the presence of the added properties and route accordingly.

#3

OK I think I'm following. But I can't get it to work, I'm not sure what's wrong.

On the outside (the "calling" flow), I have a change node where I'm adding an "entity" property to the message before it hits the subflow.

Within the subflow, I'm getting an error the "Already on?" Current State node. I added a debug node to look at my msg object, and I can see the entity in there as expected. Am I not referencing it correctly in the Current State node's properties?

#4

Hi @rogersmj

You will need to check the 'current state' node's help to see if and how it supports the Entry ID being specified by a message property. You cannot assume it knows what to do with {{entity}} - that isn't a generic capability available in all nodes.

You may have seen another thread on a very related topic in the last day or so. WE are working to add the ability to set properties on a per-subflow-instance basis. So you can customise individual instances. This will be available across the next couple of releases.

#5

Does the info tab on that node type give any recommendations? The instructions on github/npm are somewhat lacking.

#6

Oooh...ok I didn't realize that. I dug around the docs for the node type and didn't see anything relevant. I will make a feature request in that project.

Agreed...I thought referencing the message object anywhere in the properties was a standard thing, so I was sure I was missing something at the platform level. But apparently not.

Thank you both for the help.