Zeitschaltuhr

Für welche Projekte verwendet Ihr OpenHAB? Was habt Ihr automatisiert? Stellt eure Projekte hier vor.

Moderatoren: Cyrelian, seppy

Antworten
ThomasW
Beiträge: 33
Registriert: 9. Jan 2021 16:12
Answers: 1

Zeitschaltuhr

Beitrag von ThomasW »

Hallo zusammen,

Ich habe hier ein klein wenig gebastelt und mir eine Zeitschaltuhr zusammengebaut. Diese möchte ich euch gerne zur Verfügung stellen.

Zum einen hier das Widget zum einstellen der Zeit: Die Zeit wird in ein Item Zeit als String geschrieben.

Code: Alles auswählen

uid: TimeController
tags: []
props:
  parameters: []
  parameterGroups: []
timestamp: Apr 10, 2021, 8:22:12 PM
component: f7-card
slots:
  default:
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
      slots:
        default:
          - component: Label
            config:
              text: "Bitte wähle einen Tag aus:"
              style:
                font-weight: 600
                margin-top: 15px
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
      slots:
        default:
          - component: f7-segmented
            config:
              strong: false
              style:
                width: 450px
                margin-top: 5px
            slots:
              default:
                - component: oh-button
                  config:
                    text: Aus
                    action: variable
                    actionVariable: day
                    actionVariableValue: 1
                    disabled: =(vars.day === 1)
                    textColor: "=(vars.day === 1) ? 'green' : 'red'"
                    style:
                      font-weight: 400
                - component: oh-button
                  config:
                    text: Heute
                    action: variable
                    actionVariable: day
                    actionVariableValue: 2
                    disabled: =(vars.day === 2)
                    textColor: "=(vars.day === 2) ? 'green' : 'red'"
                    style:
                      font-weight: 400
                - component: oh-button
                  config:
                    text: Morgen
                    action: variable
                    actionVariable: day
                    actionVariableValue: 3
                    disabled: =(vars.day === 3)
                    textColor: "=(vars.day === 3) ? 'green' : 'red'"
                    style:
                      font-weight: 400
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
      slots:
        default:
          - component: Label
            config:
              text: "Bitte wähle eine Zeit aus:"
              style:
                font-weight: 600
                margin-top: 15px
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 5px
      slots:
        default:
          - component: oh-input
            config:
              type: time
              defaultValue: 00:00
              validate: true
              step: 60
              min: 00:00
              max: 23:59
              required: true
              variable: time
              disabled: =(vars.day === 1)
              bgColor: "=(vars.day === 1) ? 'red' : 'white'"
              textColor: black
            slots: {}
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 20px
          margin-buttom: 20px
      slots:
        default:
          - component: oh-button
            config:
              raised: true
              text: Einstellen
              action: command
              actionItem: Zeit
              actionCommand: =  vars.time + ' , ' + vars.day
              actionFeedback: ok
Screenshot 2021-04-10 155326.jpg
Innerhalb einer Rule welche bei Veränderung des Items ausgelöst wird führe ich folgendes JavaScript aus:

Code: Alles auswählen

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var DateTime   = Java.type("java.time.ZonedDateTime");
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");


var timeH = itemRegistry.getItem('Zeit').getState().toString().split(' , ')[0].split(':')[0];  
var timeM = itemRegistry.getItem('Zeit').getState().toString().split(' , ')[0].split(':')[1];  
var mode = itemRegistry.getItem('Zeit').getState().toString().split(' , ')[1]; 

this.timer = (this.timer === undefined) ? null : this.timer;

switch(mode) {
    case "1": {
      this.timer.cancel(); //Timer abbrechen 
      this.timer = null;
      break;
    } 
    
    case "2": { // Heute
       if(berechnung()>0){ // Eingestellte Zeit liegt in der Zukunpft
         if (timer === null){
           this.timer = ScriptExecution.createTimer(DateTime.now().plusMinutes(berechnung()), kaffee);
          }else{
            this.timer.reschedule(DateTime.now().plusMinutes(berechnung()));
          }
        }else{
          logger.warn("Die Zeit ist bereits abgelaufen");
        }
      break;
    }
    
    case "3": { //Morgen
       if(berechnung()+1440>0){ 
         if (timer === null){
           this.timer = ScriptExecution.createTimer(DateTime.now().plusMinutes(berechnung()+1440), kaffee);
          }else{
            this.timer.reschedule(DateTime.now().plusMinutes(berechnung()+1440));
          }
        }else{
          logger.warn("Die Zeit ist bereits abgelaufen");
        }
      break;
    }
      
}


//Funktion welche im Timer aufgerufen wird hier entsprechend die Anweisungen hinterlegen
function kaffee(){
  //logger.info("Timer ist abgelaufen"); 
  events.sendCommand('aKaffeemaschiene_Schalter', 'ON'); //Schaltbefehl
  events.sendCommand('Zeit', '00:00 , 1'); // Zeitschaltung wieder ausschalten
}

function berechnung(){
  var i = 0;
  i = timeM - DateTime.now().getMinute();
  i = i + ((timeH - DateTime.now().getHour())*60);
  return i;
}
Hier arbeite ich mit einem Timer welcher entsprechend auf die passende Uhrzeit gestellt wird.
Da ich die Zeitschaltuhr nur benötige um damit meine Kaffeemaschine zu einer vorgegebenen Uhrzeit einzuschalten reicht mir die Funktionalität für 2 Tage (Heute/Morgen).

schöne Grüße
Thomas
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.
OH3 auf RPI4,
Heizungssteuerung: Fritz DECT,
Zigbee: Conbee2 über Phoscon-GW: Aqara Opple Schalter, Aqara Temperatursensoren, Aqara Fenstersensoren, Aqara Aktoren, IKEA Lichter
Astro Binding
DWDUnwetter Binding
OpenWeatherMap Binding
FritzBox TR 064 Binding
iRobot Binding
Telegramm Binding für Benachrichtigungen und Steuerung von unterwegs
Rolladensteuerung: Rademacher DuoFern über HTTP Binding

ThomasW
Beiträge: 33
Registriert: 9. Jan 2021 16:12
Answers: 1

Re: Zeitschaltuhr

Beitrag von ThomasW »

Habe das ganze überarbeitet und die Fehler welche ich im Betrieb gefunden habe beseitigt Beitrag aktuallisiert:

Mit etwas Basteln habe ich eine Zeitschaltuhr mit Wochenprogramm gebastelt. Hier kann für jeden Wochentag 2 Schaltzeitpunkte eingestellt werden (AN/AUS), auch ist es möglich einzelne Wochentage auszuschalten.

das Ganze Widget sieht so aus:
Screenshot 2021-04-16 140159.jpg
der code dazu ist etwas komplizierter

Code: Alles auswählen

uid: TimeControllerWeek
tags: []
props:
  parameters:
    - context: item
      description: An item to control
      label: Item
      name: item
      required: false
      type: TEXT
  parameterGroups: []
timestamp: Apr 22, 2021, 10:17:23 AM
component: f7-card
slots:
  default:
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
      slots:
        default:
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: " "
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Mo:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Di:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Mi:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Do:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Fr:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Sa:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "So:"
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 20px
          margin-buttom: 20px
      slots:
        default:
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Ein:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[0].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: moEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[1].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: diEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[2].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: miEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[3].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: doEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[4].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: frEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[5].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: saEin
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[6].split(',')[0])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: soEin
                    bgColor: white
                    textColor: black
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 20px
          margin-buttom: 20px
      slots:
        default:
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Aus:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[0].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: moAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[1].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: diAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[2].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: miAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[3].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: doAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[4].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: frAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[5].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: saAus
                    bgColor: white
                    textColor: black
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: oh-input
                  config:
                    type: time
                    defaultValue: =(items[props.item].state.split(';')[6].split(',')[1])
                    validate: true
                    step: 60
                    min: 00:00
                    max: 23:59
                    required: true
                    variable: soAus
                    bgColor: white
                    textColor: black
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 20px
          margin-buttom: 20px
      slots:
        default:
          - component: f7-col
            config:
              style:
                margin-right: 10px
            slots:
              default:
                - component: Label
                  config:
                    text: "Aktiv:"
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: moAktiv
                          actionVariableValue: "0"
                          disabled: "=(vars.moAktiv) ?  (vars.moAktiv === '0') : ((items[props.item].state.split(';')[0].split(',')[2]) === '0')  "
                          textColor: "=(vars.moAktiv) ? ( (vars.moAktiv === '0') ? 'green' : 'red') : (((items[props.item].state.split(';')[0].split(',')[2]) === '0') ? 'green' : 'red')"
                          active: "=(vars.moAktiv) ?  (vars.moAktiv === '0') : ((items[props.item].state.split(';')[0].split(',')[2]) === '0')  "
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: moAktiv
                          actionVariableValue: 1
                          disabled: "=(vars.moAktiv) ?  (vars.moAktiv === 1) : ((items[props.item].state.split(';')[0].split(',')[2]) === '1')  "
                          textColor: "=(vars.moAktiv) ? ( (vars.moAktiv === 1) ? 'green' : 'red') : (((items[props.item].state.split(';')[0].split(',')[2]) === '1') ? 'green' : 'red')"
                          active: "=(vars.moAktiv) ?  (vars.moAktiv === 1) : ((items[props.item].state.split(';')[0].split(',')[2]) === '1')  "
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: diAktiv
                          actionVariableValue: "0"
                          active: "=(vars.diAktiv) ?  (vars.diAktiv === '0') : ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[1].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.diAktiv) ?  (vars.diAktiv === '0') : ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[1].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.diAktiv) ? ( (vars.diAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[1].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: diAktiv
                          actionVariableValue: 1
                          active: "=(vars.diAktiv) ?  (vars.diAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[1].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.diAktiv) ?  (vars.diAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[1].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.diAktiv) ? ( (vars.diAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[1].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: miAktiv
                          actionVariableValue: "0"
                          active: "=(vars.miAktiv) ?  (vars.miAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[2].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.miAktiv) ?  (vars.miAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[2].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.miAktiv) ? ( (vars.miAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[2].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: miAktiv
                          actionVariableValue: 1
                          active: "=(vars.miAktiv) ?  (vars.miAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[2].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.miAktiv) ?  (vars.miAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[2].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.miAktiv) ? ( (vars.miAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[2].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: doAktiv
                          actionVariableValue: "0"
                          active: "=(vars.doAktiv) ?  (vars.doAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[3].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.doAktiv) ?  (vars.doAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[3].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.doAktiv) ? ( (vars.doAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[3].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: doAktiv
                          actionVariableValue: 1
                          active: "=(vars.doAktiv) ?  (vars.doAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[3].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.doAktiv) ?  (vars.doAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[3].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.doAktiv) ? ( (vars.doAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[3].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: frAktiv
                          actionVariableValue: "0"
                          active: "=(vars.frAktiv) ?  (vars.frAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[4].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.frAktiv) ?  (vars.frAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[4].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.frAktiv) ? ( (vars.frAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[4].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: frAktiv
                          actionVariableValue: 1
                          active: "=(vars.frAktiv) ?  (vars.frAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[4].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.frAktiv) ?  (vars.frAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[4].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.frAktiv) ? ( (vars.frAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[4].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          small: true
                          text: Aus
                          action: variable
                          actionVariable: saAktiv
                          actionVariableValue: "0"
                          active: "=(vars.saAktiv) ?  (vars.saAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[5].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.saAktiv) ?  (vars.saAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[5].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.saAktiv) ? ( (vars.saAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[5].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          small: true
                          text: Ein
                          action: variable
                          actionVariable: saAktiv
                          actionVariableValue: 1
                          active: "=(vars.saAktiv) ?  (vars.saAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[5].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.saAktiv) ?  (vars.saAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[5].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.saAktiv) ? ( (vars.saAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[5].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
          - component: f7-col
            config:
              style:
                margin-right: 10px
                white: 50%
            slots:
              default:
                - component: f7-segmented
                  config:
                    strong: true
                    style:
                      width: 100%
                      margin-top: 5px
                  slots:
                    default:
                      - component: oh-button
                        config:
                          text: Aus
                          small: true
                          action: variable
                          actionVariable: soAktiv
                          actionVariableValue: "0"
                          active: "=(vars.soAktiv) ?  (vars.soAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[6].split(',')[2]) === '0') : false)  "
                          disabled: "=(vars.soAktiv) ?  (vars.soAktiv === '0') :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[6].split(',')[2]) === '0') : false)  "
                          textColor: "=(vars.soAktiv) ? ( (vars.soAktiv === '0') ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[6].split(',')[2]) === '0') ? 'green' : 'red')"
                          style:
                            width: 50%
                      - component: oh-button
                        config:
                          text: Ein
                          small: true
                          action: variable
                          actionVariable: soAktiv
                          actionVariableValue: 1
                          active: "=(vars.soAktiv) ?  (vars.soAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[6].split(',')[2]) === '1') : false)  "
                          disabled: "=(vars.soAktiv) ?  (vars.soAktiv === 1) :  ((items[props.item].state.split(';')[0].split(',')[2] != NULL) ? ((items[props.item].state.split(';')[6].split(',')[2]) === '1') : false)  "
                          textColor: "=(vars.soAktiv) ? ( (vars.soAktiv === 1) ? 'green' : 'red') : (items[props.item].state.split(';')[0].split(',')[2] == NULL) ? 'red'  : (((items[props.item].state.split(';')[6].split(',')[2]) === '1') ? 'green' : 'red')"
                          style:
                            width: 50%
    - component: f7-row
      config:
        class:
          - justify-content-center
          - align-items-center
          - text-align-center
        style:
          margin-top: 20px
          margin-buttom: 20px
      slots:
        default:
          - component: oh-button
            config:
              raised: true
              text: Einstellen
              action: command
              actionItem: =(props.item)
              actionFeedback: ok
              actionCommand: "= [(vars.moEin === NULL) ? (items[props.item].state.split(';')[0].split(',')[0]) : (vars.moEin)] + ',' + [(vars.moAus === NULL) ? (items[props.item].state.split(';')[0].split(',')[1]) : (vars.moAus)] + ',' + [(vars.moAktiv === NULL) ? (items[props.item].state.split(';')[0].split(',')[2]) : (vars.moAktiv)] + ';' + [(vars.diEin === NULL) ? (items[props.item].state.split(';')[1].split(',')[0]) : (vars.diEin)] + ',' + [(vars.diAus === NULL) ? (items[props.item].state.split(';')[1].split(',')[1]) : (vars.diAus)] + ',' + [(vars.diAktiv === NULL) ? (items[props.item].state.split(';')[1].split(',')[2]) : (vars.diAktiv)] + ';' + [(vars.miEin === NULL) ? (items[props.item].state.split(';')[2].split(',')[0]) : (vars.miEin)] + ',' + [(vars.miAus === NULL) ? (items[props.item].state.split(';')[2].split(',')[1]) : (vars.miAus)] + ',' + [(vars.miAktiv === NULL) ? (items[props.item].state.split(';')[2].split(',')[2]) : (vars.miAktiv)] + ';' + [(vars.doEin === NULL) ? (items[props.item].state.split(';')[3].split(',')[0]) : (vars.doEin)] + ',' + [(vars.doAus === NULL) ? (items[props.item].state.split(';')[3].split(',')[1]) : (vars.doAus)] + ',' + [(vars.doAktiv === NULL) ? (items[props.item].state.split(';')[3].split(',')[2]) : (vars.doAktiv)] + ';' + [(vars.frEin === NULL) ? (items[props.item].state.split(';')[4].split(',')[0]) : (vars.frEin)] + ',' + [(vars.frAus === NULL) ? (items[props.item].state.split(';')[4].split(',')[1]) : (vars.frAus)] + ',' + [(vars.frAktiv === NULL) ? (items[props.item].state.split(';')[4].split(',')[2]) : (vars.frAktiv)] + ';' + [(vars.saEin === NULL) ? (items[props.item].state.split(';')[5].split(',')[0]) : (vars.saEin)] + ',' + [(vars.saAus === NULL) ? (items[props.item].state.split(';')[5].split(',')[1]) : (vars.saAus)] + ',' + [(vars.saAktiv === NULL) ? (items[props.item].state.split(';')[5].split(',')[2]) : (vars.saAktiv)] + ';' + [(vars.soEin === NULL) ? (items[props.item].state.split(';')[6].split(',')[0]) : (vars.soEin)] + ',' + [(vars.soAus === NULL) ? (items[props.item].state.split(';')[6].split(',')[1]) : (vars.soAus)] + ',' + [(vars.soAktiv === NULL) ? (items[props.item].state.split(';')[6].split(',')[2]) : (vars.soAktiv)] "

das Widget benötigt ein String item in welchem die Informationen gespeichert werden.


passend zu dem Widget habe ich eine Rule geschrieben welche bei Veränderung des Items ausgelöst wird:

Code: Alles auswählen

// Einstellungen bitte hier die namen der beiden Rules für ein und aus eintragen

var ruleEin = "Zeitschaltung_ein";
var ruleAus = "Zeitschaltung_aus";

// Einstellungen bitte hier das Item angeben welches die eingestellten Zeiten enthält

var input = "Zeitschaltuhr";

// Hier beginnt das eigentliche script

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
'use strict';
scriptExtension.importPreset("RuleSupport");
scriptExtension.importPreset("RuleSimple");


// Suchen der Rules anhand des namens
var einRule = rules.getAll().stream().filter(function(i){ return i.name == ruleEin }).findFirst().get();
var ausRule = rules.getAll().stream().filter(function(i){ return i.name == ruleAus }).findFirst().get();

// trigger werden hier gespeichert
var triggerEin = {};
var triggerAus = {};

var i;

for (i=0; i<=6; i++) {
  
  // Hilfsvariablen
  var day="";
  var jahr="";
  var cronEin="";
  var cronAus="";
  var timeEinH = itemRegistry.getItem(input).getState().toString().split(';')[i].split(',')[0].split(':')[0];  
  var timeEinM = itemRegistry.getItem(input).getState().toString().split(';')[i].split(',')[0].split(':')[1]; 
  var timeAusH = itemRegistry.getItem(input).getState().toString().split(';')[i].split(',')[1].split(':')[0];  
  var timeAusM = itemRegistry.getItem(input).getState().toString().split(';')[i].split(',')[1].split(':')[1]; 
  var aktive = itemRegistry.getItem(input).getState().toString().split(';')[i].split(',')[2]; 
  
  // Tag auswählen
  switch (i) {
    case 0 : { day = "MON"; break; }
    case 1 : { day = "TUE"; break; }
    case 2 : { day = "WED"; break; }
    case 3 : { day = "THU"; break; }
    case 4 : { day = "FRI"; break; }  
    case 5 : { day = "SAT"; break; }
    case 6 : { day = "SUN"; break; }
  }
  
  // Wenn Tage ausgeschaltet werden diese nur im Jahr 2019 ausgeführt 
  // Das get sicher noch eleganter also wer eine idee hat immer raus damit.
  if (aktive == "1"){ 
    jahr=" *";
  }else{
    jahr=" 2019"
  }
  
  // Erstellung der richtigen cron Expression
  cronEin = "0 "+ timeEinM + " " + timeEinH + " ? * " + day + jahr;  
  cronAus = "0 "+ timeAusM + " " + timeAusH + " ? * " + day + jahr; 
  
  // Trigger erstellen
  triggerEin[i] = TriggerBuilder.create()
        .withId("aTimer" + i)
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            new Configuration({
                "cronExpression": cronEin
            })).build(); 
  
   triggerAus[i] = TriggerBuilder.create()
        .withId("aTimer" + i)
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            new Configuration({
                "cronExpression": cronAus
            })).build(); 
 
  
}

// Trigger in die Rules einsetzen
einRule.setTriggers([triggerEin[0], triggerEin[1], triggerEin[2], triggerEin[3], triggerEin[4], triggerEin[5], triggerEin[6] ]);
ausRule.setTriggers([triggerAus[0], triggerAus[1], triggerAus[2], triggerAus[3], triggerAus[4], triggerAus[5], triggerAus[6] ]);
damit das ganze funktioniert benötigt ihr jetzt noch 2 weitere Rules in welchen ihr eure gewünschten Aktionen eintragt welche beim einschalten und ausschalten ausgeführt werden sollen.
Die von mir geschriebene Rule benötigt nur die entsprechenden Namen und setzt dann die Cron Trigger nach den von euch eingestellten Zeiten.
Ich finde diese Lösung sehr schön da sie ohne Timer auskommt.

durch Nutzung einer persistence kann man das item auch beim Neustart behalten. Dies ist aber nicht zwingend erforderlich da die Einstellungen in den Rules ja fest sind.

Wem das gefällt kann sich dies gerne Kopieren und für eigene Projekte nutzen.
Sollte jemand noch Verbesserungen haben bin ich dafür gerne offen
Du hast keine ausreichende Berechtigung, um die Dateianhänge dieses Beitrags anzusehen.
OH3 auf RPI4,
Heizungssteuerung: Fritz DECT,
Zigbee: Conbee2 über Phoscon-GW: Aqara Opple Schalter, Aqara Temperatursensoren, Aqara Fenstersensoren, Aqara Aktoren, IKEA Lichter
Astro Binding
DWDUnwetter Binding
OpenWeatherMap Binding
FritzBox TR 064 Binding
iRobot Binding
Telegramm Binding für Benachrichtigungen und Steuerung von unterwegs
Rolladensteuerung: Rademacher DuoFern über HTTP Binding

Antworten