Build rules with Blickfeld Flow
Blickfeld Flow runs your own rules on the sensor. It is a low-code editor based on Node-RED: you place nodes on a canvas, wire them together and deploy the result, without a computer next to the sensor. Use it to send data in a format or over a protocol that Push does not offer, to combine zone states into your own logic, or to call the API of the sensor on a schedule. Deployed flows run on the sensor, also with the browser closed, and start again after a reboot.
The editor
-
In the sidebar, click Configuration > Flow.
The Node-RED editor opens inside the WebGUI and the sidebar of the WebGUI collapses.
A change to a flow takes effect only after you deploy it: click Deploy at the top right of the editor or press Ctrl+D. A lock on a tab marks a built-in flow of the firmware; it is read-only and not meant to be edited. The editor itself is described in the Node-RED user guide.
The Blickfeld nodes
Next to the standard nodes of Node-RED, Flow offers:
| Node | Category | What it does |
|---|---|---|
blickfeld-qb2-method |
common |
Calls a method of the API of the sensor, for example to stream the zone states, read the health or change a setting. It reaches the sensor it runs on without a key, and other sensors in the network with an application key. Requests and responses are JSON. See method node. |
blickfeld-device |
Configuration node |
Stores the address and the application key of another sensor once, for every method node that talks to it. |
The blickfeld-qb2-method node comes with example flows.
To import one, click the main menu button at the top right of the editor, then Import, open the Examples tab and choose the example under flows > node-red-contrib-blickfeld-qb2-method:
-
events_fetching streams the events of the sensor into the Debug messages, with a catch node that shows the errors of the stream.
-
pause_alarm_history pauses the alarm history every day at 22:00 and resumes it at 06:00. It reads the perception configuration, sets
history.pauseand writes the configuration back.
The method node page shows the steps and the flow.
Build a first flow
As an example, consider a flow that sends data of the sensor to another system.
It consists of an inject node that starts a blickfeld-qb2-method node, a function node that turns the response into messages, and an mqtt out node that sends them to your MQTT broker.
The method node returns the response as JSON, with the field names of the API reference in lower camel case, for example generatorName for generator_name.
This flow publishes one MQTT message for each vehicle that leaves a traffic lane zone, with its class and its speed.
-
Drag an inject node onto the canvas. Double-click it, tick Inject once after 0.1 seconds, leave the delay at its default value, and click Done.
-
Drag a blickfeld-qb2-method node onto the canvas and wire the output of the inject node to it. Double-click it:
-
Leave Connection at Local device.
-
At Method, choose
percept_processing.Event/Stream. -
At Request, choose JSON and enter
{}. The stream then starts with the next event. -
At Response, choose
response., theneventin the list that opens. The node then puts each event intomsg.payload. -
Leave Auto reconnect server-side streams on error ticked.
-
Click Done.
-
-
Drag a function node onto the canvas and wire the method node to it. Double-click it, enter this code and click Done:
const detected = msg.payload.trafficVehicleDetected; if (!detected) { return null; } const vehicle = detected.vehicle; msg.topic = "blickfeld/vehicles"; msg.payload = { zone: detected.zoneUuid, class: vehicle.modelClassification?.predictedClass?.tlsVehicle, speed: Math.abs(vehicle.velocity) * 3.6 }; return msg;It passes only the vehicle events and sends the ID of the lane zone, the class of the vehicle, for example
TLS_VEHICLE_CAR, and the speed in km/h. -
Drag an mqtt out node onto the canvas and wire the function node to it. Double-click it, add a server with the host name and port of your broker, leave Topic empty to use the topic of each message, and click Done.
-
Optionally, wire a debug node to the method node to watch its output in the Debug messages of the sidebar.
-
Click Deploy at the top right of the editor.
Your broker receives one message on blickfeld/vehicles for each vehicle.
Use the same pattern for other data: stream percept_processing.Event/Stream for the events, or call diagnostics.Health/Get for the health, and replace the mqtt out node with an http request, websocket out, tcp out or udp out node.
Receive HTTP requests
Another system can send data into a flow with an HTTPS request to an http in node, for example to tell the flow that a gate is open.
The node answers under /flow of the address of the WebGUI: an http in node with the URL /gate receives the requests to https://qb2-ABC123XYZ.local/flow/gate.
The sensor passes a request on only with a valid token in the cookie Authorization; without one, or with an expired one, it answers 401 Unauthorized.
-
Drag an http in node onto the canvas. Double-click it, set Method to POST, enter
/gateat URL and click Done. -
Drag a function node onto the canvas and wire the http in node to it. Double-click it, enter this code and click Done:
flow.set("gateOpen", msg.payload.open === true); msg.payload = { gateOpen: flow.get("gateOpen") }; return msg;It keeps the state of the gate in the context of the flow, where other nodes of the flow read it with
flow.get("gateOpen"), and answers with it. -
Drag an http response node onto the canvas and wire the function node to it.
-
Click Deploy.
The example flow
[
{
"id": "b1f10a0e00000101",
"type": "tab",
"label": "Receive HTTP requests",
"disabled": false,
"info": "Answers a POST request to /flow/gate: remembers whether the gate is open and returns it.",
"env": []
},
{
"id": "b1f10a0e00000111",
"type": "http in",
"z": "b1f10a0e00000101",
"name": "",
"url": "/gate",
"method": "post",
"upload": false,
"swaggerDoc": "",
"x": 110,
"y": 80,
"wires": [
[
"b1f10a0e00000112"
]
]
},
{
"id": "b1f10a0e00000112",
"type": "function",
"z": "b1f10a0e00000101",
"name": "Remember the gate",
"func": "flow.set(\"gateOpen\", msg.payload.open === true);\nmsg.payload = { gateOpen: flow.get(\"gateOpen\") };\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 310,
"y": 80,
"wires": [
[
"b1f10a0e00000113"
]
]
},
{
"id": "b1f10a0e00000113",
"type": "http response",
"z": "b1f10a0e00000101",
"name": "",
"statusCode": "",
"headers": {},
"x": 500,
"y": 80,
"wires": []
}
]
The client gets the token with the Login method of the Authentication service, signed in with an application key, and sends it as Authorization=Bearer <token>.
A read-only key is enough and lets the client change nothing else on the sensor.
A token is valid for 24 hours: sign in for each request, or keep the token and sign in again when the sensor answers 401.
This Python script sends {"open": true} to the flow, with the blickfeld_qb2 library for the sign-in.
It accepts the certificate of the sensor only if Blickfeld signed it, as the library does for its own connections.
Run it with the host name of the sensor and the secret of the application key:
python3 flow_http_request.py qb2-<serial number> <application key>
import json
import ssl
import sys
import urllib.request
import blickfeld_qb2
from blickfeld_qb2.base.grpc.device_ca_cert import ca_cert
from blickfeld_qb2.secure.services import Authentication, AuthenticationLoginRequestApplication
def sign_in(host, key):
with blickfeld_qb2.Channel(host) as channel:
application = AuthenticationLoginRequestApplication(key=key)
return Authentication(channel).login(application=application).token
def send_to_flow(host, key, path, data):
# Accept only a certificate signed by the device CA of Blickfeld, as blickfeld_qb2.Channel does
tls = ssl.create_default_context(cadata=ca_cert)
tls.check_hostname = False
request = urllib.request.Request(
f"https://{host}/flow/{path}",
data=json.dumps(data).encode(),
headers={
"Content-Type": "application/json",
"Cookie": f"Authorization=Bearer {sign_in(host, key)}",
},
)
with urllib.request.urlopen(request, context=tls) as response:
return json.loads(response.read())
if __name__ == "__main__":
host, key = sys.argv[1:3]
print(send_to_flow(host, key, "gate", {"open": True}))
Output:
{'gateOpen': True}
Access over the API
Flows are stored in the configuration of the sensor. The Flow service lists, reads, stores and deletes them in the JSON format of Node-RED that the editor imports and exports, and reloads Node-RED. Store and delete take effect after a reload.
To roll out the same flow to several sensors, export it in the editor with Export and import it on each sensor, or store it with the Flow service. Passwords and keys entered in nodes, such as the password of an MQTT broker, are stored apart from the flows: an export leaves them out. Enter them again after an import.
Limits
| Limit | What it means | What to do |
|---|---|---|
Compute power |
Flow runs with a lower priority than the processing of the sensor and may use at most one of the four processor cores and 768 MB of memory. A busy flow does not disturb the zones and the alarms, but becomes slow itself. |
Reduce the message rate early in the flow, see Rate limit messages, and keep large messages out of the flow, see Working with messages. |
Point clouds |
Flow has too little computing power to process point clouds in real time. |
Do not process point clouds in a flow. If your application needs it, contact Support and repair. |
Storage |
Packages from the palette are installed on the data partition of the sensor, which has 2 GB. Packages with many dependencies can fill it. |
Install only the packages you need. |
Files |
The file and watch nodes are not available. Flows run in a sandbox that only reaches its own data directory on the sensor. |
Exchange data over the network. |
Maintainability |
Large flows are quick to build and hard to maintain. |
Read Developing flows before a flow grows beyond a few nodes. |
Troubleshooting
- The method node shows an error or does not connect
-
Open the node and click Test method to see the response or the error without deploying. For another sensor, check its address and application key in the blickfeld-device node; Test checks the key. A catch node wired to the method node receives every error.
- Nothing arrives in the Debug messages
-
Check that you clicked Deploy, that the inject node fires once after the start, and that the debug node is switched on. The debug sidebar shows at most 1000 characters of each message.