Hello
Bon voila une grosse récap de ce que j’ai fait :
Mon code :
Citation## Vérification présence des variables ou setting par défaut
(empty($scenario->getData(« EDF_Nb_Plages »))) ? $scenario->setData(« EDF_Nb_Plages », « 1 ») : null;
(empty($scenario->getData(« EDF_HP_Start »))) ? $scenario->setData(« EDF_HP_Start », « 08:08 ») : null;
(empty($scenario->getData(« EDF_HC_Start »))) ? $scenario->setData(« EDF_HC_Start », « 00:08 ») : null;
(empty($scenario->getData(« EDF_Tarif_KWh_HP »))) ? $scenario->setData(« EDF_Tarif_KWh_HP », « 0.1727 ») : null;
(empty($scenario->getData(« EDF_Tarif_KWh_HC »))) ? $scenario->setData(« EDF_Tarif_KWh_HC », « 0.1296 ») : null;
(empty($scenario->getData(« EDF_Tarif_Abo_Mois »))) ? $scenario->setData(« EDF_Tarif_Abo_Mois », « 11.60 ») : null;
(empty($scenario->getData(« EDF_Last_Var_Update »))) ? $scenario->setData(« EDF_Last_Var_Update », date(‹ Y-m-d H:i:s ›,strtotime(« now - 1 days »))) : null;
Vérification de la dernière exécution réussie
$commande_conso_hier_hp = cmd::byString(« #[MAISON][Variables EDF][Conso Jour HC]# »);
$collectDate_conso_hier_hp = $commande_conso_hier_hp->getCollectDate();
if (date(‹ Y-m-d ›, strtotime($collectDate_conso_hier_hp)) == date(‹ Y-m-d ›,strtotime(« yesterday »))){
$scenario->setLog(« Scenario déjà lancé, annulation. Date dernier enregistrement valeurs: ».date(‹ Y-m-d ›, strtotime($collectDate_conso_hier_hp)));
} else {
## récupération de la date de dernière maj et vérification du nombre de jours à updater
$derniere_maj_donnees = date('Y-m-d',strtotime($scenario->getData("EDF_Last_Var_Update")));
if($derniere_maj_donnees == 0) {}
$nb_jour_a_updater = scenarioExpression::time_diff($derniere_maj_donnees, "now",d);
if ($nb_jour_a_updater == 1){
$scenario->setLog("Seulement 1 jour a updater");
} else {
$scenario->setLog("Attention: ".$nb_jour_a_updater." jours a updater");
}
### Verification presences donnees horaires
$commande_ConsoHoraire = cmd::byString("#[MAISON][Compteur Linky][Consommation horaire]#");
$collectDate_ConsoHoraire = $commande_ConsoHoraire->getCollectDate();
if (date('Y-m-d', strtotime($collectDate_ConsoHoraire)) == date('Y-m-d',strtotime("now"))) {
### Get and Set variables
$nb_plages_horaires = $scenario->getData("EDF_Nb_Plages");
$debut_heure_pleine = $scenario->getData("EDF_HP_Start");
$debut_heure_creuse = $scenario->getData("EDF_HC_Start");
$debut_heure_pleine2 = $scenario->getData("EDF_HP2_Start");
$debut_heure_creuse2 = $scenario->getData("EDF_HC2_Start");
$tarif_heure_pleine = $scenario->getData("EDF_Tarif_KWh_HP");
$tarif_heure_creuse = $scenario->getData("EDF_Tarif_KWh_HC");
$tarif_abonnement = $scenario->getData("EDF_Tarif_Abo_Mois");
## Commandes
$commande_conso_hier_hc = cmd::byString("#[MAISON][Variables EDF][Tarif Jour HP]#");
$commande_tarif_hier_hp = cmd::byString("#[MAISON][Variables EDF][Tarif Jour HC]#");
$commande_tarif_hier_hc = cmd::byString("#[MAISON][Variables EDF][Tarif Jour]#");
$commande_tarif_hier = cmd::byString("#[MAISON][Variables EDF][Conso Mois HP]#");
$commande_conso_mois_hp = cmd::byString("#[MAISON][Variables EDF][Conso Mois HC]#");
$commande_conso_mois_hc = cmd::byString("#[MAISON][Variables EDF][Conso Mois HC]#");
$commande_moyenne_conso_jour_hp = cmd::byString("#[MAISON][Variables EDF][Moyenne Conso Jour HP]#");
$commande_moyenne_conso_jour_hc = cmd::byString("#[MAISON][Variables EDF][Moyenne Conso Jour HC]#");
$commande_estimation_conso_mois_hp = cmd::byString("#[MAISON][Variables EDF][Estimation Conso Mois HP]#");
$commande_estimation_conso_mois_hc = cmd::byString("#[MAISON][Variables EDF][Estimation Conso Mois HC]#");
$commande_estimation_conso_mois = cmd::byString("#[MAISON][Variables EDF][Estimation Conso Mois]#");
$commande_estimation_prix_mois_hp = cmd::byString("#[MAISON][Variables EDF][Estimation Prix Mois HP]#");
$commande_estimation_prix_mois_hc = cmd::byString("#[MAISON][Variables EDF][Estimation Prix Mois HC]#");
$commande_estimation_prix_mois = cmd::byString("#[MAISON][Variables EDF][Estimation Prix Mois Global]#");
$id_commande_ConsoHoraire = $commande_ConsoHoraire->getId();
$id_commande_ConsoHoraireCreux = $commande_conso_hier_hc->getId();
$id_commande_ConsoHorairePlein = $commande_conso_hier_hp->getId();
### Lancement du calcul des données pour x jours
for($n=($nb_jour_a_updater-1); $n >= 0; $n--){
$date_valeurs = date('Y-m-d H:i:s', strtotime("yesterday - $n days 00:01"));
$date_JourEt1_minuit = date('Y-m-d H:i:s', strtotime("today - $n days 00:00"));
$scenario->setLog("Ajout du ".date('Y-m-d', strtotime("yesterday - $n days")));
$jour_restant_mois = scenarioExpression::time_diff("now - $n days", "first day of next month",d);
## NOTE: la valeur de la minute 0 ne doit pas être prise => ajout de 1mn
$debut_hp = date('Y-m-d H:i:s', strtotime("yesterday - $n days $debut_heure_pleine +1 minute"));
$debut_hc = date('Y-m-d H:i:s', strtotime("yesterday - $n days $debut_heure_creuse + 1 minute"));
if ($nb_plages_horaires == 1) {
$conso_hier_hp = history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc)["sum"] /2;
}else{
$debut_hp2 = date('Y-m-d H:i:s', strtotime("yesterday - $n days $debut_heure_pleine2 +1 minute"));
$debut_hc2 = date('Y-m-d H:i:s', strtotime("yesterday - $n days $debut_heure_creuse2 +1 minute"));
## Heures pleines = Minuit j-1 => $debut_hc + $debut_hp => $debut_hc2 + $debut_hp2 => minuit
$conso_hier_hp = ((history::getStatistique($id_commande_ConsoHoraire, $date_valeurs, $debut_hc)["sum"]) + (history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc2)["sum"]) + (history::getStatistique($id_commande_ConsoHoraire, $debut_hp2, $date_JourEt1_minuit)["sum"])) /2;
}
## Si donnée horaire à 0, envoyer un message dans le centre de message, sinon continuer
if ($conso_hier_hp == 0) {
$title = 'Erreur Récupération données EDF';
$message = 'Données de consommation horaire null pour le '.date('Y-m-d', strtotime("yesterday - $n days"));
message::add($title, $message);
} else {
if ($nb_plages_horaires == 1) {
$conso_hier_hc = (history::getStatistique($id_commande_ConsoHoraire, $date_valeurs, $debut_hp)["sum"] + history::getStatistique($id_commande_ConsoHoraire, $debut_hc, $date_JourEt1_minuit)["sum"] ) /2;
} else {
## Heures creuses = $debut_hc => $debut_hp + $debut_hc2 => $debut_hp2
$conso_hier_hc = (history::getStatistique($id_commande_ConsoHoraire, $debut_hc, $debut_hp)["sum"] + history::getStatistique($id_commande_ConsoHoraire, $debut_hc2, $debut_hp2)["sum"] ) /2;
}
$tarif_hier_hp = $conso_hier_hp * $tarif_heure_pleine;
$tarif_hier_hc = $conso_hier_hc * $tarif_heure_creuse;
$tarif_hier = $tarif_hier_hp + $tarif_hier_hc;
# Historisation intermédiaire
$commande_conso_hier_hp->event($conso_hier_hp, $date_valeurs);
$commande_conso_hier_hc->event($conso_hier_hc, $date_valeurs);
$commande_tarif_hier_hp->event($tarif_hier_hp, $date_valeurs);
$commande_tarif_hier_hc->event($tarif_hier_hc, $date_valeurs);
$commande_tarif_hier->event($tarif_hier, $date_valeurs);
# Calculs
$conso_mois_hc = scenarioExpression::statisticsBetween($id_commande_ConsoHoraireCreux, "sum", "first day of this month", "now");
$conso_mois_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "sum", "first day of this month", "now");
$moyenne_conso_jour_hc = scenarioExpression::statisticsBetween($id_commande_ConsoHoraireCreux, "avg", "first day of this month", "now");
$moyenne_conso_jour_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "avg", "first day of this month", "now");
$estimation_conso_mois_hc = $conso_mois_hc + ($moyenne_conso_jour_hc * $jour_restant_mois);
$estimation_conso_mois_hp = $conso_mois_hp + ($moyenne_conso_jour_hp * $jour_restant_mois);
$estimation_conso_mois = $estimation_conso_mois_hc + $estimation_conso_mois_hp;
$estimation_prix_mois_hc = $estimation_conso_mois_hc * $tarif_heure_creuse;
$estimation_prix_mois_hp = $estimation_conso_mois_hp * $tarif_heure_pleine;
$estimation_prix_mois_global = $estimation_prix_mois_hc + $estimation_prix_mois_hp + $tarif_abonnement;
## Log dans le scénario pour tracer si besoin
$scenario->setLog("conso_hier_hp = ".$conso_hier_hp);
$scenario->setLog("conso_hier_hc = ".$conso_hier_hc);
$scenario->setLog("tarif_hier_hp = ".$tarif_hier_hp);
$scenario->setLog("tarif_hier_hc = ".$tarif_hier_hc);
$scenario->setLog("tarif_hier = ".$tarif_hier);
$scenario->setLog("conso_mois_hc = ".$conso_mois_hc);
$scenario->setLog("conso_mois_hp = ".$conso_mois_hp);
$scenario->setLog("moyenne_conso_jour_hc = ".$moyenne_conso_jour_hc);
$scenario->setLog("moyenne_conso_jour_hp = ".$moyenne_conso_jour_hp);
$scenario->setLog("jour_restant_mois = ".$jour_restant_mois);
$scenario->setLog("estimation_conso_mois_hc = ".$estimation_conso_mois_hc);
$scenario->setLog("estimation_conso_mois_hp = ".$estimation_conso_mois_hp);
$scenario->setLog("estimation_conso_mois = ".$estimation_conso_mois);
$scenario->setLog("estimation_prix_mois_hc = ".$estimation_prix_mois_hc);
$scenario->setLog("estimation_prix_mois_hp = ".$estimation_prix_mois_hp);
$scenario->setLog("estimation_prix_mois_global = ".$estimation_prix_mois_global);
# Historisation
$commande_conso_mois_hp->event($conso_mois_hp, $date_valeurs);
$commande_conso_mois_hc->event($conso_mois_hc, $date_valeurs);
$commande_moyenne_conso_jour_hp->event($moyenne_conso_jour_hp, $date_valeurs);
$commande_moyenne_conso_jour_hc->event($moyenne_conso_jour_hc, $date_valeurs);
$commande_estimation_conso_mois_hp->event($estimation_conso_mois_hp, $date_valeurs);
$commande_estimation_conso_mois_hc->event($estimation_conso_mois_hc, $date_valeurs);
$commande_estimation_conso_mois->event($estimation_conso_mois, $date_valeurs);
$commande_estimation_prix_mois_hp->event($estimation_prix_mois_hp, $date_valeurs);
$commande_estimation_prix_mois_hc->event($estimation_prix_mois_hc, $date_valeurs);
$commande_estimation_prix_mois->event($estimation_prix_mois_global, $date_valeurs);
# Mise à jour variable dernière màj en fois boucle terminée si pas d'erreur
$scenario->setData("EDF_Last_Var_Update", date('Y-m-d H:i:s',strtotime("now - $n days")));
}
}
} else { ## Si données horaires non présentes, rescheduler un lancement 30mn plus tard
$scenario->setLog("Donnees horaire non presente. Replanification de la tache. Derniere collecte horaire EDF presente: ".$collectDate_ConsoHoraire);
# Infos sur le schéduleur
$scenario_scheduler = scenario::byString("#[MAISON][TECHNIQUE][Scenario Scheduler]#");
#Récupération des tags du scénario
$tags = $scenario_scheduler->getTags();
#Modification des tags
$tags['#cible#'] = "EDF";
$tags['#action#'] = "LAUNCH";
$tags['#duree#'] = 30 ;
#Envoi des tags et le lancement de la reprogrammation
$scenario_scheduler->setTags($tags);
$scenario_scheduler->launch();
}
Mais j’ai du loupé quelque chose car comme on peux le voir sur mon virtuel ( en vert ) tout reste à 0
Merci