BUGS HUNTER Part 2

Scheduler that ignores settings.

Please note that these are my observations based on experiments and observations. They may not correspond to reality, only Sonoff can confirm the accuracy of my results.

A common problem that bothers users is the set scheduler that ignores the settings.

A common problem I’ve noticed. I have a set time and the timer doesn’t do anything. I was able to trigger this problem in two ways:

  1. I set the time and temporarily turned off the device. I turned the device on just before the set time and then did it again by turning the device on just after the set time. In both cases, the timer completely ignored it.

This shows that the algorithm is poorly designed.

2, the case where I have filled NSPP with data from ZigBee sensors, scenes and other tasks, so the scheduler will ignore the sheduler. Because the condition is met but no longer valid:

if ( Scehuled.time && Scheduled.data == Actual.time && Actual.date ) then exec.heater

In layman’s terms, if the timer is delayed by one second, it will not start because:

The algorithm is designed so that if you want to run a task at 19:30 and the scheduler gets an order at 19:31, the above condition no longer applies. And so it will not execute the requested task.

This algorithm proposal would explain why sometimes the timer runs and sometimes it doesn’t.

I think I roughly understand what you’re trying to say: You mean that if you set a scheduled trigger for 19:30 and another condition B is also required to execute action C, then if condition B is met before 19:30, the action C will not be executed — it will only execute if condition B is met exactly at 19:30? Or if condition B is met after 19:30, it still won’t run?

If I understood correctly: In this case, if condition B is met before 19:30, action C can still execute normally. But if condition B is met after 19:30, it will not execute.

If my understanding is wrong, please describe your setup in more detail — whether it’s about the TRV or other device scenarios.

Yes, you understand, in simple terms, if the scheduler does not have time to execute the task at 19.30, it will not start it. It is enough that any process has a higher priority and will need more time to run, so the scheduler will start, but the current system time is already different.

And therefore, if it compares the set time with the system time and they do not match, nothing will start.

Example:

import time
import json
import os
from datetime import datetime, timedelta

STATE_FILE = “scheduler_state.json”
INTERVAL_SECONDS = 10 # every 10 sec

def load_last_run():
if not os.path.exists(STATE_FILE):
return None
with open(STATE_FILE, “r”) as f:
data = json.load(f)
return datetime.fromisoformat(data[“last_run”])

def save_last_run(dt):
with open(STATE_FILE, “w”) as f:
json.dump({“last_run”: dt.isoformat()}, f)

def task(run_time):
print(f"[{datetime.now()}] Executing task: {run_time}")

def run_scheduler():
last_run = load_last_run()
now = datetime.now()

if last_run is None:
    print("Furt run")
    last_run = now
    save_last_run(last_run)
    return

# Calculate sheduler delay
missed_runs = []
next_run = last_run + timedelta(seconds=INTERVAL_SECONDS)

while next_run <= now:
    missed_runs.append(next_run)
    next_run += timedelta(seconds=INTERVAL_SECONDS)

# Execute missing task
for run_time in missed_runs:
    task(run_time)

#Exec Actual task
task(now)

save_last_run(now)

if name == “main”:
while True:
run_scheduler()
time.sleep(INTERVAL_SECONDS)