Calculs sur Heure Pleine / Heure Creuse et Tarifications / Coûts

Bonjour à tous,

pour information, j’ai développé un scénario qui permet de calculer les diverses consommations selon les heures pleines et creuses et en déduire les coûts associés (personnalisation via des variables).

Données calculés :

  • conso_hier_hp
  • conso_hier_hc
  • tarif_hier_hp
  • tarif_hier_hc
  • tarif_hier
  • conso_mois_hc
  • conso_mois_hp
  • moyenne_conso_jour_hc
  • moyenne_conso_jour_hp
  • jour_restant_mois
  • estimation_conso_mois_hc
  • estimation_conso_mois_hp
  • estimation_conso_mois
  • estimation_prix_mois_hc
  • estimation_prix_mois_hp
  • estimation_prix_mois_global

Note: les variables sont appelées « hier » car les données dates de hier, mais j’ajoute dans l’history à la date de hier, ce qui implique que les données finales seront à la bonne date, comme ce qui est fait avec le plugin Enedis.

Le scénario permet de contrôler si les données sont présentes ou non et se relance automatiquement si jamais les données n’ont pas encore été récupérées par le plugin Enedis.

Plusieurs scénarios de plages horaires existent, pour éviter de faire un script qui gère tous les cas possibles et que beaucoup de choses ne servent à rien, je vais découper le code selon les plages possibles (update au fur et à mesure qu’ils seront créés, n’hésitez pas à transmettre votre adaptation si elle n’est pas encore créée). Il faudra donc choisir son code selon le type de plage horaire d’HC et HP que vous avez. Pour le restes des besoins, ils sont similaires entre tous les scénarios.

1 / Besoins pour faire fonctionner le scénario

  • Un virtuel avec l’ensemble des commandes infos citées au dessus
  • Un ensemble de variables afin de gérer les HC et HP ainsi que les coûts d’abonnement
  • Un scénario scheduler (qui peut être mutualisé pour plein de choses) pour piloter en asynchrone le scénario final (il y a donc 2 scénarios)

Note: les variables sont maintenant directement créées par le scénario, il ne suffit que de personnaliser ses valeurs par défaut.

a - Le virtuel nommé « Variables EDF »

et toutes ses variables à créer:

#[Appartement][Variables EDF][Conso Jour HP]#
#[Appartement][Variables EDF][Conso Jour HC]#
#[Appartement][Variables EDF][Tarif Jour HP]#
#[Appartement][Variables EDF][Tarif Jour HC]#
#[Appartement][Variables EDF][Tarif Jour]#
#[Appartement][Variables EDF][Conso Mois HP]#
#[Appartement][Variables EDF][Conso Mois HC]#
#[Appartement][Variables EDF][Moyenne Conso Jour HP]#
#[Appartement][Variables EDF][Moyenne Conso Jour HC]#
#[Appartement][Variables EDF][Estimation Conso Mois HP]#
#[Appartement][Variables EDF][Estimation Conso Mois HC]#
#[Appartement][Variables EDF][Estimation Conso Mois]#
#[Appartement][Variables EDF][Estimation Prix Mois HP]#
#[Appartement][Variables EDF][Estimation Prix Mois HC]#
#[Appartement][Variables EDF][Estimation Prix Mois Global]#

b - Les variables à personnaliser les valeurs dans le script

  • EDF_HP_Start
  • EDF_HC_Start
  • EDF_Tarif_KWh_HP (et HP2)
  • EDF_Tarif_KWh_HC (et HC2)
  • EDF_Tarif_Abo_Mois
  • EDF_Last_Var_Update (information mise à jour par le scénario)

c - Le scénario scheduler (mutualisable)

Je fais appel à ce scénario avec envoi de tags pour relancer les calculs

d - Le scénario final de création des variables EDF « Mise Jour Donnees EDF »

— Une seule plage horaire —

1/ Cas début heures creuses avant minuit

Exemple cas concret : 22h30 => 7h30

Version code du 26/03/2021 : Fonctionnement validé

################################################
## Scenario début heures creuses avant minuit ##
################################################
## Vérification présence des variables ou implémentation avec valeurs par défaut
(empty($scenario->getData("EDF_HP_Start"))) ? $scenario->setData("EDF_HP_Start", "06:30") : null;
(empty($scenario->getData("EDF_HC_Start"))) ? $scenario->setData("EDF_HC_Start", "22:30") : 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("#[Appartement][Variables EDF][Conso Jour HP]#");
$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")));
		$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("#[Appartement][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
		$debut_heure_pleine = $scenario->getData("EDF_HP_Start");
		$debut_heure_creuse = $scenario->getData("EDF_HC_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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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"));
			$conso_hier_hp = history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc)["sum"] /2;

			## Si donnée horaire à 0, envoyer un message dans le centre de message et utiliser les moyennes
			if (empty($conso_hier_hp)) {

				$title = 'Scénario Variables EDF';
				$message = 'Données de consommation horaire vide pour le '.date('Y-m-d', strtotime("yesterday - $n days")).'. Estimation des valeurs via les statistiques de consommation du mois en cours.';
				message::add($title, $message);
				
              #Récupération de la consommation du jour
        	    $id_commande_ConsoJour = cmd::byString("#[Appartement][Compteur Linky][Consommation Jour]#")->getId();
              	$conso_hier = history::getStatistique($id_commande_ConsoJour, date('Y-m-d H:i:s', strtotime("yesterday - $n days 00:00:00")), date('Y-m-d H:i:s', strtotime("yesterday - $n days 23:59:59")))["last"];
              	$moyenne_conso_jour_hc = $commande_moyenne_conso_jour_hc->execCmd();
                $moyenne_conso_jour_hp = $commande_moyenne_conso_jour_hp->execCmd();
	            $ratio_conso_hp = ($moyenne_conso_jour_hp / ($moyenne_conso_jour_hp + $moyenne_conso_jour_hc)) * 100;
				$conso_hier_hp = ($conso_hier * $ratio_conso_hp) / 100;
				$conso_hier_hc = ($conso_hier * (100 - $ratio_conso_hp)) / 100;
				
			} else { 
				#Calcul de l'heure creuse si valeurs journalières OK
				$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;
			}																																													
          
			# Si les données ne sont pas vides ou nulls, continuer les calculs
			if (!empty($conso_hier_hc) && !empty($conso_hier_hp)) {
          
				# Finalisation des calculs divers
				$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 midnight", "now");
				$conso_mois_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "sum", "first day of this month midnight", "now");

				$moyenne_conso_jour_hc = scenarioExpression::statisticsBetween($id_commande_ConsoHoraireCreux, "avg", "first day of this month midnight", "now");
				$moyenne_conso_jour_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "avg", "first day of this month midnight", "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 {
				$scenario->setLog("Les données de consommation du ".date('Y-m-d', strtotime("yesterday - $n days"))." n'ont pas pu être traitées ni avec les données horaires ni via les statistiques de consommation du mois");
			}
		}

	} 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("#[Technique][Tools][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();
	}

}

2/ Cas début heures creuses dès minuit

Exemple cas concret : 00h00 => 7h30

Version code du 26/03/2021 : Fonctionnement validé

##############################################
## Scenario début heures creuses dès minuit ##
##############################################
## Vérification présence des variables ou implémentation avec valeurs par défaut
(empty($scenario->getData("EDF_HP_Start"))) ? $scenario->setData("EDF_HP_Start", "07:30") : null;
(empty($scenario->getData("EDF_HC_Start"))) ? $scenario->setData("EDF_HC_Start", "00:00") : 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("#[Appartement][Variables EDF][Conso Jour HP]#");
$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")));
		$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("#[Appartement][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
		$debut_heure_pleine = $scenario->getData("EDF_HP_Start");
		$debut_heure_creuse = $scenario->getData("EDF_HC_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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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("today - $n days $debut_heure_creuse + 1 minute"));
			$conso_hier_hp = history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc)["sum"] /2;

			## Si donnée horaire à 0, envoyer un message dans le centre de message et utiliser les moyennes
			if (empty($conso_hier_hp)) {

				$title = 'Scénario Variables EDF';
				$message = 'Données de consommation horaire vide pour le '.date('Y-m-d', strtotime("yesterday - $n days")).'. Estimation des valeurs via les statistiques de consommation du mois en cours.';
				message::add($title, $message);
				
				#Récupération de la consommation du jour
        	    $id_commande_ConsoJour = cmd::byString("#[Appartement][Compteur Linky][Consommation Jour]#")->getId();
              	$conso_hier = history::getStatistique($id_commande_ConsoJour, date('Y-m-d H:i:s', strtotime("yesterday - $n days 00:00:00")), date('Y-m-d H:i:s', strtotime("yesterday - $n days 23:59:59")))["last"];
              	$moyenne_conso_jour_hc = $commande_moyenne_conso_jour_hc->execCmd();
                $moyenne_conso_jour_hp = $commande_moyenne_conso_jour_hp->execCmd();
	            $ratio_conso_hp = ($moyenne_conso_jour_hp / ($moyenne_conso_jour_hp + $moyenne_conso_jour_hc)) * 100;
				$conso_hier_hp = ($conso_hier * $ratio_conso_hp) / 100;
				$conso_hier_hc = ($conso_hier * (100 - $ratio_conso_hp)) / 100;
				
			} else {

				#Calcul de l'heure creuse si valeurs journalières OK
				
				$conso_hier_hc = (history::getStatistique($id_commande_ConsoHoraire, $date_valeurs, $debut_hp)["sum"] ) /2;
			}
          
			# Si les données ne sont pas vides ou nulls, continuer les calculs
			if (!empty($conso_hier_hc) && !empty($conso_hier_hp)) {
          
				# Finalisation des calculs divers
				$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 midnight", "now");
				$conso_mois_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "sum", "first day of this month midnight", "now");

				$moyenne_conso_jour_hc = scenarioExpression::statisticsBetween($id_commande_ConsoHoraireCreux, "avg", "first day of this month midnight", "now");
				$moyenne_conso_jour_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "avg", "first day of this month midnight", "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 {
				$scenario->setLog("Les données de consommation du ".date('Y-m-d', strtotime("yesterday - $n days"))." n'ont pas pu être traitées ni avec les données horaires ni via les statistiques de consommation du mois");
			}
		}

	} 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("#[Technique][Tools][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();
	}

}

La suite des autres cas possibles dans le commentaire suivant :slight_smile:

8 « J'aime »

— Deux plages horaires —

1/ Cas 2 plages dans la même journée

Exemple cas concret : 2h00 => 7h00 et 14h00 => 17h00

Version code du 26/03/2021 : /!\ Fonctionnement non confirmé

##############################################
#  Scenario 2 plages HC dans la même journée #
##############################################
## Vérification présence des variables ou setting par défaut
(empty($scenario->getData("EDF_HP_Start"))) ? $scenario->setData("EDF_HP_Start", "07:00") : null;
(empty($scenario->getData("EDF_HC_Start"))) ? $scenario->setData("EDF_HC_Start", "02:00") : null;
(empty($scenario->getData("EDF_HP2_Start"))) ? $scenario->setData("EDF_HP2_Start", "17:00") : null;
(empty($scenario->getData("EDF_HC2_Start"))) ? $scenario->setData("EDF_HC2_Start", "14:00") : 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("#[Appartement][Variables EDF][Conso Jour HP]#");
$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")));
	
	$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("#[Appartement][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
		$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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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"));
			

			$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"));
			## 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 et utiliser les moyennes
			if (empty($conso_hier_hp)) {

				$title = 'Scénario Variables EDF';
				$message = 'Données de consommation horaire vide pour le '.date('Y-m-d', strtotime("yesterday - $n days")).'. Estimation des valeurs via les statistiques de consommation du mois en cours.';
				message::add($title, $message);
				
				#Récupération de la consommation du jour
        	    $id_commande_ConsoJour = cmd::byString("#[Appartement][Compteur Linky][Consommation Jour]#")->getId();
              	$conso_hier = history::getStatistique($id_commande_ConsoJour, date('Y-m-d H:i:s', strtotime("yesterday - $n days 00:00:00")), date('Y-m-d H:i:s', strtotime("yesterday - $n days 23:59:59")))["last"];
              	$moyenne_conso_jour_hc = $commande_moyenne_conso_jour_hc->execCmd();
                $moyenne_conso_jour_hp = $commande_moyenne_conso_jour_hp->execCmd();
	            $ratio_conso_hp = ($moyenne_conso_jour_hp / ($moyenne_conso_jour_hp + $moyenne_conso_jour_hc)) * 100;
				$conso_hier_hp = ($conso_hier * $ratio_conso_hp) / 100;
				$conso_hier_hc = ($conso_hier * (100 - $ratio_conso_hp)) / 100;
				
			} else {

				#Calcul de l'heure creuse si valeurs journalières OK								   
				## 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;
			}	
          		
			if (!empty($conso_hier_hc) && !empty($conso_hier_hp)) {
          
				# Finalisation des calculs divers
				$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 midnight", "now");
				$conso_mois_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "sum", "first day of this month midnight", "now");

				$moyenne_conso_jour_hc = scenarioExpression::statisticsBetween($id_commande_ConsoHoraireCreux, "avg", "first day of this month midnight", "now");
				$moyenne_conso_jour_hp = scenarioExpression::statisticsBetween($id_commande_ConsoHorairePlein, "avg", "first day of this month midnight", "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 {
				$scenario->setLog("Les données de consommation du ".date('Y-m-d', strtotime("yesterday - $n days"))." n'ont pas pu être traitées ni avec les données horaires ni via les statistiques de consommation du mois");
			}
		}

	} 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("#[Technique][Tools][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();
	}

}

— Scenario sans HP/HC —

Scénario sans HP/HC fait pour les personnes souhaitant faire un calcul du tarif journalier ainsi que des statistiques sur la consommation pour le mois.

Version code du 26/03/2021 : Fonctionnement confirmé

################################################
## Scenario Statistiques consommation sans HC ##
################################################
## Vérification présence des variables ou implémentation avec valeurs par défaut
(empty($scenario->getData("EDF_Tarif_KWh"))) ? $scenario->setData("EDF_Tarif_KWh", "0.1582") : null;
(empty($scenario->getData("EDF_Tarif_Abo_Mois"))) ? $scenario->setData("EDF_Tarif_Abo_Mois", "10.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 = cmd::byString("#[Tools][Variables EDF][Conso Jour]#");
$collectDate_conso_hier = $commande_conso_hier->getCollectDate();

if (date('Y-m-d', strtotime($collectDate_conso_hier)) == date('Y-m-d',strtotime("yesterday"))){
	$scenario->setLog("Scenario déjà lancé, annulation. Date enregistrement valeurs:".date('Y-m-d', strtotime($collectDate_conso_hier)));
} 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")));
		$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 journaliere

		$commande_ConsoJour = cmd::byString("#[Appartement][Compteur Linky][Consommation Jour]#");
		$collectDate_ConsoJour = $commande_ConsoJour->getCollectDate();

	if (date('Y-m-d', strtotime($collectDate_ConsoJour)) == date('Y-m-d',strtotime("yesterday"))) {

		### Get and Set variables
		$tarif_heure_pleine = $scenario->getData("EDF_Tarif_KWh");
		$tarif_abonnement = $scenario->getData("EDF_Tarif_Abo_Mois");

		## Commandes
		$commande_tarif_hier = cmd::byString("#[Tools][Variables EDF][Tarif Jour]#");
		$commande_conso_mois = cmd::byString("#[Tools][Variables EDF][Conso Mois]#");
		$commande_moyenne_conso_jour = cmd::byString("#[Tools][Variables EDF][Moyenne Conso Jour]#");
		$commande_estimation_conso_mois = cmd::byString("#[Tools][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois = cmd::byString("#[Tools][Variables EDF][Estimation Prix Mois]#");
		$commande_estimation_prix_mois_global = cmd::byString("#[Tools][Variables EDF][Estimation Prix Mois Global]#");
		$id_commande_ConsoJourPlein = $commande_conso_hier->getId();
		$id_commande_ConsoJour = $commande_ConsoJour->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"));
			$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
			$conso_hier = history::getStatistique($id_commande_ConsoJour, date('Y-m-d H:i:s', strtotime("yesterday - $n days 00:00:00")), date('Y-m-d H:i:s', strtotime("yesterday - $n days 23:59:59")))["last"];
			
			## Si donnée horaire à 0, envoyer un message dans le centre de message, sinon continuer
			if (empty($conso_hier)) {

				$title = 'Scénario Variables EDF';
				$message = 'Données de consommation null pour le '.date('Y-m-d', strtotime("yesterday - $n days"));
				message::add($title, $message);
				
			} else {

				$tarif_hier = $conso_hier * $tarif_heure_pleine;

				# Historisation intermédiaire
				$commande_conso_hier->event($conso_hier, $date_valeurs);
				$commande_tarif_hier->event($tarif_hier, $date_valeurs);


				# Calculs 
				$conso_mois = scenarioExpression::statisticsBetween($id_commande_ConsoJourPlein, "sum", "first day of this month midnight", "now");
				$moyenne_conso_jour = scenarioExpression::statisticsBetween($id_commande_ConsoJourPlein, "avg", "first day of this month midnight", "now");
				$estimation_conso_mois = $conso_mois + ($moyenne_conso_jour * $jour_restant_mois);
				$estimation_prix_mois = $estimation_conso_mois * $tarif_heure_pleine;
				$estimation_prix_mois_global = $estimation_prix_mois + $tarif_abonnement;

				## Log dans le scénario pour tracer si besoin
				$scenario->setLog("conso_hier = ".$conso_hier);
				$scenario->setLog("tarif_hier = ".$tarif_hier);
				$scenario->setLog("conso_mois_hp = ".$conso_mois);
				$scenario->setLog("moyenne_conso_jour = ".$moyenne_conso_jour);
				$scenario->setLog("jour_restant_mois = ".$jour_restant_mois);
				$scenario->setLog("estimation_conso_mois = ".$estimation_conso_mois);
				$scenario->setLog("estimation_prix_mois = ".$estimation_prix_mois);
				$scenario->setLog("estimation_prix_mois_global = ".$estimation_prix_mois_global);


				# Historisation
				$commande_conso_mois->event($conso_mois, $date_valeurs);
				$commande_moyenne_conso_jour->event($moyenne_conso_jour, $date_valeurs);
				$commande_estimation_conso_mois->event($estimation_conso_mois, $date_valeurs);
				$commande_estimation_prix_mois->event($estimation_prix_mois, $date_valeurs);
				$commande_estimation_prix_mois_global->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 du jour non presente. Replanification de la tache. Derniere collecte journaliere EDF presente: ".$collectDate_ConsoJour);

		# Infos sur le schéduleur
		$scenario_scheduler = scenario::byString("#[Technique][Tools][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();
	}
}

Personnalisation du code

Attention le code nécessite d’être personnalisé avec son environnement afin de notamment faire appel au bon chemin vers ses commandes du virtuel et également appel au bon scénario scheduler.

image

Finalisation

Il ne reste ensuite qu’à mettre une programmation pour le scénario Variable EDF pour qu’il se lance juste après l’horaire aléatoire du plugin Enedis.

Les variables seront ensuite à utiliser comme bon vous semble :slight_smile:

Si vous voyez des améliorations à apporter ou ajout d’autres calculs, n’hésitez pas ! C’est mon premier scénario code de ce type, il y a surement plein de choses à optimiser :slight_smile:

Bonne journée.

Mick74

EDIT 14/02/2021 : Mise en place de la gestion des trous de collecte (voir message du 14/02 pour les détails)
EDIT 17/02/2021 : Correction d’un bug sur une gestion de date.
EDIT 02/03/2021 : Correction d’un bug sur les calculs (premier jour du mois non pris en compte). Et remise en forme du message principale
EDIT 26/03/2021 : Ajout d’une fonctionnalité permettant de combler les trous de données enedis avec une estimation des HC/HP en utilisant les statistiques du mois. Quelques améliorations du code. Ajout d’un scénario sans HP/HC pour le calculs des tarifs et statistiques.


J’oubliais, petit exemple des logs du scénario:

------------------------------------
[2021-02-11 13:08:09][SCENARIO] Start : Scenario lance manuellement.
[2021-02-11 13:08:09][SCENARIO] Exécution du sous-élément de type [action] : code
[2021-02-11 13:08:09][SCENARIO] Exécution d'un bloc code
[2021-02-11 13:08:09][SCENARIO] conso_hier_hp = 10.085
[2021-02-11 13:08:09][SCENARIO] conso_hier_hc = 9.395
[2021-02-11 13:08:09][SCENARIO] tarif_hier_hp = 1.7416795
[2021-02-11 13:08:09][SCENARIO] tarif_hier_hc = 1.217592
[2021-02-11 13:08:09][SCENARIO] tarif_hier = 2.9592715
[2021-02-11 13:08:09][SCENARIO] conso_mois_hc = 15.20
[2021-02-11 13:08:09][SCENARIO] conso_mois_hp = 26.34
[2021-02-11 13:08:09][SCENARIO] moyenne_conso_jour_hc = 7.600000
[2021-02-11 13:08:09][SCENARIO] moyenne_conso_jour_hp = 13.170000
[2021-02-11 13:08:09][SCENARIO] jour_restant_mois = 18
[2021-02-11 13:08:09][SCENARIO] estimation_conso_mois_hc = 152
[2021-02-11 13:08:09][SCENARIO] estimation_conso_mois_hp = 263.4
[2021-02-11 13:08:09][SCENARIO] estimation_conso_mois = 415.4
[2021-02-11 13:08:09][SCENARIO] estimation_prix_mois_hc = 19.6992
[2021-02-11 13:08:09][SCENARIO] estimation_prix_mois_hp = 45.48918
[2021-02-11 13:08:09][SCENARIO] estimation_prix_mois_global = 76.78838
[2021-02-11 13:08:10][SCENARIO] Fin correcte du scénario
------------------------------------
[2021-02-12 10:45:02][SCENARIO] Start : Scenario execute automatiquement sur programmation.
[2021-02-12 10:45:02][SCENARIO] Exécution du sous-élément de type [action] : code
[2021-02-12 10:45:02][SCENARIO] Exécution d'un bloc code
[2021-02-12 10:45:02][SCENARIO] Donnees horaires non presentes. Replanification de la tache. Derniere collecte horaire EDF presente: 2021-02-11 00:00:00
[2021-02-12 10:45:02][SCENARIO] Fin correcte du scénario
------------------------------------

Et un petit exemple de ce qu’il est possible de faire dans une vue (les données sont en cours de construction)

1 « J'aime »

Bonjour,
Super tuto. J’avais aussi codé un scénario pour heures pleines et heures creuses mais très approximatif par rapport à ce que tu as fait, avec d’ailleurs un résultat peu précis.

Dans ton scénario, tu supposes qu’il n’y a qu’une période d’heures creuses pas période de 24h. Sauf que certains comme moi, ont 2 périodes d’heures creuses dans la journée. Par exemple j’ai 1h00-7h00, et 12h00-14h00.
Je vais regarder pour ajuster ton code à mon besoin mais certains moins aguerris auront peut-être du mal.

Bonjour,

oui j’ai pris en compte uniquement mon besoin ^^
Il faut donc adapter selon ses périodes. J’ai essayé de mettre un maximum de commentaires pour que ce soit compréhensible et qu’on puisse l’apdater.

Je peux regarder 5mn pour mettre un commentaire en cas de 2 créneaux d’heure creuse, mais l’idée est la même, c’est juste qu’il faut plus de variables et surtout penser dans le calcul à ne pas mettre la « minute 0 » car cela correspond à la consommation des 30 dernières minutes :slight_smile:

1 « J'aime »

Petite mise à jour pour permettre de récupérer l’historique si jamais il y a une coupure de l’API ENEDIS sur plusieurs jours.

Attention, se base sur la variable « EDF_Last_Var_Update ». Par ailleurs je considère que les données ne seront pas encore lissée par la mise en historique et donc qu’elles seront sur 30mn pour les X jours qui auront été ajoutés lors de la dernière maj par le plugin-enedis.

Je n’ai pas pu tester le scénario en condition réelle pour le moment, j’aurai le résultat quand les données reviendront.

Cependant le run est fonctionnel:

[2021-02-14 00:31:39][SCENARIO] Start : Scenario lance manuellement.
[2021-02-14 00:31:39][SCENARIO] Exécution du sous-élément de type [action] : code
[2021-02-14 00:31:39][SCENARIO] Exécution d'un bloc code
[2021-02-14 00:31:39][SCENARIO] Attention: 3 jours a updater
[2021-02-14 00:31:39][SCENARIO] Donnees horaire non presente. Replanification de la tache. Derniere collecte horaire EDF presente: 2021-02-11 00:00:00
[2021-02-14 00:31:39][SCENARIO] Fin correcte du scénario

Code:

## Vérification de la dernière exécution réussie

$commande_conso_hier_hp = cmd::byString("#[Appartement][Variables EDF][Conso Jour HP]#");
$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 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 = $scenario->getData("EDF_Last_Var_Update");
		$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("#[Appartement][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
		$debut_heure_pleine = $scenario->getData("EDF_HP_Start");
		$debut_heure_creuse = $scenario->getData("EDF_HC_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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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"));
			$conso_hier_hp = history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc)["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 {

				$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;
				$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("#[Technique][Tools][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();
	}

}

Je mettrai à jour ce poste en cas d’erreur de code. N’hésitez pas à me remonter tout bug.

Hello,

pour info suite à la remise en état du service, j’ai testé le scénario et recalculer toutes les données et c’est bien fonctionnel :

------------------------------------
[2021-02-16 17:13:56][SCENARIO] Start : Scenario lance manuellement.
[2021-02-16 17:13:56][SCENARIO] Exécution du sous-élément de type [action] : code
[2021-02-16 17:13:56][SCENARIO] Exécution d'un bloc code
[2021-02-16 17:13:56][SCENARIO] Attention: 5 jours a updater
[2021-02-16 17:13:56][SCENARIO] Ajout du 2021-02-11
[2021-02-16 17:13:57][SCENARIO] conso_hier_hp = 13.53
[2021-02-16 17:13:57][SCENARIO] conso_hier_hc = 8.74
[2021-02-16 17:13:57][SCENARIO] tarif_hier_hp = 2.336631
[2021-02-16 17:13:57][SCENARIO] tarif_hier_hc = 1.132704
[2021-02-16 17:13:57][SCENARIO] tarif_hier = 3.469335
[2021-02-16 17:13:57][SCENARIO] conso_mois_hc = 23.94
[2021-02-16 17:13:57][SCENARIO] conso_mois_hp = 39.87
[2021-02-16 17:13:57][SCENARIO] moyenne_conso_jour_hc = 7.980000
[2021-02-16 17:13:57][SCENARIO] moyenne_conso_jour_hp = 13.290000
[2021-02-16 17:13:57][SCENARIO] jour_restant_mois = 17
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois_hc = 159.6
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois_hp = 265.8
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois = 425.4
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_hc = 20.68416
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_hp = 45.90366
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_global = 78.18782
[2021-02-16 17:13:57][SCENARIO] Ajout du 2021-02-12
[2021-02-16 17:13:57][SCENARIO] conso_hier_hp = 12.38
[2021-02-16 17:13:57][SCENARIO] conso_hier_hc = 7.995
[2021-02-16 17:13:57][SCENARIO] tarif_hier_hp = 2.138026
[2021-02-16 17:13:57][SCENARIO] tarif_hier_hc = 1.036152
[2021-02-16 17:13:57][SCENARIO] tarif_hier = 3.174178
[2021-02-16 17:13:57][SCENARIO] conso_mois_hc = 31.94
[2021-02-16 17:13:57][SCENARIO] conso_mois_hp = 52.25
[2021-02-16 17:13:57][SCENARIO] moyenne_conso_jour_hc = 7.985000
[2021-02-16 17:13:57][SCENARIO] moyenne_conso_jour_hp = 13.062500
[2021-02-16 17:13:57][SCENARIO] jour_restant_mois = 16
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois_hc = 159.7
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois_hp = 261.25
[2021-02-16 17:13:57][SCENARIO] estimation_conso_mois = 420.95
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_hc = 20.69712
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_hp = 45.117875
[2021-02-16 17:13:57][SCENARIO] estimation_prix_mois_global = 77.414995
etc...
[2021-02-16 17:13:58][SCENARIO] Fin correcte du scénario

Si vous avez des idées d’amélioration/ ajouts, n’hésitez pas.

PS: pour pouvoir tout retraiter, j’ai dû refaire un autre objet Enedis et relancer une synchro pour récupérer les 7 derniers jours et les recopier sur mon vrai historique (le plugin n’a pas récupéré la diff). Le scénario a ensuite automatiquement recalculé les jours manquants.

Attention, petite coquille sur un format de date du fait que j’ai rajouté l’heure dans la variable mise à jour à la fin du scénario. Je mets à jour tout le code dans le premier post pour que ce soit plus simple.

La coquille au début :
Remplacer:
$derniere_maj_donnees = $scenario->getData("EDF_Last_Var_Update");

par:
$derniere_maj_donnees = date('Y-m-d',strtotime($scenario->getData("EDF_Last_Var_Update")));

1 « J'aime »

Hello

Ton scenario m’intéresse beaucoup sauf qu’ a partir de la tu m’as perdu : :upside_down_face: :grinning:

et ses variables:

#[Appartement][Variables EDF][Conso Jour HP]#
#[Appartement][Variables EDF][Conso Jour HC]#
#[Appartement][Variables EDF][Tarif Jour HP]#
#[Appartement][Variables EDF][Tarif Jour HC]#
#[Appartement][Variables EDF][Tarif Jour]#
#[Appartement][Variables EDF][Conso Mois HP]#
#[Appartement][Variables EDF][Conso Mois HC]#
#[Appartement][Variables EDF][Moyenne Conso Jour HP]#
#[Appartement][Variables EDF][Moyenne Conso Jour HC]#
#[Appartement][Variables EDF][Estimation Conso Mois HP]#
#[Appartement][Variables EDF][Estimation Conso Mois HC]#
#[Appartement][Variables EDF][Estimation Conso Mois]#
#[Appartement][Variables EDF][Estimation Prix Mois HP]#
#[Appartement][Variables EDF][Estimation Prix Mois HC]#
#[Appartement][Variables EDF][Estimation Prix Mois Global]#
Les variables à créer et personnaliser les valeurs

image

EDF_HP_Start
EDF_HC_Start
EDF_Tarif_KWh_HP
EDF_Tarif_KWh_HC
EDF_Tarif_Abo_Mois
EDF_Last_Var_Update (information mise à jour par le scénario)
Le scénario scheduler (mutualisable)

Je fais appel à ce scénario avec envoi de tags pour relancer les calculs
image

Le scénario de création des variables

Version code du 17/02/2021

## Vérification de la dernière exécution réussie

$commande_conso_hier_hp = cmd::byString("#[Appartement][Variables EDF][Conso Jour HP]#");
$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 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")));
		$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("#[Appartement][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
		$debut_heure_pleine = $scenario->getData("EDF_HP_Start");
		$debut_heure_creuse = $scenario->getData("EDF_HC_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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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"));
			$conso_hier_hp = history::getStatistique($id_commande_ConsoHoraire, $debut_hp, $debut_hc)["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 {

				$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;
				$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("#[Technique][Tools][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();
	}

}
Il ne reste ensemble qu’à mettre un cron à une horaire juste après l’aléatoire du plugin Enedis.

image

De ce fait pourrait tu mettre des captures d’ecran pour que je comprenne mieux comment procédé ?

Merci

Où en es-tu exactement ? Car en gros tu as cité pratiquement tout le tutoriel ^^

Bonjour,
J’ia bien appliqué ton code en prenant soin d’adapter et créé tous les variables et objets.

j’ai bien environs 18000 données à uploader mais mon code ne rentre pas dans la condition étant donné que mes dernières données sont du 24-02-21 23:30 et nous sommes le 25-02-21.

je mets une capture d’écran de mon log.

Ok c’est très simple, il faut mettre la bonne date pour la variable suivante:

image

Au maximum tu dois avoir 7j en données horaires qui sont en période de 30mn, si tu as récréé le compteur. Il faut donc mettre la date au 18 ou au 19 février. Si tu mets plus, les données historisées ne seront pas bonnes et donc le calculs seront faux.

Il faudrait que je mette une valeur par défaut si la variable est vide.

J’ai effectivement mis la variable au 18 février mais rien ne change en relançant le scénario.
Juste les jours à updater sont modifiés.


En fait la date de collect n’est pas à aujourd’hui, du coup pour lui il n’a encore pas les données pour s’éxécuter. Je n’ai pas calculé qu’on est le 25. Normalement, le plugin enedis aurait du rajouter une valeur au 25 février à minuit comme la capture ci-dessous.

image

C’est bien ce qu’il me semblait. donc je vais attendre 00H01 ce soir pour voir ce qu’il se passe.

J’ai une autre question car je dispose de 2 trames horaires HP HC comment doit on modifier le code ?

Merci pour ton aide

Tu peux relancer la synchro avec la commande Rafraichir au niveau de l’objet compteur.

Sinon voici une nouvelle version qui intègre la partie 2 plages horaires.
J’ai également ajouté un contrôle des variables et set par défaut si jamais elles existent pas.
Je n’ai pas contre pas pu tester vu que je n’ai pas 2 créneaux.

Je suis parti sur cette hypothèse en terme de plages horaires:

image

En terme de variables:
Heures pleines = Minuit j-1 => $debut_hc + $debut_hp => $debut_hc2 + $debut_hp2 => minuit
Dans le cas présent:
$debut_hc => EDF_HC_Start = 02:00
$debut_hp => EDF_HP_Start = 07:00
$debut_hc2 => EDF_HC2_Start = 14:00
$debut_hp2 => EDF_HP2_Start = 17:00

@getbes , si tu veux également tester.

Si vos retours sont positifs, je mettrai à jour le sujet principale :slight_smile:

## 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", "06:30") : null;
(empty($scenario->getData("EDF_HC_Start"))) ? $scenario->setData("EDF_HC_Start", "22:30") : null;
(empty($scenario->getData("EDF_HP2_Start"))) ? $scenario->setData("EDF_HP2_Start", "A REMPLIR") : null;
(empty($scenario->getData("EDF_HC2_Start"))) ? $scenario->setData("EDF_HC2_Start", "A REMPLIR") : 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("#[Appartement][Variables EDF][Conso Jour HP]#");
$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("#[Appartement][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("#[Appartement][Variables EDF][Conso Jour HC]#");
		$commande_tarif_hier_hp = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HP]#");
		$commande_tarif_hier_hc = cmd::byString("#[Appartement][Variables EDF][Tarif Jour HC]#");
		$commande_tarif_hier = cmd::byString("#[Appartement][Variables EDF][Tarif Jour]#");
		$commande_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Conso Mois HP]#");
		$commande_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Conso Mois HC]#");
		$commande_moyenne_conso_jour_hp = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HP]#");
		$commande_moyenne_conso_jour_hc = cmd::byString("#[Appartement][Variables EDF][Moyenne Conso Jour HC]#");
		$commande_estimation_conso_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HP]#");
		$commande_estimation_conso_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois HC]#");
		$commande_estimation_conso_mois = cmd::byString("#[Appartement][Variables EDF][Estimation Conso Mois]#");
		$commande_estimation_prix_mois_hp = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HP]#");
		$commande_estimation_prix_mois_hc = cmd::byString("#[Appartement][Variables EDF][Estimation Prix Mois HC]#");
		$commande_estimation_prix_mois = cmd::byString("#[Appartement][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("#[Technique][Tools][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();
	}

}

Bonjour,

Merci pour l’évolution. Pas trop de temps en ce moment. Je vais essayer la semaine prochaine et je te fais un retour.

Pas de soucis. Faut voir avec @yannou ce qu’il en pense du coup. De mon côté le scénario continue de bien marcher pour le mode 1 plage horaire.

Il faudra bien penser à renseigner les plages et aussi compléter la variable qui indique leur nombre (seté à 1 par défaut). Lancer une première fois le scénario devrait créer toutes les variables avec les valeurs par défaut. A exécuter de manière indépendante car sinon la suite du scénario se déroule, ou sinon il faudra simplement penser à enlever 1j à la variable « EDF_Last_Var_Update » afin qu’il refasse les calculs proprement avec les bonnes valeurs de variables (ou encore les renseigner directement dans le code qui set les variables par défaut :wink: ).

Merci pour tes modifications c’est gentil de te pencher sur nos problèmes :slight_smile:

Jai integrer ton codes avec tes nouvelles variables. j’ai rafraîchi l’objet compteur mais rien n’y fait :browning:

Tu peux mettre le plugin enedis en mode debug et mettre les logs une fois ton refresh effectué ?
C’est bizarre que tes données datent toujours de la veille, normalement la valeur de minuit (qui représente la consommation des 30 dernières minutes de la journée) devrait être présente également.

Et tout cas c’est le comportement que j’ai, je ne sais pas si d’autres personnes peuvent le confirmer.

Voilà ce que j’ai en mode débug après un rafraissiment. Bizarre effectivement…