OpenBeken scripts: sharing global variables using channels, setChannel, $CHn, persistence tips
User question
How to use global variables between two OpenBeken scripts ?
Artificial Intelligence Response
Direct answer to the question
- Use Channels as your global variables. Write with setChannel/addChannel and read with $CHn. Channels are shared across all OpenBeken scripts (autoexec.bat, aliases, event/change handlers), so any script can read/write the same channel index. (elektroda.com)
- Example:
- Script A: setChannel 20 1
- Script B: if $CH20==1 then do_something
- Channels are numeric (floating‑point) registers. For persistence across reboots, initialize or restore them in autoexec.bat, or bind them to a channel type that the firmware saves; plain “logic” channels start at 0 after reboot unless you reinitialize them. (elektroda.com)
Key points
- Global scope: there is one interpreter; scripts are not isolated. (elektroda.com)
- Numeric only: channels hold numbers; use $CHn in expressions and conditionals. (elektroda.com)
- No C‑style “global var” declaration (e.g., global x=0) and no generic setConfig/getConfig for script variables; use channels instead. See constants/expressions and scripting examples. (github.com)
Detailed problem analysis
-
Execution model
- OpenBeken runs a single, event‑driven command interpreter. Files such as autoexec.bat and blocks like alias, addEventHandler, addChangeHandler, addRepeatingEvent all share the same runtime state. Therefore, anything you write to a channel in one “script” is visible to any other. Practically, this is your “global variable” mechanism. (elektroda.com)
-
Channels as global registers
- Channels are general‑purpose numeric registers you can set, add to, and read in expressions:
- setChannel 10 123
- setChannel 11 $CH10*10
- if $CH11<200 then do_something
This is the idiomatic way to share state between scripts. The syntax and usage are shown in the project’s answers and examples, and in the expressions/constants documentation. (elektroda.com)
-
Inter‑script communication pattern
- Producer script writes to a channel:
- Example: a button handler or MQTT command sets CH20 to indicate “armed” mode:
alias arm backLog setChannel 20 1; echo Armed
- Consumer script reads the same channel and acts:
addChangeHandler Channel5 == 1 if $CH20==1 then echo ALARM
- Because all handlers and aliases see the same $CH20, this works across files and event contexts. (elektroda.com)
-
Initialization and persistence
- Plain channel values used as “logic registers” are volatile. Best practice is to set initial values in autoexec.bat, and, where needed, use channel type/flags that persist device settings. Community reports show that types and certain driver‑managed states survive reboots, while labels and some bindings don’t; plan to re‑apply what you need in autoexec. (elektroda.com)
-
Advanced scheduling and computed globals
- You can compute and store time‑related values in channels (e.g., seconds since midnight, delayed sunset time), then reuse those numbers across scripts and scheduled events. Recent updates support variable expansion in addClockEvent, and directly using a numeric time (TimerSeconds). This lets several scripts share the same computed times through shared channels. (elektroda.com)
Current information and trends
- Channels are explicitly documented as the variable mechanism, and they are floating‑point. Use $CHn anywhere you need a value. (elektroda.com)
- addClockEvent has evolved to accept multiple expansions and numeric “TimerSeconds,” enabling robust computed schedules (store inputs in channels, reference them in all scripts). (elektroda.com)
- The Charts driver lets you plot arbitrary variables/channels on‑device, making channels even more central as shared data points. (elektroda.com)
Supporting explanations and details
-
Minimal two‑script example
Script A (e.g., autoexec.bat or a button handler):
- setChannel 100 0
- alias set_night_mode backlog setChannel 100 1; echo NightMode=1
Script B (e.g., a sensor handler):
- addChangeHandler Channel5 == 1 if $CH100==1 then backlog led_dimmer 10; echo Motion at night
Rationale: CH100 is your global flag. Any script can set/read it with setChannel/$CH100. (elektroda.com)
-
Numeric calculations and conditionals
- setChannel 11 $CH10*1.8+32
- if $CH11>75 then fan_on
Expressions and system variables (e.g., $hour, $minute) are listed in constants.md; you can mix them with $CHn freely. (github.com)
-
Scheduling with shared globals
- Store a computed sunset+delay in CH10 once, then multiple scripts can use it:
- setChannel 10 $sunset+1800
- addClockEvent $CH10 0xff 4 turn_on_scene
This avoids recalculating scattered logic. (elektroda.com)
-
Hiding/labeling your “globals”
- To avoid cluttering UI and to document intent:
- setChannelLabel 100 "NightMode"
- setChannelPrivate 100 1
- setChannelVisible 100 0
These keep your logic channels available to scripts but out of the normal panel. (elektroda.com)
-
Observability and external access
Ethical and legal aspects
- If you expose the HTTP interface beyond your LAN, protect it (e.g., firewall, reverse proxy with auth). Channel writes control physical hardware; treat them as sensitive operations. (elektroda.com)
Practical guidelines
- Pick a reserved range for “logic channels” (e.g., 80–120) so you don’t collide with hardware‑mapped channels.
- Always initialize required globals in autoexec.bat; don’t assume their previous runtime values survived a reboot. (elektroda.com)
- Use setChannelLabel and setChannelPrivate to document and conceal internal state. (elektroda.com)
- For cross‑device “global” variables, use:
- MQTT (publish on Device A, subscribe on Device B),
- Tasmota Device Groups (UDP) for mirroring channel state on the LAN, which OpenBeken supports, including in the Windows simulator, or
- SendGET from one device to another’s /cm endpoint. (github.com)
- For time‑based coordination, compute once (e.g., CH10 = $hour3600+$minute60) and reuse across handlers and schedules. (elektroda.com)
Possible disclaimers or additional notes
- There is no C‑style variable declaration or a universal setConfig/getConfig primitive for ad‑hoc script variables; use channels as globals and autoexec.bat to re‑establish state. Consult the official constants and examples for supported expressions and commands. (github.com)
- Not all properties persist: community reports show some labels/bindings require reapplication at boot—plan accordingly. (elektroda.com)
- Avoid excessive flash writes; don’t try to persist fast‑changing counters.
Suggestions for further research
- Review constants and expressions you can use with $CHn (docs/constants.md). (github.com)
- Browse autoexec.bat examples for larger script patterns (docs/autoexecExamples.md). (github.com)
- Explore recent scheduling examples (addClockEvent, TimerSeconds) to structure multi‑script timing logic cleanly. (elektroda.com)
- If you need local visualization of shared variables, try the Charts driver tutorials. (elektroda.com)
Brief summary
- In OpenBeken, “global variables between two scripts” are simply channels. Set them with setChannel/addChannel and read them with $CHn; all scripts share the same values. Initialize what you need at boot in autoexec.bat, label/hide your logic channels, and consider driver/channel types or autoexec re‑init if you need persistence. For multi‑device sharing, use MQTT, Device Groups, or HTTP. (elektroda.com)
If you share your specific use case (what you need to pass, numeric range, persistence requirements), I can sketch the exact channel map and boot/init snippet.
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.