Jump to content
Shelly IoT Innovation Challenge started! ×

Script for a "staircase lighting" controlled via a virtual/enum


mschaeffler

Recommended Posts

  • Members
// Konstanten
let maxCyclic = 60 * 4;
let maxState = {
    "-": 0,
    "S": 90*4,
    "D": 5*60*4,
    "L": 60*60*4,
    "w": 1,
    "W": 12
};

// globale Variablen
let switchoffTimer = null;
let state          = Shelly.getComponentStatus( "enum", 200 ).value;
let cntState       = 0;
let cntCyclic      = 0;
let mqttPrefix     = Shelly.getComponentConfig( "mqtt" ).topic_prefix;

function showState()
{
    MQTT.publish( mqttPrefix+"/status/enum:200", "{\"value\":\""+state+"\"}", 1, false );
}

function setState(aState)
{
    state    = aState;
    cntState = maxState[aState];
    showState();
}

function checkState()
{
    if( state !== "-" )
    {
        cntState = cntState - 1;
        if( cntState <= 0 )
        {
            if( state === "S" || state === "D" || state === "L" )
            {
                switchWarn1();
            }
            else if( state === "w" )
            {
                switchWarn2();
            }
            else
            {
                Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
            }
        }
    }
}

function switchOff()
{
    print( "Output switched off" );
    Shelly.call( "Switch.Set", { id:0, on:false }, null, null );
    setState( "-" );
}

function switchOn(aState)
{
    print( "Output switched on ", aState );
    Shelly.call( "Switch.Set", { id:0, on:true }, null, null );
    setState( aState );
}

function switchWarn1()
{
    print( "Output switched warn 1" );
    Shelly.call( "Switch.Set", { id:0, on:false }, null, null );
    setState( "w" );
}

function switchWarn2()
{
    print( "Output switched warn 2" );
    Shelly.call( "Switch.Set", { id:0, on:true }, null, null );
    setState( "W" );
}

function shortPress()
{
    print( "short press" );
    if( state !== "L" )
    {
        switchOn( "S" );
    }
}

function doublePress()
{
    print( "double press" );
    if( state !== "L" )
    {
        switchOn( "D" );
    }
}

function longPress()
{
    print( "long press" );
    if( state !== "L" )
    {
        switchOn( "L" );
    }
    else
    {
        Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
    }
}

// Handler
Shelly.addEventHandler(
    function(event,ud)
    {
        //print( JSON.stringify(event) );
        if( event.name === 'input' && event.id === 0 )
        {
            print( "Input event", event.id );
            if( event.info.event === "single_push" )
            {
                Shelly.call( "Enum.Set", { id:200, value:"S" }, null, null );
            }
            else if( event.info.event === "double_push" )
            {
                Shelly.call( "Enum.Set", { id:200, value:"D" }, null, null );
            }
            else if( event.info.event === "long_push" )
            {
                Shelly.call( "Enum.Set", { id:200, value:"L" }, null, null );
            }
        }
    },
    null
);

Shelly.addStatusHandler(
    function(event,ud)
    {
        //print( JSON.stringify(event) );
        if( event.component === "enum:200" )
        {
            print(event.delta.value);
            if( event.delta.value === "S" )
            {
                shortPress();
            }
            else if( event.delta.value === "D" )
            {
                doublePress();
            }
            else if( event.delta.value === "L" )
            {
                longPress();
            }
            else if( event.delta.value === "-" )
            {
                switchOff();
            }
        }
    },
    null
);

// Main
if( state !== "-" )
{
    Shelly.call( "Enum.Set", { id:200, value:"-" }, null, null );
}
else
{
    showState();
}
Timer.set( 250, true,
    function(ud)
    {
        if( ++cntCyclic >= maxCyclic )
        {
            cntCyclic = 0;
            showState();
        }
        checkState();
    },
    null
);

 

Link to comment
Share on other sites

  • Members

the main idea is this:

  • a short press on the button or sending "S" to the enum starts a 4min timer and then a warning (short off and some additional time on)
  • two pressed or "D" a longer timer
  • and long press or "L" light for one hour.

I have a similar script running on two gen2 devices and just changed the communication to the control system in a RPC style with the virtual enum.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...