Page 1 of 1

Villkor i .bsh-filerna

Posted: Fri Mar 17, 2023 9:45 am
by AWulff
Hej,

Jag har suttit och surfat runt en hel del här på forumet och även i nexahome-manualen.

Jag har upptäckt att det verkar finnas en hel del villkor som man kan använda som inte finns med i manualen. Är det någon som har en samlad förteckning över vilka villkor som finns tillgängliga, samt vad dessa gör?

Re: Villkor i .bsh-filerna

Posted: Fri Mar 17, 2023 9:45 am
by hekj
Nedanstående programkod är tillgänglig från bsh scripts, jag hoppas att den ska gå att förstå med hjälp av det som tidigare publicerats i forumet (trots att det inte finns någon beskrivning här) :oops:

Code: Select all

// Setters

adjustSunlight(txt) {
  Vector list = new Vector();
  commands_.add(list);
  list.add("sunlight");
  list.add("" + txt);
}

mode(txt) {
  Vector list = new Vector();
  commands_.add(list);
  list.add("mode");
  list.add(txt);
}

attribute(txt) {
  Vector list = new Vector();
  commands_.add(list);
  list.add("attribute");
  list.add(txt);
}

deviceCmd(device, cmd, minutes) {
  Vector list = new Vector();
  commands_.add(list);
  list.add("device");
  list.add(device);
  list.add(cmd);
  if (!("" + minutes).equals("null")) {
    list.add("" + minutes);
  }
}
deviceCmd(device, cmd) {
  deviceCmd(device, cmd, null);
}
deviceOn(device) {
  deviceCmd(device, "On", null);
}
deviceOn(device, minutes) {
  deviceCmd(device, "On", "" + minutes);
}
deviceOff(device) {
  deviceCmd(device, "Off", null);
}
deviceOff(device, minutes) {
  deviceCmd(device, "Off", "" + minutes);
}
deviceLevel(device, level) {
  deviceCmd(device, level, null);
}
deviceLevel(device, level, minutes) {
  deviceCmd(device, level, "" + minutes);
}

execFile(txt) {
  exec_ = new Vector();
  commands_.add(exec_);
  exec_.add("exec");
  exec_.add(txt);
}
execParam(txt) {
  exec_.add(txt);
}

echo(txt) {
  echo_ = new Vector();
  commands_.add(echo_);
  echo_.add("echo");
  echo_.add(toSwedish(txt));
}

outAppend(txt) {
  echo("(outAppend) " + txt);
}
errAppend(txt) {
  echo("(eAppend) " + txt);
}

startTimer(file, seconds) {
  timer_ = new Vector();
  commands_.add(timer_);
  timer_.add("timer");
  timer_.add(file + ":" + seconds);
}

stopTimer(file) {
  timer_ = new Vector();
  commands_.add(timer_);
  timer_.add("timer");
  timer_.add(file + ":-1");
}

setData(key, value) {
  dataTable_.put(toSwedish(key), value);
}

sensorCmd(device, cmd, param1, param2) {
  Vector list = new Vector();
  commands_.add(list);
  list.add("sensor");
  list.add(device);
  list.add(cmd);
  list.add("" + param1);
  if (!("" + param2).equals("null")) {
    list.add("" + param2);
  }
}
setSensorValue(device, value) {
  sensorCmd(device, "set", value, (String)null);
}
setMinimumSensorValue(device, device1, device2) {
  sensorCmd(device, "min", device1, device2);
}
setMaximumSensorValue(device, device1, device2) {
  sensorCmd(device, "max", device1, device2);
}

// Getters

getScriptName() {
  return getData("scriptName_");
}

getMode() {
  if (currentMode_ != null) {
     return currentMode_;
  } else {
     return "?";
   }
}

toSwedish(device) {
  if (device == null) {
    return device;
  }
  c195 = false;
  result = "";
  for (int ndx = 0; ndx < device.length(); ndx++) {
    c = device.charAt(ndx);
    i = (int)c;
    result = result + c;
    if (c195) {
      if (i == 165) {
        result = result.substring(0, result.length() - 2) + 'å';
      } else if (i == 164) {
        result = result.substring(0, result.length() - 2) + 'ä';
      } else if (i == 182) {
        result = result.substring(0, result.length() - 2) + 'ö';
      } else if (i == 8230) {
        result = result.substring(0, result.length() - 2) + 'Å';
      } else if (i == 8222) {
        result = result.substring(0, result.length() - 2) + 'Ä';
      } else if (i == 8211) {
        result = result.substring(0, result.length() - 2) + 'Ö';
      }
    }
    c195 = ((int)device.charAt(ndx) == 195);
  }
  return result;
}

getDeviceStatus(device) {
  String status = statusTable_.get(toSwedish(device));
  if (status == null) {
    status = "";
  }
  return status;
}

getDeviceLevel(device) {
  String level = levelTable_.get(toSwedish(device));
  if (level == null || deviceIsOnOff(device)) {
    level = "-1";
  } else if (level.length() == 0) {
    if (deviceIsOn(device)) {
      return "100";
    } else {
      return "0";
    }
  }
  return level;
}

getSensorValue(device) {
  String value = valueTable_.get(toSwedish(device));
  if (value != null) {
    int pos = value.indexOf(" (");
    if (pos != -1) {
      value = value.substring(0, pos);
    }
  }
  return value;
}

getSensorTimestamp(device) {
  String timestamp = valueTable_.get(toSwedish(device));
  if (timestamp != null) {
    int pos = timestamp.indexOf(" (");
    if (pos != -1) {
      timestamp = timestamp.substring(pos + 2, timestamp.length() - 1);
    }
  }
  return timestamp;
}

getSensorTimestamp(device, sdf_long) {
  if (!sdf_long) { return getSensorTimestamp(device); }
  return getDeviceTimestamp(device);
}

getDeviceTimestamp(device) {
  return timestampTable_.get(toSwedish(device));
}

getWattsValue(device) {
  return (String)wattsTable_.get(toSwedish(device));
}

randomValue(min, max) {
  float f = rnd_.nextFloat();
  int value = (int)(min + Math.round((float)(max-min) * f));
  return value;
}

getData(key) {
  String txt = dataTable_.get(toSwedish(key));
  return (txt != null && txt.startsWith("#BASE64#") ? decodeBase64(txt.substring(8)) : txt);
}

timeToNextEvent() {
  return timeToNextEvent_;
}

getEventQueue(ndx) {
  if (ndx >= 0 && ndx < eventQueue_.size()) {
    return (String)eventQueue_.get(ndx);
  } else {
    return "";
  }
}

getSunrise() {
  return sunrise_;
}
getSunset() {
  return sunset_;
}

getExecutedBy() {
  return executedBy_;
}

getLastSentCommandError() {
  return decodeBase64(lastSentCommandError_);"
}

// Checkers

executedByCmd() {
  return (executedBy_.endsWith("C"));
}

executedByEvent() {
  return (executedBy_.endsWith("E"));
}

executedByTimer() {
  return (executedBy_.endsWith("T"));
}

executedByUpdate() {
  return (executedBy_.endsWith("U"));
}

executedByBsh() {
  return (executedBy_.endsWith("B"));
}

executedByWeb() {
  return (executedBy_.endsWith("W"));
}

lastSentCommandErrorContains(txt) {
  return (getLastSentCommandError().toLowerCase().indexOf(txt.toLowerCase()) != -1);
}

modeIs(txt) {
  return getMode().equalsIgnoreCase(txt);
}

attributeIsOn(txt) {
  return currentAttributes_.contains(txt);
}

cmdIsOn() {
  return "ON".equals(getCmd());
}
cmdIsOff() {
  return "OFF".equals(getCmd());
}
getCmd() {
  return getData("cmd_");
}
getCmdLevel() {
  return getData("level_");
}
getCmdDeviceId() {
  return getData("deviceId_");
}
getCmdDeviceName() {
  return getData("deviceAlias_");
}
getCmdRepeat() {
  return getData("repeat_");
}

getParams(script) {
  return getData(script + ".params_");
}
getParam(script, ndx) {
  return getData(script + ".param" + ndx + "_");
}

deviceIsOnOff(device) {
  return getDeviceStatus(device).length() == 0;
}
deviceIsOn(device) {
  return getDeviceStatus(device).equalsIgnoreCase("On");
}
deviceIsOff(device) {
  return getDeviceStatus(device).equalsIgnoreCase("Off");
}

deviceLevelIs(device, txt) {
  if (txt.endsWith("%")){
    txt = txt.substring(0, txt.length() - 1);
  }
  return getDeviceLevel(device).equals(txt);
}
deviceLevelIs(device, txt) {
  if (txt.endsWith("%")){
    txt = txt.substring(0, txt.length() - 1);
  }
  return getDeviceLevel(device).compareTo(txt) == 0;
}
deviceLevelIsGreaterThan(device, txt) {
  if (txt.endsWith("%")){
    txt = txt.substring(0, txt.length() - 1);
  }
  int txtLevel = Integer.parseInt(txt);
  int devLevel = Integer.parseInt(getDeviceLevel(device));
  return devLevel > txtLevel;
}
deviceLevelIsLessThan(device, txt) {
  if (txt.endsWith("%")){
    txt = txt.substring(0, txt.length() - 1);
  }
  int txtLevel = Integer.parseInt(txt);
  int devLevel = Integer.parseInt(getDeviceLevel(device));
  return devLevel < txtLevel;
}

sunIsUp() {
  Calendar cal = Calendar.getInstance();
  String hour = "" + cal.get(Calendar.HOUR_OF_DAY);
  String minute = "" + cal.get(Calendar.MINUTE);
  if (hour.length() < 2) {
     hour = "0" + hour;
  }
  if (minute.length() < 2) {
     minute = "0" + minute;
  }
  String now = hour + ":" + minute;
  if (sunrise_ != null && sunset_ != null) {
     if (now.compareTo(sunrise_.substring(11)) >= 0
        && now.compareTo(sunset_.substring(11)) < 0) {
        return true;
     }
  }
  return false;
}

todayIs(txt) {
  Calendar cal = Calendar.getInstance();
  int day = cal.get(Calendar.DAY_OF_WEEK);
  StringTokenizer st = new StringTokenizer(txt.toUpperCase(), ", ");
  while (st.hasMoreTokens()) {
    String tok = st.nextToken();
    if (day == Calendar.MONDAY && tok.equals("MONDAY")) {
       return true;
    } else if (day == Calendar.TUESDAY && tok.equals("TUESDAY")) {
       return true;
    } else if (day == Calendar.WEDNESDAY && tok.equals("WEDNESDAY")) {
       return true;
    } else if (day == Calendar.THURSDAY && tok.equals("THURSDAY")) {
       return true;
    } else if (day == Calendar.FRIDAY && tok.equals("FRIDAY")) {
       return true;
    } else if (day == Calendar.SATURDAY && tok.equals("SATURDAY")) {
       return true;
    } else if (day == Calendar.SUNDAY && tok.equals("SUNDAY")) {
       return true;
    }
  }
  return false;
}

hourIs(value) {
  Calendar cal = Calendar.getInstance();
  int hour = cal.get(Calendar.HOUR_OF_DAY);
  return hour == value;
}
hourIsGreaterThan(value) {
  Calendar cal = Calendar.getInstance();
  int hour = cal.get(Calendar.HOUR_OF_DAY);
  return hour > value;
}
hourIsLessThan(value) {
  Calendar cal = Calendar.getInstance();
  int hour = cal.get(Calendar.HOUR_OF_DAY);
  return hour < value;
}

timeIs(hhColonMm) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  if (sdf.format(Calendar.getInstance().getTime()).compareTo(hhColonMm) == 0) {
     return true;
  } else {
     return false;
  }
}
timeIsGreaterThan(hhColonMm) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  if (sdf.format(Calendar.getInstance().getTime()).compareTo(hhColonMm) > 0) {
     return true;
  } else {
     return false;
  }
}
timeIsLessThan(hhColonMm) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
  if (sdf.format(Calendar.getInstance().getTime()).compareTo(hhColonMm) < 0) {
     return true;
  } else {
     return false;
  }
}

timeIsBetween(String hhColonMmFrom, String hhColonMmTo) {
   Date now = Calendar.getInstance().getTime();
   SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
   if (hhColonMmFrom.compareTo(hhColonMmTo) > 0) {
      if (sdf.format(now).compareTo(hhColonMmFrom) >= 0 || sdf.format(now).compareTo(hhColonMmTo) <= 0) {
         return true;
      }
   } else if (sdf.format(now).compareTo(hhColonMmFrom) >= 0 && sdf.format(now).compareTo(hhColonMmTo) <= 0) {
      return true;
   }
   return false;
}

timerIsRunning(file) {
  String timer = (String)timerTable_.get(file);
  return timer != null;
}

cancelEvent() {
  Vector list = new Vector();
  commands_.add(list);
  list.add("CANCEL EVENT");
}

hideOutput() {
  Vector list = new Vector();
  commands_.add(list);
  list.add("HIDE OUTPUT");
}
Updaterad 2017-06-17

Re: Villkor i .bsh-filerna

Posted: Fri Mar 17, 2023 9:45 am
by AWulff
Stort tack för svaret. Det där hjälper mycket!

Re: Villkor i .bsh-filerna

Posted: Fri Mar 17, 2023 9:45 am
by elmaco
Hej, denna skulle jag behöva mer hjälp med, jag vill tända Lampa2 om dimlevel är högre än 60% på lampa1.

Jag har lagt till det som är undrestruket, är jag på rätt spår?

Code: Select all

[b][u]Device = "lampa1"
txt = "60"[/u][/b]

deviceLevelIsGreaterThan(device, txt) {
  if (txt.endsWith("%")){
    txt = txt.substring(0, txt.length() - 1);
  }
  int txtLevel = Integer.parseInt(txt);
  int devLevel = Integer.parseInt(getDeviceLevel(device));
  return devLevel > txtLevel;

[b][u]deviceOn("Lampa2");[/u][/b]

}
Tack på förhand/
Marcus

Re: Villkor i .bsh-filerna

Posted: Fri Mar 17, 2023 9:45 am
by hekj
Har inte testat själv, men följande kod kanske fungerar.

Code: Select all

device1 = "lampa1";
device2 = "lampa2";
limit = "60";
if (deviceLevelIsGreaterThan(device1, limit)) {
  deviceOn(device2);
}