logo elektroda
logo elektroda
X
logo elektroda

OpenBeken scripting: check if file exists on LittleFS, workarounds for file presence detection

User question

is it possible to check file exist in openbeken script?

Artificial Intelligence Response

Direct answer to the question

  • Short answer: No. OpenBeken’s scripting language does not expose a built‑in “file exists” predicate (there’s no test like if exist filename). Scripts can run files and use conditions, but they cannot query LittleFS for a filename and branch on that result. (github.com)
  • Key points:
    • OBK scripts live on LittleFS, and you can start/stop scripts and use if/else, variables, channels, delays, events, etc. (github.com)
    • Files can be listed or managed from the Web UI/console, but the output isn’t parseable inside a script, so you cannot implement a native file-existence check. (elektroda.com)

Detailed problem analysis

  • What the scripting engine can do

    • Invoke other scripts by filename (startScript fileName [label]) and use conditional logic on device state (channels, Wi‑Fi state, timers). This is sufficient for most automation tasks. (github.com)
    • OBK maintains an extensive, auto‑generated command set; as of the latest public docs, no command provides a boolean “file present?” test. If a predicate existed, it would appear in the commands list. (github.com)
  • Why there is no file-existence primitive

    • OBK targets resource‑constrained MCUs. The scripting layer is intentionally lightweight (device control, MQTT/HTTP, timers, simple conditionals) rather than a shell with a full filesystem API. That keeps the interpreter small and predictable.
  • Consequence

    • You cannot write if exist myfile.bat ... directly.
    • However, you can structure scripts to infer the file’s presence by side effects, or design flows that do not require the check at all.
  • Proven workarounds 1) Side‑effect flag (robust)

    • Pattern: Initialize a known “flag” channel to 0. Attempt to run the optional script. Inside that script, set the flag to 1. After the attempt, test the flag; if it’s still 0, treat the file as “missing.”
    • Rationale: startScript will run if the file is present; the only way the flag becomes 1 is when that file actually executes. 2) Blind execution (simplest)
    • Pattern: Just call startScript optional.bat. If present, it runs; if not, nothing changes. Follow with defaults. Use only if you don’t need to branch explicitly on presence. 3) Out‑of‑band verification (for humans or external controllers)
    • From the Web App’s File system tab you can list/download the LittleFS contents to confirm a particular file exists before deployment. An external controller (e.g., Home Assistant/Node‑RED) can trigger a “list files” command and decide what to do, but device‑side scripts cannot parse the listing. (elektroda.com)
  • Example implementation (side‑effect flag)

    • autoexec.bat (main script)
    • setChannel 90 0
    • startScript optional_config.bat main
    • if $CH90==1 then echo "optional_config present" else echo "optional_config missing; applying defaults" endif
    • optional_config.bat
    • main:
      • setChannel 90 1
      • [your extra configuration...]
      • exit;
    • Notes:
    • Channels are convenient general‑purpose variables in OBK scripts. (github.com)
    • Label usage (like main:) is supported by the script engine and makes control flow explicit. (github.com)

Current information and trends

  • The authoritative command lists (updated per release) still do not show a file‑existence predicate as of the 1.18.22x series (December 2025/January 2026 timeframe). If a native check is ever added, it will appear there first. (github.com)
  • LittleFS management from the Web UI (list, upload, download, format) continues to be the supported way to inspect files manually; scripts remain intentionally limited in filesystem introspection. (elektroda.com)

Supporting explanations and details

  • Why the side‑effect pattern is reliable
    • It uses the execution of the target file itself as the truth source. If the script runs, it sets the flag; if it doesn’t, the flag stays at the initialized value.
    • This avoids depending on console output parsing (which OBK scripts can’t do) or on undocumented error codes.
  • Why blind execution is often “good enough”
    • Many setups only need “run if present, otherwise ignore.” Trying to run a non‑existent script simply results in no effect on state; the rest of autoexec continues.

Ethical and legal aspects

  • Avoid destructive commands such as filesystem format (lfs_format) in shared templates unless you fully understand the impact. Users may lose files (including autoexec.bat) if they execute such commands inadvertently. The docs and community explicitly caution about this. (elektroda.com)
  • If you automate remote devices, communicate clearly to users/clients that scripts cannot self‑audit file presence; plan safe fallbacks.

Practical guidelines

  • Implementation steps
    • Create optional_config.bat that sets a known channel (e.g., 90) to 1 near the top.
    • In autoexec.bat: setChannel 90 0; startScript optional_config.bat; test $CH90 and branch accordingly.
    • Keep your defaults in autoexec.bat so the device remains operational if the optional file is absent.
  • Best practices
    • Reserve a dedicated “flag” channel number and document it.
    • Keep optional scripts idempotent; running them twice shouldn’t break anything.
    • Use the Web UI File system tab to validate files during development and before OTA rollout. (elektroda.com)
  • Potential challenges and mitigations
    • Race conditions: If multiple scripts might set the same channel, namespace your flags (choose higher channel IDs and document ownership).
    • Maintenance: If you later rename the optional file, update the startScript call and redeploy both files.

Possible disclaimers or additional notes

  • Limitations
    • No native “exists?” test in scripts; no way to parse filesystem listings in‑script as of January 14, 2026. (github.com)
  • Alternatives
    • External orchestration (Home Assistant/Node‑RED) can list files and publish a “present/missing” status back to OBK over MQTT for script consumption.

Suggestions for further research

  • Monitor the auto‑generated command references for any new LittleFS predicates (search for lfs_… or exists in future releases). (github.com)
  • Review the scripting engine page and examples to refine multi‑script coordination and labels. (github.com)
  • Community threads and docs on the File system tab workflows (backup/restore, “Download Tar of FS”) for safer deployment procedures. (elektroda.com)

Brief summary

  • OpenBeken currently offers no built‑in “file exists” check in its scripting language. Use a side‑effect flag pattern (recommended) or blind execution to keep logic simple and robust. Validate file presence manually via the Web UI or externally if you need human‑visible confirmation. Check the official, auto‑generated command lists in future releases in case a predicate is added. (github.com)

If you share what you’re trying to gate on file presence (e.g., conditional configuration, over‑the‑air script packs), I can sketch the exact autoexec.bat you need.

Ask additional question

Wait...(2min)
Disclaimer: The responses provided by artificial intelligence (language model) may be inaccurate and misleading. Elektroda is not responsible for the accuracy, reliability, or completeness of the presented information. All responses should be verified by the user.