When are Particle.variable()s actually sent to the cloud?

When a Photon or Electron (particularly the latter) publishes one or more variables with, say,

Particle.variable("officetemp", officetemp);

when is the data associated with this variable actually transported between the device and the cloud? Is it on update (i.e. at every Particle.variable() invocation), or is it when a REST API call to the cloud is performed?

The reason I ask: I’d like to be able to update some values relatively frequently on the device, every 10s, say, but I don’t wish to incurr the expense associated with the data transport unless an application actively queries the values.

Variables are retrieved from the device only when requested. Also, you normally put a call to Particle.variable in setup() and only execute it once. You don’t tell it every time it changes; the current value of the variable is retrieved when requested.

1 Like

Excellent, thank you,

What happens if the device is unreachable (cloud to device)? It’s not clear to me from the Reference documentation whether the requestor gets a result (cached by the cloud?) or an error. May I ask you to elaborate?

The cloud API returns an error if the device is not online at the time of the request. The value isn’t cached, because variables are only retrieved when requested, so there would be no way to assure that the data is valid.

If you do need offline values, most people use a publish event and then either use a web hook or monitor the event stream from another server to remember the value. The ability for the Particle cloud to remember the last published value is a planned feature, but it doesn’t exist yet.

That’s fine and suits me well.

The disadvantage of publish events (in terms of data consumption) will be that the value will be transported whether it’s consumed or not which will defeat my purpose of minimizing mobile data usage.

You are in control of the data.
So you could expose a var and publish just before you go offline - but this only works for planned disconnects.