electronique:fournisseur-electricite-barry:barry-connaitre-le-cout-horaire-du-kwh

Barry, connaître le coût horaire du kWh

Parlons programmation et domotique !

Afin d'adapter les appareils qui consomment beaucoup d'énergie (chauffe-eau, chauffage par accumulation, charge de voiture…) au meilleur tarif horaire, je me lance dans la conception et la réalisation d'un commutateur intelligent.

Tout d'abord, je dois pouvoir anticiper le prix du kWh journalier. Barry propose une API dont la documentation se trouve à l'adresse https://developer.barry.energy/. Cette API va me permettre de me connecter sur les serveurs de Barry afin d'obtenir des informations. La méthode getMeteringPoints permet de connaître le coût du prix du kWh à chaque heure. Je vais retenir 8 tranches horaires qui me permettent de savoir quand c'est le meilleur moment pour consommer.

Voici un extrait de code en PHP pour interroger les API fournis par Barry Energy.

Aujourd'hui, il y a 3 méthodes disponibles : getMeteringPoints, getAggregatedConsumption et getAggregatedConsumption.

<?PHP
 
 
$priceArea = array(
  'dk' => 'DK_NORDPOOL_SPOT_DK1',
  'fr' => 'FR_EPEX_SPOT_FR'
);
 
 
function getMethod($postData) {
  // Le token suivant n'est pas valide. Il faut le modifier avec le votre, obtenu dans l'application Barry Eenrgy
  $authToken = 'Bx6Da6v0h34MM7OhAjCaN9zak+IZLNe9tUlCebw7+LiP8+5SH6BcdNrOY85s9q7Sdfrmc/yyjWrJROlJ9vhlRCYY310TsHcGNodMzr3cGfZOwVYNPYWxGxcZHO94p6W98SJC/TdIYhsE+tRnvMMKHktTdkRjmMolHAtWpYyFzDw=';
  $url = "https://jsonrpc.barry.energy/json-rpc";
 
  echo "<pre>";
  echo '<p>La méthode appelée : </p>';
  print_r(json_encode($postData));
  echo "</pre></br>";
 
 
  // Create the context for the request
  $context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => "Authorization: Bearer {$authToken}\r\n".
            "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
  ));
 
  // Send the request
  $response = file_get_contents($url, FALSE, $context);
 
  // Check for errors
  if($response === FALSE){
      die('Error');
  }
  return $response;
}
 
 
function translateDate($strDate) {
  $nomJourSemaineEN = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
  $nomJourSemaineFR = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche");
  $nomMoisEN = array("January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
  $nomMoisFR = array("Janvier","Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");
  $strDate = str_ireplace($nomMoisEN, $nomMoisFR, $strDate);
  $strDate = str_ireplace($nomJourSemaineEN, $nomJourSemaineFR, $strDate);
  return $strDate;
}
 
 
setlocale(LC_ALL,'fr');
 
//$dateJour=date('d F Y', strtotime('31-12-2018'));
$dateJour=date('d F Y');
$dateJourDemain=date('d F Y', strtotime('+1 day', strtotime($dateJour)));
 
echo "<h1>Métriques de Barry Energy</h1>";
echo "<p>Rapport établi le ".translateDate(date('l d F Y'))." à ".date('H:i:s')."</p>";
 
 
echo "<h2>Etat de l'abonnement</h2>";
 
// The data to send to the API getMeteringPoints
$postData = array('method' => 'co.getbarry.api.v1.OpenApiController.getMeteringPoints'
  , 'id' => 0
  , 'jsonrpc' => '2.0'
  , 'params' => array()
);
 
$response = getMethod($postData);
// Decode the response
$responseData = json_decode($response, TRUE);
 
echo "<pre>";
echo '<p>La réponse : </p>';
print_r($responseData);
echo "</pre>";
 
 
echo "<h2>Consommation du $dateJour </h2>";
 
// The data to send to the API getAggregatedConsumption
$postData = array('method' => 'co.getbarry.api.v1.OpenApiController.getAggregatedConsumption'
  , 'id' => 0
  , 'jsonrpc' => '2.0'
  , 'params' => array(date(DATE_ATOM, strtotime($dateJour)), date(DATE_ATOM, strtotime($dateJourDemain)))
);
 
$response = getMethod($postData);
// Decode the response
$responseData = json_decode($response, TRUE);
 
echo "<pre>";
echo '<p>La réponse : </p>';
print_r($responseData);
echo "</pre>";
 
 
 
echo "<h2>Prix du $dateJour </h2>";
 
// The data to send to the API getPrice
$postData = array('method' => 'co.getbarry.api.v1.OpenApiController.getPrice'
  , 'id' => 0
  , 'jsonrpc' => '2.0'
  , 'params' => array($priceArea['fr'], date(DATE_ATOM, strtotime($dateJour)), date(DATE_ATOM, strtotime($dateJourDemain)) )
 
);
 
$response = getMethod($postData);
// Decode the response
$responseData = json_decode($response, TRUE);
 
echo "<pre>";
echo '<p>La réponse : </p>';
print_r($responseData);
echo "</pre>";
 
  • electronique/fournisseur-electricite-barry/barry-connaitre-le-cout-horaire-du-kwh.txt
  • Dernière modification : 2021/03/03 22:52
  • de Cédric ABONNEL