Saltar la navegación

Reglas

http://docs.openhab.org/configuration/rules-dsl.html#event-based-triggers

Sintaxis de una regla

rule "rule name"
when
    <TRIGGER CONDITION1> or
    <TRIGGER_CONDITION2> or
    <TRIGGER_CONDITION3>
    ...
then
    <SCRIPT_BLOCK>
end

Activadores basados en eventos:

Item <item> received command [<command>]
Item <item> received update [<state>]
Item <item> changed [from <state>] [to <state>]

https://github.com/openhab/openhab1-addons/wiki/Actions#sendcommand-vs-postupdate

Activadores basados en tiempo:

Time is midnight
Time is noon
Time cron "<cron expression>"
Cron-Expressions are used to configure instances of CronTrigger.
Cron-Expressions are strings that are actually made up of seven sub-expressions,
that describe individual details of the schedule.
These sub-expression are separated with white-space, and represent: Seconds Minutes Hours Day-of-Month Month Day-of-Week Year (optional field) An example of a complete cron-expression is the string “0 0 12 ? * WED”
which means “every Wednesday at 12:00:00 pm”.

El * es para indicar todos
El ? para omitir

http://www.quartz-scheduler.org/documentation/quartz-2.1.x/tutorials/tutorial-lesson-06

Generador de expresiones cron:

http://www.cronmaker.com/

Activadores basado en el sistema:

System started y System shuts down

rule "Speedtest init"
when
    System started
then
    createTimer(now.plusSeconds(30)) [|
        if (Speedtest_Summary.state == NULL || Speedtest_Summary.state == "") Speedtest_Summary.postUpdate("unknown")
    ]
end

Acciones:

https://github.com/openhab/openhab1-addons/wiki/Actions

if(Temperature.state < 20) {
    	sendCommand(Heating, ON)
    }

Ejemplo interesante:

La luz se enciende cuando hay movimiento detectado (por ej. se pulsa el interruptor o se abre la puerta) y el brillo está por debajo del umbral. Cada 1min se comprueba si ha habido movimiento desde el último chequeo. Si no es así: apague la luz.
var Number counter = 0
    var Number lastCheck = 0
    
    rule "corLightOn"
    when   
            Item corMotion changed from OFF to ON or
            Item corFrontDoor changed from CLOSED to OPEN
    then   
            counter = counter + 1
            if(corBright.state < 40) {
                    sendCommand(corLightCeil, ON)
            }
    end
    
    rule "corLightOff"
    when   
            Time cron "0 * * * * ?"
    then   
            if(lastCheck == counter) {
                    counter = 0
                    lastCheck = -1;
                    sendCommand(corLightCeil, OFF)
                    sendCommand(corMotion, OFF)
            } else {
                    lastCheck = counter
            }
    end