Scheduled shutdown of balenaSound on Pi 3B

Hello:

I love the balenaSound project, but have a question about shutdown. I believe a useful feature would be the ability to shut the device down by itself, either after not playing audio for a preset time (configurable in the device variables section), or at a certain time of the day, like 4.00 am or something - again, configurable.

My Pi 3 has been through a few SD Cards where there has been a power failure, and this seems to have taken the card down - at least I assume that’s what’s going on. Shutting down this way will switch the device off gracefully, and save energy as well.

Thanks, and great project.

Hello Mike,

I’m new on the forum, but here are my thoughts.

If you have a multi docker environment, and define it via a docker-compose yaml file, you should be able interact with the supervisor via the host APIvia the local docker network.

See https://www.balena.io/docs/learn/develop/runtime/ for the environment variables you can use ( BALENA_SUPERVISOR_ADDRESS seems convenient). Be sure to also set the label io.balena.features.supervisor-api: '1' on the container where you want to implement the shutdown timer.

This resource https://www.balena.io/docs/reference/supervisor/supervisor-api/ gives an example command to shut down the hardware:

curl -X POST --header "Content-Type:application/json" \
    "$BALENA_SUPERVISOR_ADDRESS/v1/shutdown?apikey=$BALENA_SUPERVISOR_API_KEY"

We can wrap this in a python script for example and put it in it’s own container. Something that I quickly have thrown together:

import requests
import os
import re
from dateutil.relativedelta import relativedelta
import datetime

headers = {
    'Content-Type': 'application/json',
}

params = (
    ('apikey', os.getenv('BALENA_SUPERVISOR_API_KEY')),
)

nap_time = os.getenv('NAP_TIME')
match = re.match('(\d{1,2}):(\d{1,2})', nap_time)

if match is not None:
    hour = match.group[1]
    minute = match.group[2]

    today = datetime.date.today()
    nap_time_today = datetime.date(
        today.year, 
        today.month, 
        today.day,
        hour,
        minute
    )
    while (datetime.datetime.today() < nap_time_today):
        time.sleep(5)

    requests.post('http://{}/v1/shutdown'.format(os.getenv('BALENA_SUPERVISOR_ADDRESS')), headers=headers, params=params)

Then we can set the environment variable NAP_TIME to e.g.: 4:00 (or any 24-hour clock time) and the script should shut down the host.

I hope this brings you some inspiration.
Woody

1 Like

Hello Woody:

Thanks for your fast reply. I’ll have a go at this when I get a moment! Really appreciate this.

Mike.