Optional "parameters" for a subflow?

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.

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.

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?

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.

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

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.

@rogersmj any chance you have the finished json I can use as an example ?

1 Like

@rogersmj, how did you solved the 2nd condition/parameter of your subflow?

The entity id I could solve with using {{payload.data.entity_id}} as Entity ID, and add as an extra output property a msg.payload with 'entity id' as value. With that value I turned of the specific entity as Entity ID in the OFF-node.

[{"id":"88dec55dd6167201","type":"subflow","name":"240 sec + stop","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"75ec09693e67a8a4"}]}],"out":[],"env":[],"meta":{},"color":"#DDAA99"},{"id":"75163226fcb9816d","type":"change","z":"88dec55dd6167201","name":"STOP timer","rules":[{"t":"set","p":"payload","pt":"msg","to":"STOP","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":80,"wires":[["2fef29d3487871e6"]]},{"id":"2fef29d3487871e6","type":"stoptimer","z":"88dec55dd6167201","duration":"240","units":"Second","payloadtype":"num","payloadval":"","name":"","x":570,"y":140,"wires":[["52d6c3811384e498"],[]]},{"id":"75ec09693e67a8a4","type":"api-current-state","z":"88dec55dd6167201","name":"is {{payload.data.entity_id}} ON?","server":"6182da52.101a64","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"{{payload.data.entity_id}}","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"triggerId"}],"for":0,"forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":270,"y":80,"wires":[["75163226fcb9816d"],["2fef29d3487871e6"]]},{"id":"52d6c3811384e498","type":"api-call-service","z":"88dec55dd6167201","name":"Overloop {{payload}} UIT","server":"6182da52.101a64","version":3,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"{{payload}}","data":"","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":810,"y":140,"wires":[[]]},{"id":"6182da52.101a64","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"10"}]