Conditional cloud sync
By default, viam-server checks for new data to sync at the configured interval (sync_interval_mins).
You can additionally configure sync to only happen when certain conditions are met.
For example:
- Only sync when on WiFi
- Sync when conditions are met or events are detected
- Sync during certain time windows
To sync only when certain conditions are met, you must provide the data manager a sensor that returns "should_sync": true when sync should happen and "should_sync": false otherwise.
This page shows you the implementation of a sensor which only allows sync during a defined time interval. You can use it as the basis of your own custom logic.
You can also view the trigger-sync-examples module for more examples.
Prerequisites
Return should_sync as a reading from a sensor
Note
If you are using a pre-built module like sync-at-time:timesyncsensor, you can skip this section and go directly to Add your sensor to determine when to sync.
This section is for users who want to create their own custom sensor module.
The following example implementation of the Readings() method returns "should_sync": true if the current time is in a specified time window, and "should_sync": false otherwise.
func (s *timeSyncer) Readings(context.Context, map[string]interface{}) (map[string]interface{}, error) {
currentTime := time.Now()
var hStart, mStart, sStart, hEnd, mEnd, sEnd int
n, err := fmt.Sscanf(s.start, "%d:%d:%d", &hStart, &mStart, &sStart)
if err != nil || n != 3 {
s.logger.Error("Start time is not in the format HH:MM:SS.")
return nil, err
}
m, err := fmt.Sscanf(s.end, "%d:%d:%d", &hEnd, &mEnd, &sEnd)
if err != nil || m != 3 {
s.logger.Error("End time is not in the format HH:MM:SS.")
return nil, err
}
zone, err := time.LoadLocation(s.zone)
if err != nil {
s.logger.Error("Time zone cannot be loaded: ", s.zone)
return nil, err
}
startTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(),
hStart, mStart, sStart, 0, zone)
endTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(),
hEnd, mEnd, sEnd, 0, zone)
// Handle time windows that span midnight (e.g., 23:00 to 01:00)
// If end time is earlier than or equal to start time, the window spans midnight
if endTime.Before(startTime) || endTime.Equal(startTime) {
// Add 24 hours to endTime to account for midnight crossover
endTime = endTime.Add(24 * time.Hour)
}
readings := map[string]interface{}{"should_sync": false}
readings["time"] = currentTime.String()
// Check if current time is within the sync window
if currentTime.After(startTime) && currentTime.Before(endTime) {
s.logger.Debug("Syncing")
readings["should_sync"] = true
return readings, nil
}
// Otherwise, do not sync.
s.logger.Debug("Not syncing. Current time not in sync window: " + currentTime.String())
return readings, nil
}
Note
You can return other readings alongside the should_sync value.
You must also update the Config and resource struct to include the required attributes and update the init() and Validate() functions appropriately.
Check the implementation of the sensor on GitHub for more detailed information.
For additional examples, see the Readings function of the time-interval-trigger code and the color-trigger code.
Add your sensor to determine when to sync
This section shows how to add and configure sync-at-time:timesyncsensor.
If you created your own sensor module following the previous section, you will need to follow similar steps with your module:
Add the sensor to your machine
On your machine’s CONFIGURE tab, click the + button next to your machine part in the left menu.
Select Component or service, then search for and select the sync-at-time:timesyncsensor model provided by the sync-at-time module.
Click Add module, then enter a name or use the suggested name for your sensor and click Create.
Configure your time frame
Go to the new component panel and copy and paste the following attribute template into your sensor’s attributes field:
{
"start": "HH:MM:SS",
"end": "HH:MM:SS",
"zone": "<TIMEZONE>"
}
{
"start": "18:29:00",
"end": "18:30:00",
"zone": "CET"
}
The following attributes are available for the naomi:sync-at-time:timesyncsensor sensor:
| Name | Type | Required? | Description |
|---|---|---|---|
start | string | Required | The start time for the time frame during which you want to sync. Example: "14:10:00". |
end | string | Required | The end of the sync time frame, for example: "15:35:00". |
zone | string | Required | The time zone for the start and end time, for example: "CET". |
In the next step, you will configure the data manager to take the sensor into account when syncing.
Configure the data manager to sync based on sensor
On your machine’s CONFIGURE tab, switch to JSON mode and add a selective_syncer_name field with the name of the sensor you configured, and add the sensor to the depends_on field:
{
"name": "data_manager-1",
"api": "rdk:service:data_manager",
"model": "rdk:builtin:builtin",
"attributes": {
"additional_sync_paths": [],
"capture_dir": "",
"capture_disabled": false,
"selective_syncer_name": "<SENSOR-NAME>",
"sync_disabled": false,
"sync_interval_mins": 0.1,
"tags": []
},
"depends_on": ["<SENSOR-NAME>"]
}
{
"name": "datamanager",
"api": "rdk:service:data_manager",
"model": "rdk:builtin:builtin",
"attributes": {
"additional_sync_paths": [],
"selective_syncer_name": "timesensor",
"sync_interval_mins": 0.1,
"capture_dir": "",
"tags": []
},
"depends_on": ["timesensor"]
}
You have now configured sync to happen during a specific time slot.
Test your sync configuration
To test your setup, configure a webcam or another component and enable data capture on the component.
Make sure to physically connect any hardware parts to the computer controlling your machine.
For a camera component, use the GetImages method.
The data manager will now capture data.
Go to the CONTROL tab.
You should see the sensor.
Click on GetReadings.

If you are in the time frame for sync, the time sync sensor will return true.
If you are not in the time frame, it will return false.
To verify that sync is working:
- If the sensor returns
trueand data has been captured, go to the Data tab to see your synced data. - If the sensor returns
falsebut you want to test sync, adjust the configuration of your time sync sensor to include the current time. - Check again on the CONTROL tab to verify the sensor reading, and on the Data tab to confirm data is syncing.
Was this page helpful?
Glad to hear it! If you have any other feedback please let us know:
We're sorry about that. To help us improve, please tell us what we can do better:
Thank you!