Skip to content

Instantly share code, notes, and snippets.

@secbyd
Last active July 24, 2023 13:12
Show Gist options
  • Save secbyd/94c2928451ec1782ecc0905da8bd6ce5 to your computer and use it in GitHub Desktop.
Save secbyd/94c2928451ec1782ecc0905da8bd6ce5 to your computer and use it in GitHub Desktop.
Query database for current price and update FHEM accordingly
#!/usr/bin/perl -l
use strict;
use warnings;
use DBI;
use Net::Telnet;
my $dbName="electricity";
my $dbUser="********";
my $dbPass="********";
my $dbHost="127.0.0.1";
my $dbPort=3306;
my $fhemHost="127.0.0.1";
my $fhemPort=7072;
my $fhemDummy="negElecPrice";
my $dbh = DBI->connect("DBI:mysql:database=$dbName;host=$dbHost;port=$dbPort",
$dbUser, $dbPass,
{'RaiseError' => 1});
my $query = "SELECT marketPrice FROM prices WHERE fromDate = DATE_FORMAT(NOW(),'%Y-%m-%d %H:00:00') AND marketPrice<0;";my $sth = $dbh->prepare($query);
$sth->execute();
my $row = $sth->fetchrow_hashref;
my $priceisnormal = 1;
my $telnet = new Net::Telnet (
Timeout=>10,
Port=>$fhemPort,
);
$telnet->open($fhemHost);
while ($row) {
$priceisnormal = 0;
$telnet->print("set $fhemDummy on");
$row = $sth->fetchrow_hashref;
}
if ($priceisnormal) {
$telnet->print("set $fhemDummy off");
}
$telnet->print('quit');
$telnet->close;
$sth->finish();
$dbh->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment