lzoli Posted May 23 Share Posted May 23 Hi People, I would like to ask your help. I have two shelly rgbw2-s in white mode (4 channel white) and a shelly i4 and have been trying to remote control the rgbw2 group from the i4 without luck. What I try to do is to remote turn on/off 3 channels out of 4 and do other functions as well like dimming, but I have a problem I can't figure out. Here is the script, the original I found online and rewritten a little: /** * @copyright shelly-tools contributors * @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html) * * This script is intended to control a Shelly RGBW2 with a single button from * a Shelly Plus device. Every time the button from the Plus device is pushed it jumps to the next step * defined in the sequence array. */ // CONFIG START // IP address or hostname(s) from RGBW2 device(s) let REMOTE = { group: ["192.168.0.220","192.168.0.208"], channel: ["0","1","2"], input: 2, // ID from the push button: 0 for Shelly Plus 1 / Plus 1 PM or 0,1,2 or 3 for the Shelly i4. }; // Dimming level let dimsequence = [ '?brightness=65', // 65% '?brightness=33', // 33% '?brightness=11', // 11% '?brightness=2', // 2% '?brightness=100', // 100% ]; // CONFIG END // no need to change anything below this line.. let dimpos = 0; // add an evenHandler for button type input and various push events Shelly.addEventHandler( function (event, user_data) { //print(JSON.stringify(event)); if (typeof event.info.event !== 'undefined') { if (event.info.event === 'double_push' && event.info.id === REMOTE.input) { for (let i=0; i<REMOTE.group.length; i++) { for (let j=0; j<REMOTE.channel.length; j++) { setRGBW(REMOTE.group[i], REMOTE.channel[j]); } } dimpos = 0; } else if (event.info.event === 'single_push' && event.info.id === REMOTE.input) { for (let i=0; i<REMOTE.group.length; i++) { for (let j=0; j<REMOTE.channel.length; j++) { toggleRGBW(REMOTE.group[i], REMOTE.channel[j]); } } } else if (event.info.event === 'long_push' && event.info.id === REMOTE.input) { for (let i=0; i<REMOTE.group.length; i++) { for (let j=0; j<REMOTE.channel.length; j++) { dimRGBW(REMOTE.group[i], REMOTE.channel[j], dimpos); } } dimpos++; if (dimsequence.length === dimpos) { dimpos = 0; } } else { return true; } } else { return true; } }, ); // send RBGW on command function setRGBW(group, channel) { Shelly.call( "http.get", { url: 'http://' + group + '/white/' + channel + '?turn=on&brightness=100' }, function (response, error_code, error_message, ud) { }, null ); }; // send RBGW dimming command function dimRGBW(group, channel, dimpos) { Shelly.call( "http.get", { url: 'http://' + group + '/white/' + channel + dimsequence[dimpos] }, function (response, error_code, error_message, ud) { print(dimsequence[dimpos]); }, null ); }; // send RGBW toggle command function toggleRGBW(group, channel) { Shelly.call( "http.get", { url: 'http://' + group + '/white/' + channel + '?turn=toggle' }, function (response, error_code, error_message, ud) { }, null ); } If I only control 2 channels out of 3 on both of my shelly devices or I control only 1 shelly device and 3 channel on it, works fine, does what I ask from the rgbw2 controller. If I want to control 3 or 4 channels on both of my shelly bgbw devices, my script crashes with the following error: shelly_notification:209 Event from input:2: {"component":"input:2","id":2,"event":"btn_down","ts":1714545421.30} 08:37:01 shelly_notification:209 Event from input:2: {"component":"input:2","id":2,"event":"btn_up","ts":1714545421.53} 08:37:01 in function called from system 08:37:02 shelly_user_script.:388 error: Error in EjsCall 08:37:02 shelly_notification:209 Event from input:2: {"component":"input:2","id":2,"event":"single_push","ts":1714545421.80} 08:37:02 shos_rpc_inst.c:230 http.get via loopback 08:37:02 shelly_http_client.:306 0x3ffde5cc: HTTP GET http://192.168.0.208/white/0?turn=toggle 08:37:02 shos_rpc_inst.c:230 http.get via loopback 08:37:02 shelly_http_client.:306 0x3ffe1df0: HTTP GET http://192.168.0.208/white/1?turn=toggle 08:37:02 shelly_notification:163 Status change of script:2: {"id":2,"errors":["error"],"running":false} 08:37:02 shos_rpc_inst.c:230 http.get via loopback 08:37:02 shelly_http_client.:306 0x3ffe2220: HTTP GET http://192.168.0.208/white/2?turn=toggle 08:37:02 shos_rpc_inst.c:230 http.get via loopback 08:37:02 shelly_http_client.:306 0x3ffe3058: HTTP GET http://192.168.0.220/white/0?turn=toggle 08:37:02 shos_rpc_inst.c:230 http.get via loopback 08:37:02 shelly_http_client.:306 0x3ffe32ac: HTTP GET http://192.168.0.220/white/1?turn=toggle 08:37:02 shelly_http_client.:606 0x3ffe2220: Finished; bytes 295, code 200, redir 0/3, auth 0, status OK 08:37:02 shelly_ejs_rpc.cpp:77 No call record for 130 08:37:02 shelly_http_client.:606 0x3ffe32ac: Finished; bytes 295, code 200, redir 0/3, auth 0, status OK 08:37:02 shelly_ejs_rpc.cpp:77 No call record for 132 08:37:02 shelly_http_client.:606 0x3ffe3058: Finished; bytes 295, code 200, redir 0/3, auth 0, status OK 08:37:04 shelly_ejs_rpc.cpp:77 No call record for 131 08:37:04 shelly_http_client.:606 0x3ffe1df0: Finished; bytes 295, code 200, redir 0/3, auth 0, status OK 08:37:04 shelly_http_client.:606 0x3ffde5cc: Finished; bytes 295, code 200, redir 0/3, auth 0, status OK 08:37:04 shelly_ejs_rpc.cpp:77 No call record for 129 08:37:04 shelly_ejs_rpc.cpp:77 No call record for 128 08:37:04 As you see, the script fails to execute http://192.168.0.220/white/2?turn=toggle and just fails and exits instead. I really can't figure what I'm doing wrong or how I should do it better to stop the script crashing on the i4 and control that last channel on the rgbw2. Anyone could give me any clue? Many thanks! Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish Link to comment Share on other sites More sharing options...
johann.pascher Posted August 26 Share Posted August 26 Your script looks well-structured and handles different button press events to control a Shelly RGBW2 device from a Shelly Plus device. Here’s a detailed review and some recommendations to ensure it works as expected: ### Review and Recommendations 1. **Event Handling**: The event handler differentiates between `double_push`, `single_push`, and `long_push`, which is correct. Ensure the `REMOTE.input` value matches the actual input ID for the button you're using. 2. **Function Implementations**: - **`setRGBW`**: This function sends a request to turn on the RGBW device with full brightness. - **`dimRGBW`**: This function adjusts the brightness based on the `dimsequence` array. - **`toggleRGBW`**: This function toggles the RGBW device on or off. 3. **Error Handling**: Currently, the callback functions in `Shelly.call` do not handle errors or log responses. Adding error handling and logging can help with debugging. 4. **Variable Naming**: The variable `dimpos` represents the current position in the `dimsequence` array. It’s used to cycle through different brightness levels. This is managed well in your script. 5. **Edge Cases**: Consider adding checks to ensure that `dimpos` is always within bounds and handle any potential issues with the `REMOTE.group` or `REMOTE.channel` values. 6. **Efficiency**: Your current setup sends commands to all groups and channels, which is fine if that’s your intention. If you need to optimize or limit actions to specific groups or channels, you might need additional logic. ### Revised Script with Improvements Here’s an improved version of your script with added error handling and comments for clarity: ```javascript /** * @copyright shelly-tools contributors * @license GNU Affero General Public License (https://www.gnu.org/licenses/agpl-3.0.de.html) * * This script is intended to control a Shelly RGBW2 with a single button from * a Shelly Plus device. Every time the button from the Plus device is pushed it jumps to the next step * defined in the sequence array. */ // CONFIG START let REMOTE = { group: ["192.168.0.220","192.168.0.208"], channel: ["0","1","2"], input: 2, // ID from the push button }; let dimsequence = [ '?brightness=65', // 65% '?brightness=33', // 33% '?brightness=11', // 11% '?brightness=2', // 2% '?brightness=100', // 100% ]; // CONFIG END let dimpos = 0; Shelly.addEventHandler(function (event, user_data) { if (event.info && event.info.event) { if (event.info.event === 'double_push' && event.info.id === REMOTE.input) { for (let i = 0; i < REMOTE.group.length; i++) { for (let j = 0; j < REMOTE.channel.length; j++) { setRGBW(REMOTE.group[i], REMOTE.channel[j]); } } dimpos = 0; } else if (event.info.event === 'single_push' && event.info.id === REMOTE.input) { for (let i = 0; i < REMOTE.group.length; i++) { for (let j = 0; j < REMOTE.channel.length; j++) { toggleRGBW(REMOTE.group[i], REMOTE.channel[j]); } } } else if (event.info.event === 'long_push' && event.info.id === REMOTE.input) { for (let i = 0; i < REMOTE.group.length; i++) { for (let j = 0; j < REMOTE.channel.length; j++) { dimRGBW(REMOTE.group[i], REMOTE.channel[j], dimpos); } } dimpos = (dimpos + 1) % dimsequence.length; // Cycle through the dimsequence } else { print("Unhandled event: " + JSON.stringify(event)); } } else { print("Invalid event: " + JSON.stringify(event)); } }); // Function to send RGBW on command function setRGBW(group, channel) { Shelly.call("http.get", { url: 'http://' + group + '/white/' + channel + '?turn=on&brightness=100' }, function (response, error_code, error_message) { if (error_code !== 0) { print("Error in setRGBW: " + error_message); } }); } // Function to send RGBW dimming command function dimRGBW(group, channel, dimpos) { Shelly.call("http.get", { url: 'http://' + group + '/white/' + channel + dimsequence[dimpos] }, function (response, error_code, error_message) { if (error_code !== 0) { print("Error in dimRGBW: " + error_message); } else { print("Dimming command sent: " + dimsequence[dimpos]); } }); } // Function to send RGBW toggle command function toggleRGBW(group, channel) { Shelly.call("http.get", { url: 'http://' + group + '/white/' + channel + '?turn=toggle' }, function (response, error_code, error_message) { if (error_code !== 0) { print("Error in toggleRGBW: " + error_message); } }); } ``` ### Key Changes: - **Error Handling**: Added logging for error messages in `setRGBW`, `dimRGBW`, and `toggleRGBW` functions. - **Dim Position Cycling**: Simplified the cycling of `dimpos` to ensure it stays within the bounds of `dimsequence`. - **Event Validation**: Added validation and logging for unhandled or invalid events. This should help make your script more robust and easier to debug. Make sure to test thoroughly and adjust any configuration as needed for your specific setup. Quote Translate Revert translation? English (American) Finnish French German Italian Portuguese (European) Spanish Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.