I would like to add the following code block to the playground: Parallel Switching Logic for Relays and Lights #29
Replies: 2 comments 5 replies
-
This is great. A good addition. There is nobody available to massage your content, but can you follow the directions here? https://github.com/tasmota/Berry_playground#contributing If you have never done a fork/PR on a repo this is a good way to learn. There are many tutorial pages out there on how to do a PR on git(hub). All you have to do is take the relevant parts of what you have written above and make it into a readme page in the "tutorials and code blocks" as part of a PR and bob's your uncle I can merge it. https://github.com/tasmota/Berry_playground/tree/main/tutorial-code-blocks/virtualMembers Here is an example. When you fork the repo you could just duplicate this folder/readme page and then edit to suit. This is also an opportunity to learn a little markdown. For code blocks you use triple back ticks before and after (single ticks for inline). Refer to any markdown syntax cheetsheat, and as I metioned if you use Any problems with making a contribution just ask here in this thread. You contribution will be appreciated! I'm on vacation next two weeks but will be able to assist/merge as time permits. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
First off I'd like to apologize for incorrectly trying to add this to the repository, I didn't understand the proper procedure.
User 'sfromis' was kind enough to write this code block to help me solve a technical issue and better understand how Berry works.
I believe this code could be very helpful to others as it was very helpful to me, so I think it should be posted here for everyone to benefit from.
This short script allows relays (or lights) connected to an ESP32 running Tasmota to be "linked together" in order to create automated parallel switching logic.
Parallel switching is useful for all kinds of electrical control scenarios where you would need to have multiple outputs activated simultaneously and need to assign the task to the micro-controller rather than have external 3rd party automation software perform this task via mqtt. Having the micro-controller perform this switching task locally is faster and more reliable than having it performed externally by 3rd party automation software.
Specifically, this script was written to control 5 relays, but can be edited for other configurations.
What this script does is to monitor the power-state of relays 1 through 4, and if any of them are on, then relay 5 is also automatically turned on.
Likewise, if any of the relays 1 through 4 are turned off, then relay 5 is also turned off.
Also, if relays 1-4 are off, then relay 5 can not be turned on, if you try to turn it on while relays 1-4 are off, then it will immediately turn off.
Likewise, if relays 1-4 are on, thus relay 5 is on, then relay 5 can not be turned off. If you try to turn relay 5 off while relays 1-4 are on, it will immediately turn back on.
Here is the script...
Here is a breakdown of what each line of code is doing:
First line defines a class, and last line instantiates the class to create an object.
The function of init() is initialization code for when the class is instantiated (creating an object).
The tasmota.add_driver allows the class instance to receive callbacks from Tasmota in many different situations.
set_power_handler() is just one of those, and it is only to be notified when a power status change happens.
tasmota.get_power() accesses a list of power states for all channels.
Index numbers in Berry starts with 0, and with 1 in Tasmota outside of Berry. Hence pow(2) matches Power3 etc.
tasmota.set_power() simply changes the state of one power channel.
Tricky thing is using the tasmota.set_timer(0, .... The reason is that the set_power_handler callback is not allowed to change any states, therefor the set_power call is deferred for 0 microseconds, meaning after exit from set_power_handler.
Berry code entered in the Berry REPL console is not saved anywhere. You can create a file autoexec.be with Berry code to be loaded after boot. When you use the console, the new code will in most cases supersede the old, but when using tasmota.add_driver the old iteration will persist. You can either reboot, build Tasmota with a development option providing a Berry restart button in the Berry REPL console, or you can change the init() method to:
This allows the driver to remove the old version of itself, and then register the new instance.
The real-world use case:
I have built a custom irrigation control box to control 4 irrigation zone valves.
Relays 1-4 control the zone valves.
Relay 5 controls the 24 VAC transformer which energizes the zone valve solenoids.
Even when not under load, the 24 VAC transformer gets warm, and I want to keep that transformer powered off until it is actually needed in order to reduce heat buildup inside the control box and help protect the other components and extend their lifespan. Thus the need for the parallel switching logic.
If someone could please help me in getting this posted to the playground, I would appreciate it.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions