ALLTERCO / shelly-script-examples

Shelly Scripts for Gen2 Shelly devices
Apache License 2.0
209 stars 57 forks source link

List of allowed mac-address #125

Open umbertoB67 opened 1 week ago

umbertoB67 commented 1 week ago

I've 4 Shelly BLU Button Tough1. With my script, I need to trigger only if the pression arrives from of two of them. In the script below I can only bind one button. How can I bind more than one button by mac-address? In the action can I pass the mac-address of the button that has been pressed?

let CONFIG = {
    // shelly_blu_name_prefix: "SBBT",
    shelly_blu_address: "bc:02:6e:c3:c8:b9",

    actions: [
        {
            cond: { Button: 1 },
            action: onButtonPress_1
        },

        {
            cond: { Button: 2 },
            action: onButtonPress_2
        },

        {
            cond: { Button: 3 },
            action: onButtonPress_3
        },

        {
            cond: { Button: 4 },
            action: onButtonPress_4
        },
    ],
};
NoUsername10 commented 1 week ago

Perhaps something similar?

let CONFIG = {
    // List of MAC addresses for allowed buttons
    allowed_buttons: ["bc:02:6e:c3:c8:b9", "bc:02:6e:d4:e5:f6"],

    // Define actions for button presses
    actions: [
        {
            cond: { Button: 1 },
            action: onButtonPress_1
        },
        {
            cond: { Button: 2 },
            action: onButtonPress_2
        },
        {
            cond: { Button: 3 },
            action: onButtonPress_3
        },
        {
            cond: { Button: 4 },
            action: onButtonPress_4
        },
    ],
};

// Function to handle button presses
function onButtonEvent(mac, buttonNumber) {
    let isAllowed = false;

    // Manually check if the MAC address is in the allowed list
    for (let i = 0; i < CONFIG.allowed_buttons.length; i++) {
        if (CONFIG.allowed_buttons[i] === mac) {
            isAllowed = true;
            break;
        }
    }

    if (!isAllowed) {
        print("Ignoring button press from unknown MAC:", mac);
        return;
    }

    // Find the action based on the button number
    let action = null;
    for (let i = 0; i < CONFIG.actions.length; i++) {
        if (CONFIG.actions[i].cond.Button === buttonNumber) {
            action = CONFIG.actions[i];
            break;
        }
    }

    if (action && action.action) {
        action.action(mac); // Pass the MAC address to the action function
    } else {
        print("No action defined for button:", buttonNumber);
    }
}

// Example action functions
function onButtonPress_1(mac) {
    print("Button 1 pressed from device:", mac);
}

function onButtonPress_2(mac) {
    print("Button 2 pressed from device:", mac);
}

function onButtonPress_3(mac) {
    print("Button 3 pressed from device:", mac);
}

function onButtonPress_4(mac) {
    print("Button 4 pressed from device:", mac);
}
umbertoB67 commented 1 week ago

Hi @NoUsername10, I got this error:

Uncaught Error: Function "includes" not found! at if (!CONFIG.allowed_buttons.includes(mac)) {

NoUsername10 commented 1 week ago

Sry, chatGPT can be so stupid sometimes, include is not supported in Shelly Scripting. it doesn't matter how many times you explain sometimes.

The code is updated above.

umbertoB67 commented 1 week ago

The new script works well, @NoUsername10 thank you very much. But how can I accept only the inputs that come from the Blue Buttons? Now the message "Ignoring button press from unknown MAC" is printed even when opening the contact of a Shelly BLU Door Window Sensor. If in CONFIG I add shelly_blu_name_prefix: "SBBT" it no longer detects any events, not even those of the Buttons.

NoUsername10 commented 6 days ago

I don think the device name i correct perhaps? I don't know how the CONFIG works, so I'm guessing now.

Device name: Shelly BLU Button Tough 1 Device model: SBBT-002C Device Bluetooth ID: 0x0001

*Edit I tried every possible combination, but could not get shelly_blu_name_prefix: "SBBT" to work.

I suggest commenting out: print("Ignoring button press from unknown MAC:", mac); The script always start to check the device mac, so it will start anyway from what i know, you only see the log. But in absolutely not an expert :-)