# Duplicate function data
Author: IoT Open Developer
# Background
We sometimes get the question about how to duplicate data from one function to another. This can be of several reasons. Including:
- Duplicate data to another installation for security reasons.
- Duplicate data to mimic a replaced sensor.
- Duplicate one function from one installation of Lynx to another.
- Doing a calculation on the data (see Create a calculated function)
The same idea can also be used to export data from a function to another IoT platform or system.
# Prerequisites
You are probably used to using the Lynx web interface and knows what an Installation and Function is, if not read the introduction and get an account on Lynx.
You will need an API key to a user connected to the installation you want to get data from. See this guide. Once you have the key you are ready to go.
You will also need an instance of Node-RED running. With the IoT Open nodes configured. See this guide for information on that.
# Several ways to solve this problem
If you are using LoRa manager (>= version 0.8.0), and a recent Chirpstack installation this can be achieved by connecting multiple Lynx installations to one device in Chirpstack. See Chirpstack guide. That does however copy all functions from the device to the second installation and that might not be what you want.
# The idea
The idea is to manually create a function in the target installation and then feed this function with data from another function using Node-RED. The token used in Node-RED must have access to both installations.
The following steps need do be done:
- Create a function in the target installation
- Make sure the user (token) you are using in Node-RED can access both the target and the source installation
- Make a flow with the source (Lynx in) and target (Lynx out) functions with a connection between them.
- Test and verify
# Functions used in this story
I will use the function "Incoming function" from "Installation 1" and duplicate it to "Target function" in "Installation 2". Yours will most likely be called something else.
# Create the target function
The target function is easy to create. It can be done via the API or via the Web GUI. Here are the steps needed on the Web GUI.
- Open the target installation "Installation 2" in my case.
- Click on Functions
- Under "Add function" choose "Generic Function" and click "Add".
- Give the function a Name "Target function" in my case. Can be anything.
- Type should be the same as the incoming function. In my case "undefined".
- Add the medata-key "topic_read". I set it to
obj/virtual/target1
. It can be anything but must start withobj/<something>/<identifier>
and should be unique within this installation. - Optional: Set a format string for the function
Below is a dump of my screen:
# Make sure the user can access both installations
Then you have to make sure the user you are using in Node-RED has access to both installations. Open the user on the web GUI and make sure both installation are listed with a minus sign. If there is a plus sign, click on it to add the installation to the user.
# Make a simple Node-RED flow
Now all that is left to do is to create a Node-RED flow. The flow only need two nodes. One Lynx in and one Lynx out. Drag them to a workspace and connect them. In the Lynx in choose your incoming function and in the Lynx out choose your target function. See the images below.
# Test and verify
Just wait until data comes on the incoming function, and it should show up in the target function as well. Please note that it will take a couple of seconds until the new value is visible in the GUI.
# Conclusion
This is just one way to achieve the desired result. You can do this using other tools than Node-RED. But, the outline are the same regardless of what tools you like.
I also recommend reading the Create a calculated function which is pretty much the same as this one but with added logic between the two functions.
Don't forget that the same method might be used to copy data from one function on one IoT Open Platform to another.
# Got something you want to share?
Please let us know if you have something cool you have done and want to share with others. We will be happy to put it here. Also let us know if there is something else you want to know that we can write about here.