Git Product home page Git Product logo

openhab2-javascript's Introduction

openHAB 2.x: JSR223 JavaScript Code

This is a repository of very experimental JavaScript code that can be used with the SmartHome platform and openHAB 2.x.

Applications

The JSR223 scripting extensions can be used for general scripting purposes, including defining rules like in openHAB 1.x. So openHAB 1.x JavaScript Code can be migrated from openHAB 1 JSR223 binding to openHAB 2 automation.

Defining Rules

One the primary use cases for the JSR223 scripting is to define rules for the Eclipse SmartHome (ESH) rule engine.

Rules: Raw ESH API

Using the raw ESH API, the simplest rule definition would look something like:

'use strict';
se.importPreset("RuleSupport");
se.importPreset("RuleSimple");

var sRule = new SimpleRule(){
    execute: function( module, input){
        print("################ module:", module);
        events.postUpdate(ir.getItem("testItemSwitch"), ON);
        events.sendCommand(ir.getItem("testItemSwitch"), OFF);
    }
};

sRule.setTriggers([
        new Trigger(
            "aTimerTrigger", 
            "timer.GenericCronTrigger", 
            new Configuration({
                "cronExpression": "0/15 * * * * ?"
            })
        )
    ]);

automationManager.addRule(sRule);

This can be simplified with some extra JavaScript Code, found in jslib/JSRule.js:

'use strict';
load('./../conf/automation/jsr223/jslib/JSRule.js');

JSRule({
    name: "My JS Rule",
    description: "Line:"+__LINE__,
    triggers: [
        TimerTrigger("0/15 * * * * ?")//Enable/Disable Rule
    ],
    execute: function( module, input){
        print("################ module:", module);
        events.postUpdate(ir.getItem("testItemSwitch"), ON);
        events.sendCommand(ir.getItem("testItemSwitch"), OFF);
    }
});

jslib/helper.js contains more simplifying and helping functions.

jslib/PersistenceExtensions.js contains more simplifying PersistenceExtensions functions.

jslib/triggersAndConditions.js contains trigger functions.

ActionExamples.js contains examples for default actions like PersistenceExtensions, HTTP, Ping, Audio, Voice, ThingAction.

openhab2-javascript's People

Contributors

lewie avatar aruder77 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.