Bonjour qwench
qwench a écrit:Merci pour l'horloge, mais je ne peux pas soustraire 1h30 par exemple.
ex:
18:40 -> 1840 dans le plugin -> 1840-130 = 1610 -> 16:10
18:00 -> 1800 dans le plugin -> 1800-130 = 1670 ...
Je te propose une version modifiée de "calculator" qui permet aussi de jouer avec les heures ou les dates. Le plugin est entièrement compatible avec l'ancienne version de"calculator", mais tu peux aussi récupérer l'heure actuelle ou l'heure de mise à jour d'un device. et tu peux additionner ou soustraire la durée que tu veux en secondes ou minutes ou heures ou jours...
Dans la formule de "calculator", tu peux ajouter
now pour avoir l'heure actuelle en secondes
change(id_device) pour avoir l'heure de la dernière modification du device id_device (secondes).
now-change(id_device) te donnera la différence (secondes)
now-min(x) te donne l'heure - x minutes (x entier ou décimal : 1.5 ->1 minute 30) (secondes)
now-heure(x) te donne l'heure - x heures (x entier ou décimal : 1.5 ->1 heure 30) (secondes)
now-jour(x) te donne l'heure - x jours (x entier ou décimal : 1.5 ->1 jour et 12h) (secondes)
now-semaine(x) te donne l'heure - x semaines (x entier ou décimal : 1.5 ->1 semaine 3 jours et 12h) (secondes).
Tu peux extraire ensuite la valeur de la minute, l'heure, le jour, la semaine, le jour de la semaine (Lundi -> 1, ..Dimanche ->7), le mois ou l'année correspondante avec
minute(x) ,
hour(x),
day(x),
week(x),
dayweek(x),
month(x) ou
year(x) : les valeurs obtenues sont des nombres permettant des calculs "1" et non pas "01".
Pour de l'affichage, avec, éventuellement un "0" initial, il faut utiliser les mêmes noms avec un "_" final :
minute_(x),...
Par exemple, dans ton cas pour obtenir l'heure décalée de 1h30, tu peux mettre
- Code : Tout sélectionner
hour(now-heure(1.5))
ou bien
- Code : Tout sélectionner
hour(now-heure(1)-minute(30))
Pour obtenir une heure décimale, on peut remplacer les fonctions minute(x), hour(x), day(x), week(x) par
minute_dec(x),
hour_dec(x),
day_dec(x),
week_dec(x)Enfin on peut afficher le résultat de l'heure sous la forme yyyymmdd ou yyyy-mm-dd ou hhmmss ou hh:mm:ss ou bien hhmm ou hh:mm
ymd(x) -> yyyymmdd, ymd_(x) -> yyyy-mm-dd
hms(x) -> hhmmss, hms_(x) -> hh:mm:ss
hm(x) -> hhmm, hm_(x) -> hh:mm
ymdhms(x) -> yyyymmddhhmmss, ymdhms_(x) -> yyyy-mm-dd hh:mm:ss
Autres fonctions ajoutées
frac(x) -> partie fractionnaire et
round1(x),
round2(x),
round3(x) correspondant à round(x,1) round(x,2) round(x,3)
Nouveau code pour "calculator"
- Code : Tout sélectionner
<?php
// Create an object to compute the formula
$evaleedomus = new sdk_EvalMath;
// Get formula from VAR1
$formula = getArg("formula");
if (!isset($formula)) $formula = "0";
// basic evaluation:
$result = $evaleedomus->sdk_evaluate($formula);
@sdk_header('text/xml');
echo "<result>".$result."</result>";
/*
Script développé par :
Mickael VIALAT - http://www.planete-domotique.com
Basé sur l'excellent travail de Miles Kaufmann : EvalMath Class
Merci de partager toute modification ou amélioration de ce script avec la communauté eedomus
sur le forum : http://forum.eedomus.com
Ajoût des fonctions de date et heure // 2022/11 Opa95
change(id_capteur) -> date de la mesure secondes
now -> date actuelle secondes
year(),month(),week(),day(), hour(),minute() -> Nombre correspondant
year_(),month_(),day_(), hour_(),minute_() -> Nombre formaté (00..)
semaine(),jour(),heure(),min() -> nombre de secondes correspondantes
================================================================================
EvalMath - PHP Class to safely evaluate math expressions
Copyright (C) 2005 Miles Kaufmann <http://www.twmagic.com/>
================================================================================
LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1 Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
class sdk_EvalMath {
var $suppress_errors = false;
var $last_error = null;
var $v = array('e'=>2.71,'pi'=>3.14,'now'=>0); // variables (and constants)
var $f = array(); // user-defined functions
var $vb = array('e', 'pi','now'); // constants
var $fb = array( // built-in functions
'device', 'change', 'year', 'month','week','dayweek','day', 'hour', 'minute','week_dec','day_dec', 'hour_dec', 'minute_dec','year_', 'month_', 'day_', 'hour_', 'minute_',
'semaine','jour','heure','min','hms','hms_','ymd','ymd_','ymdhms','ymdhms_',
'abs', 'acos', 'asin', 'atan', 'cos', 'deg2rad', 'exp', 'floor','frac', 'log', 'pow', 'rad2deg', 'rand', 'round','round1','round2','round3', 'sin', 'sqrt');
var $devicetab = array(); // user-defined functions
function sdk_EvalMath()
{
// make the variables a little more accurate
$this->v['pi'] = pi();
$this->v['e'] = exp(1);
$this->v['now'] = time();
}
function sdk_evaluate($expr)
{
$this->last_error = null;
$expr = trim($expr);
if (substr($expr, -1, 1) == ';') $expr = substr($expr, 0, strlen($expr)-1); // strip semicolons at the end
$arr = $this->sdk_nfx($expr);
return $this->sdk_pfx($arr); // straight up evaluation, woo
}
//===================== HERE BE INTERNAL METHODS ====================\\
// Convert infix to postfix notation
function sdk_nfx($expr)
{
$index = 0;
$stack = new sdk_EvalMathStack;
$output = array(); // postfix form of expression, to be passed to pfx()
$expr = trim(strtolower($expr));
$ops = array('+', '-', '*', '/', '^', '_');
$ops_r = array('+'=>0,'-'=>0,'*'=>0,'/'=>0,'^'=>1); // right-associative operator?
$ops_p = array('+'=>0,'-'=>0,'*'=>1,'/'=>1,'_'=>1,'^'=>2); // operator precedence
$expecting_op = false; // we use this in syntax-checking the expression
// and determining when a - is a negation
if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches))
{ // make sure the characters are all good
return $this->sdk_trigger("illegal character '{$matches[0]}'");
}
while(1)
{ // 1 Infinite Loop ;)
$op = substr($expr, $index, 1); // get the first character at the current index
// find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
$ex = preg_match('/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
//===============
if ($op == '-' && !$expecting_op)
{ // is it a negation instead of a minus?
$stack->sdk_push('_'); // put a negation on the stack
$index++;
}
else
if ($op == '_')
{ // we have to explicitly deny this, because it's legal on the stack
return $this->sdk_trigger("illegal character '_'"); // but not in the input expression
}
else
if ((in_array($op, $ops) || $ex) && $expecting_op)
{ // are we putting an operator on the stack?
if ($ex) { // are we expecting an operator but have a number/variable/function/opening parethesis?
$op = '*'; $index--; // it's an implicit multiplication
}
// heart of the algorithm:
while($stack->count > 0 && ($o2 = $stack->sdk_last()) && in_array($o2, $ops) && ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2]))
{
$output[] = $stack->sdk_pop(); // pop stuff off the stack into the output
}
// many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
$stack->sdk_push($op); // finally put OUR operator onto the stack
$index++;
$expecting_op = false;
//===============
}
else
if ($op == ')' && $expecting_op)
{ // ready to close a parenthesis?
while (($o2 = $stack->sdk_pop()) != '(')
{ // pop off the stack back to the last (
if ($o2===null) return $this->sdk_trigger("unexpected ')'");
else $output[] = $o2;
}
if (preg_match("/^([a-z]\w*)\($/", $stack->sdk_last(2), $matches))
{ // did we just close a function?
$fnn = $matches[1]; // get the function name
$arg_count = $stack->sdk_pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
$output[] = $stack->sdk_pop(); // pop the function and push onto the output
if (in_array($fnn, $this->fb))
{ // check the argument count
if($arg_count > 1)
return $this->sdk_trigger("too many arguments ($arg_count given, 1 expected)");
}
else
{ // did we somehow push a non-function on the stack? this should never happen
return $this->sdk_trigger("internal error 1");
}
}
$index++;
//===============
}
else
if ($op == ',' && $expecting_op)
{ // did we just finish a function argument?
while (($o2 = $stack->sdk_pop()) != '(')
{
if ($o2===null) return $this->sdk_trigger("unexpected ','"); // oops, never had a (
else $output[] = $o2; // pop the argument expression stuff and push onto the output
}
// make sure there was a function
if (!preg_match("/^([a-z]\w*)\($/", $stack->sdk_last(2), $matches))
return $this->sdk_trigger("unexpected ','");
$stack->sdk_push($stack->sdk_pop()+1); // increment the argument count
$stack->sdk_push('('); // put the ( back on, we'll need to pop back to it again
$index++;
$expecting_op = false;
//===============
}
else
if ($op == '(' && !$expecting_op)
{
$stack->sdk_push('('); // that was easy
$index++;
$allow_neg = true;
//===============
}
else
if ($ex && !$expecting_op)
{ // do we now have a function/variable/number?
$expecting_op = true;
$val = $match[1];
if (preg_match("/^([a-z]\w*)\($/", $val, $matches))
{ // may be func, or variable w/ implicit multiplication against parentheses...
if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f))
{ // it's a func
$stack->sdk_push($val);
$stack->sdk_push(1);
$stack->sdk_push('(');
$expecting_op = false;
}
else
{ // it's a var w/ implicit multiplication
$val = $matches[1];
$output[] = $val;
}
}
else
{ // it's a plain old var or num
$output[] = $val;
}
$index += strlen($val);
//===============
}
else
if ($op == ')')
{ // miscellaneous error checking
return $this->sdk_trigger("unexpected ')'");
}
else
if (in_array($op, $ops) && !$expecting_op)
{
return $this->sdk_trigger("unexpected operator '$op'");
}
else
{ // I don't even want to know what you did to get here
return $this->sdk_trigger("an unexpected error occured");
}
if ($index == strlen($expr))
{
if (in_array($op, $ops))
{ // did we end with an operator? bad.
return $this->sdk_trigger("operator '$op' lacks operand");
}
else
break;
}
while (substr($expr, $index, 1) == ' ')
{ // step the index past whitespace (pretty much turns whitespace
$index++; // into implicit multiplication if no operator is there)
}
}
while (($op = $stack->sdk_pop())!=null)
{ // pop everything off the stack and push onto output
if ($op == '(') return $this->sdk_trigger("expecting ')'"); // if there are (s on the stack, ()s were unbalanced
$output[] = $op;
}
return $output;
}
function sdk_device($val)
{
$valtab = getValue($val);
if (isset($valtab['value']))
return $valtab['value'];
else
return 0;
}
function sdk_change($val)
{
$valtab = getValue($val);
if (isset($valtab['change'])){
$time = strtotime($valtab['change']);
return $time;}
else
return time();
}
function sdk_year($val)
{
return date('Y',$val);
}
function sdk_month($val)
{
return date('n',$val);
}
function sdk_week($val)
{
return date('W',$val);
}
function sdk_dayweek($val)
{
return date('N',$val);
}
function sdk_day($val)
{
return date('j',$val);
}
function sdk_hour($val)
{
return date('G',$val);
}
function sdk_minute($val)
{
$valtab = date('i',$val);
if (strpos($valtab.'0','0') == 1) $valtab = substr($valtab,1);
return $valtab;
}
function sdk_week_dec($val)
{
return date('W',$val)+($val%604800)/604800;
}
function sdk_day_dec($val)
{
return date('j',$val)+($val%86400)/86400;
}
function sdk_hour_dec($val)
{
return date('G',$val)+($val%3600)/3600;
}
function sdk_minute_dec($val)
{
$valtab = date('i',$val);
if (strpos($valtab.'0','0') == 1) $valtab = substr($valtab,1);
return $valtab+($val%60)/60;
}
function sdk_ymd($val)
{
return date('Ymd',$val);
}
function sdk_ymd_($val)
{
return date('Y-m-d',$val);
}
function sdk_ymdhms($val)
{
return date('YmdHis',$val);
}
function sdk_ymdhms_($val)
{
return date('Y-m-d H:i:s',$val);
}
function sdk_hms($val)
{
return date('His',$val);
}
function sdk_hms_($val)
{
return date('H:i:s',$val);
}
function sdk_month_($val)
{
return date('m',$val);
}
function sdk_day_($val)
{
return date('d',$val);
}
function sdk_hour_($val)
{
return date('h',$val);
}
function sdk_minute_($val)
{
return date('i',$val);
}
function sdk_min($val)
{
return round($val*60);
}
function sdk_heure($val)
{
return round($val*3600);
}
function sdk_jour($val)
{
return round($val*86400);
}
function sdk_semaine($val)
{
return round($val*604800);
}
// evaluate postfix notation
function sdk_pfx($tokens, $vars = array())
{
if ($tokens == false) return false;
$stack = new sdk_EvalMathStack;
foreach ($tokens as $token)
{ // nice and easy
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
if (in_array($token, array('+', '-', '*', '/', '^')))
{
if (($op2 = $stack->sdk_pop())===null) return $this->sdk_trigger("internal error 2");
if (($op1 = $stack->sdk_pop())===null) return $this->sdk_trigger("internal error 3");
switch ($token) {
case '+':
$stack->sdk_push($op1+$op2); break;
case '-':
$stack->sdk_push($op1-$op2); break;
case '*':
$stack->sdk_push($op1*$op2); break;
case '/':
if ($op2 == 0) return $this->sdk_trigger("division by zero");
$stack->sdk_push($op1/$op2); break;
case '^':
$stack->sdk_push(pow($op1, $op2)); break;
}
// if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
}
else
if ($token == "_")
{
$stack->sdk_push(-1*$stack->sdk_pop());
// if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
}
else
if (preg_match("/^([a-z]\w*)\($/", $token, $matches))
{ // it's a function!
$fnn = $matches[1];
if (in_array($fnn, $this->fb)) { // built-in function:
if (($op1 = $stack->sdk_pop())===null) return $this->sdk_trigger("internal error 4");
$fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms
switch ($fnn)
{
case "device" :
$stack->sdk_push($this->sdk_device($op1));
break;
case "change" :
$stack->sdk_push($this->sdk_change($op1));
break;
case "year" :
case "year_" :
$stack->sdk_push($this->sdk_year(round($op1)));
break;
case "month" :
$stack->sdk_push($this->sdk_month(round($op1)));
break;
case "week" :
$stack->sdk_push($this->sdk_week(round($op1)));
break;
case "dayweek" :
$stack->sdk_push($this->sdk_dayweek(round($op1)));
break;
case "day" :
$stack->sdk_push($this->sdk_day(round($op1)));
break;
case "hour" :
$stack->sdk_push($this->sdk_hour(round($op1)));
break;
case "minute" :
$stack->sdk_push($this->sdk_minute(round($op1)));
break;
case "week_dec" :
$stack->sdk_push($this->sdk_week_dec(round($op1)));
break;
case "day_dec" :
$stack->sdk_push($this->sdk_day_dec(round($op1)));
break;
case "hour_dec" :
$stack->sdk_push($this->sdk_hour_dec(round($op1)));
break;
case "minute_dec" :
$stack->sdk_push($this->sdk_minute_dec(round($op1)));
break;
case "month_" :
$stack->sdk_push($this->sdk_month_(round($op1)));
break;
case "day_" :
$stack->sdk_push($this->sdk_day_(round($op1)));
break;
case "hour_" :
$stack->sdk_push($this->sdk_hour_(round($op1)));
break;
case "minute_" :
$stack->sdk_push($this->sdk_minute_(round($op1)));
break;
case "semaine" :
$stack->sdk_push($this->sdk_semaine($op1));
break;
case "jour" :
$stack->sdk_push($this->sdk_jour($op1));
break;
case "heure" :
$stack->sdk_push($this->sdk_heure($op1));
break;
case "min" :
$stack->sdk_push($this->sdk_min($op1));
break;
case "ymd" :
$stack->sdk_push($this->sdk_ymd(round($op1)));
break;
case "ymd_" :
$stack->sdk_push($this->sdk_ymd_(round($op1)));
break;
case "hms" :
$stack->sdk_push($this->sdk_hms(round($op1)));
break;
case "hms_" :
$stack->sdk_push($this->sdk_hms_(round($op1)));
break;
case "ymdhms" :
$stack->sdk_push($this->sdk_ymdhms(round($op1)));
break;
case "ymdhms_" :
$stack->sdk_push($this->sdk_ymdhms_(round($op1)));
break;
case "abs":
$stack->sdk_push(abs($op1));
break;
case "acos":
$stack->sdk_push(acos($op1));
break;
case "asin":
$stack->sdk_push(asin($op1));
break;
case "atan":
$stack->sdk_push(atan($op1));
break;
case "cos":
$stack->sdk_push(cos($op1));
break;
case "deg2rad":
$stack->sdk_push(deg2rad($op1));
break;
case "exp":
$stack->sdk_push(exp($op1));
break;
case "floor":
$stack->sdk_push(floor($op1));
break;
case "frac":
$stack->sdk_push($op1-floor($op1));
break;
case "log":
$stack->sdk_push(log($op1));
break;
case "pow":
$stack->sdk_push(pow($op1));
break;
case "rad2deg":
$stack->sdk_push(rad2deg($op1));
break;
case "rand":
$stack->sdk_push(rand($op1));
break;
case "round":
$stack->sdk_push(round($op1));
break;
case "round1":
$stack->sdk_push(round($op1),1);
break;
case "round2":
$stack->sdk_push(round($op1),2);
break;
case "round3":
$stack->sdk_push(round($op1),3);
break;
case "sin":
$stack->sdk_push(sin($op1));
break;
case "sqrt":
$stack->sdk_push(sqrt($op1));
break;
}
}
else
if (array_key_exists($fnn, $this->f))
{ // user function
// get args
$args = array();
for ($i = count($this->f[$fnn]['args'])-1; $i >= 0; $i--)
{
if (($args[$this->f[$fnn]['args'][$i]] = $stack->sdk_pop())===null) return $this->sdk_trigger("internal error 5");
}
$stack->sdk_push($this->sdk_pfx($this->f[$fnn]['func'], $args)); // yay... recursion!!!!
}
// if the token is a number or variable, push it on the stack
}
else
{
if (is_numeric($token))
{
$stack->sdk_push($token);
}
else
if (array_key_exists($token, $this->v))
{
$stack->sdk_push($this->v[$token]);
}
else
if (array_key_exists($token, $vars))
{
$stack->sdk_push($vars[$token]);
}
else
{
return $this->sdk_trigger("undefined variable '$token'");
}
}
}
// when we're out of tokens, the stack should have a single element, the final result
if ($stack->count != 1) return $this->sdk_trigger("internal error 6");
return $stack->sdk_pop();
}
// trigger an error, but nicely, if need be
function sdk_trigger($msg)
{
$this->last_error = $msg;
if (!$this->suppress_errors) echo $msg;
return false;
}
}
class sdk_EvalMathStack
{
var $stack = array();
var $count = 0;
function sdk_push($val)
{
$this->stack[$this->count] = $val;
$this->count++;
}
function sdk_pop()
{
if ($this->count > 0)
{
$this->count--;
return $this->stack[$this->count];
}
return null;
}
function sdk_last($n=1)
{
if (isset($this->stack[$this->count-$n]))
{
return $this->stack[$this->count-$n];
}
return;
}
}
?>
:)
eedomus+, Zibase V1, RFP1000, RFXcom, RadioDriver CPL 630 X2D, capteurs puissance OWL, thermometres Oregon, téléinfo (USB Linky), detecteurs ouverture X2D, pilotage chauffage X2D, Ecoflow River PRO, PAC Shogun (Atlantic-Cozytouch)