How do I test the current value of the RGB in a python script. I have working script for setting the value but not how to test its current value
To help you better, could you please let us know which specific SunFounder product you’re using? Alternatively, sharing a photo of the product would allow us to provide the most accurate guidance.
I have the Pironman5 (not max)
I have a service running that turns RGB off at 9pm and back on at 9am but the flaw in this approach is it depends on system being available in those two minute windows. Since the script loops every 60 seconds id rather test for current status then toggle based on current time if required. To do this, I neeed to be able to poll current RGB true/false value. I find no docs on API.
I’m not sure as to the content nor full requirements of your script but as a suggestion: can you use crontab and a pair of scripts instead? That would not require any polling etc. on your part
E.g to turn on at 9 am entry would be
00 9 * * * turn_on_rgb_script
to turn off at 9 pm entry would be
00 21 * * * turn_off_rgb_script
#==== I have the following service running in
#==== /etc/systemd/system/led_timer.service
[Unit]
Description=script daemon for turning off led at night
[Service]
User=root
Group=0
WorkingDirectory=/home/admin
ExecStart=/home/admin/venv/bin/python /home/admin/led_timer.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
#======= With the following python script configured in
#======= /home/admin/led_timer.py
from datetime import datetime
import subprocess
while True:
current_time = datetime.now().strftime(“%H:%M”)
if current_time == “21:00”: # 9:00 PM
subprocess.call([“pironman5 -gm 4”], shell=True)
subprocess.call([“pironman5 -re false”], shell=True)
subprocess.call([“sudo systemctl restart pironman5.service”], shell=Tru>
time.sleep(60)
if current_time == “09:00”: # 9:00 AM
subprocess.call([“pironman5 -gm 0”], shell=True)
subprocess.call([“pironman5 -re true”], shell=True)
subprocess.call([“sudo systemctl restart pironman5.service”], shell=Tru>
time.sleep(60)
time.sleep(60)
This is totally functional, but its weakness is it relies on the system not being busy or down during eother of the two 1 minute windows. A flaw, I might add, that is shared by a crontab solution. Since the service is in a timer loop that runs
once a minute, I feel that a more elegant solution would be to poll the current status
and compare toggle if necessary based on the system clock. This way if the system is down at 9 am/pm
the led will still get toggled as soon as it is available. Assuming there is a way to poll the current rgb status.
If polling is not available (I don’t know) then would anacron work out instead? Anacron is robust against missed events, downtime etc
Currently, it seems there is no way to determine whether the LED is on or off.
You can try adjusting the code:
Instead of using ==
to check the time, which might miss the exact moment due to time discrepancies, you can change it to use a state machine and the greater-than operator to trigger the control. Using the greater-than operator ensures that you can reliably detect when to switch states, and a simple state machine can prevent conflicts between two time points.
The code logic is as follows:
from datetime import datetime
state = “OFF”
now = datetime.now()
if state == “OFF”:
if now.hour > 9 or (now.hour == 9 and now.minute > 0):
# turn on RGB LED
state = “ON”
elif state == “ON”:
if now.hour > 21 or (now.hour == 21 and now.minute > 0):
# turn off RGB LED
state = “OFF”