This feature is on the main branch of
nerves_hub_link and NervesHub but is in no tagged release as of nerves_hub_link 2.14.0 and NervesHub 2.5.4. You need a build from main of both to use it.Requirements
nerves_hub_linkfrom main, forNervesHubLink.going_to_sleep/2.- NervesHub from main. An older NervesHub logs the message as unknown and carries on. The connection still closes and the device shows as offline, so firmware does not need to check which server it is talking to.
Tell NervesHub before sleeping
CallNervesHubLink.going_to_sleep/2 just before the device sleeps:
sleeping message on the device channel and closes the connection. It returns once both are done, or after five seconds if the close is slow, so the device can sleep straight after. NervesHub does not reply.
NervesHub shows the mode to people and does nothing else with it. Use
:deep when the device loses what it was running and starts again on waking. Use :light when it carries on where it left off, as after a suspend to RAM.How long the device will sleep, in milliseconds. Leave it out when something else wakes the device, such as a GPIO or a sensor interrupt. NervesHub then shows it as asleep with no time given.
going_to_sleep/2 returns {:error, :disconnected} when the device channel is not joined. It sends nothing and leaves the connection as it was. The device can still sleep, and NervesHub shows it as offline. The call raises ArgumentError on an unknown option, a bad :mode, or a :duration_ms that is not a positive integer.
Reconnect on waking
Aftergoing_to_sleep/2 the connection stays closed.
A device in deep sleep restarts when it wakes and connects at boot as it always does.
A device that keeps running through its sleep calls NervesHubLink.reconnect/0 when it wakes:
:duration_ms also reconnects on its own once that much time has passed, in case nothing calls reconnect/0. Do not rely on that after a suspend to RAM. The BEAM’s timers may not count the time spent suspended, so the reconnect fires late.
A device that gave no duration stays disconnected until you call reconnect/0.
Check that it works
Put a device to sleep with a duration, then open it in NervesHub.- On the device page, the status dot is replaced by a sleep mark. The line beside the identifier reads “Asleep” with when the device is due back, such as “back in 5 minutes”.
- In the devices list, the Connection column shows the same, and the Connection filter has an Asleep choice.
- In the search bar,
connection = "asleep"finds sleeping devices, including overdue ones. From the CLI, usenh device list --filter connection:asleep.
Overdue devices
A device is overdue once it is later than it said by two minutes or a tenth of how long it slept, whichever is longer. A five-minute sleep has until seven minutes. A ten-hour sleep has until eleven hours. An overdue device reads “Overdue” with how late it is, and its sleep mark turns orange. A device that gave no duration is never overdue.What NervesHub does with a sleep
- Insights and health. A connection that ended in sleep does not count toward the Flapping list on Insights or toward the disconnects health metric. See Health monitoring.
- API. A device’s
sleepfield holdsstate,mode, andexpected_wake_atfrom when the device says it is going to sleep until it connects again. It is null otherwise.stateisgoing_to_sleep,asleep, oroverdue. - Webhooks. The
device.disconnectedevent carries areason_codeoflight_sleepordeep_sleep, andexpected_wake_at. See Webhooks.
Limits
- A sleep duration of zero or less, one that is not a whole number, or one over 366 days is recorded as a sleep with no time given.
- NervesHub does not yet raise anything when a device misses its wake time. Overdue devices only show in the UI and the API.
Other device clients
The message is part of the device protocol, so a client other thannerves_hub_link can send it. Before it sleeps, the device pushes sleeping on its device channel, then closes the socket:
mode is "deep" or "light", and anything else is taken as "deep". See WebSocket events.

