WebThingsIO/webthing-node

How to create a push button

Closed this issue · 2 comments

apiel commented

I try to create a push button but I can't figure out how it work:

function makeThing() {
    const thing = new Thing(
        'urn:dev:ops:my-switchbot-1234',
        'Switchbot',
        ['PushButton'],
        'Trigger Switchbot',
    );

    thing.addProperty(
        new Property(
            thing,
            'pushed',
            new Value(true, (update) => {
                console.log(`change: ${update}`);
                if (update === 'pushed') {
                    switchbot.press();
                }
            }),
            {
                '@type': 'PushedProperty',
                title: 'Switchbot',
                type: 'boolean',
                description: 'Press switchbot',
            },
        ),
    );
    return thing;
}

Right now, I get:
image

But clicking on the icon doesn't trigger any event... I just need a sinple push button, with a sinle press (no on/off).

How can fix this? Is there some example somewhere? Is there better documentation than https://iot.mozilla.org/schemas/ ?

A PushButton refers to an actual hardware push button. You would not control such a device through software, so the PushedProperty is always treated as read-only by the UI. What you want is an OnOffSwitch. Your web thing could treat it as a momentary switch that would switch back off after some short time period.

And for such a case, a 1 second "pulse" using the existing Pulse Add-on may work well for you.

Here is an image of the options for a 1-second pulse called "switch" when viewed as an input for a rule.
Screen Shot 2020-07-26 at 9 58 02 PM