Some home automation

Talks about metering
jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Some home automation

Post by jeanmarc » Mon Aug 27, 2018 9:27 am

Hello,
I know some of you want more home automation with mN. I first thought into integrating that into the interface, but i finally believe it is more cleaver to leave that appart since there is so many posibilities for each installations that could have cluttered the GUI.
Also it is actually quiet simple to get things done easly with some smalls scripts.

Here some example (i will upload them later on), you can request them using a crontab job or a systemd timer.

A simple script to command something if a meter exceeded or is under a value over the day. (eg above 15kWh)

Code: Select all

#!/usr/bin/php
<?php
// A simple script to command something if a meter exceeded or is under a value over the day.

$MNDIR  = '/srv/http/metern'; // Path to meterN
$METNUM = 1; // meter number
$MAXVAL = 15000; // eg 15kWh
$MAXCMD = ''; // If over $MAXVAL, do something
$LOWVAL = 15000;
$LOWCMD = ''; // If under $LOWVAL

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$METNUM.php");
date_default_timezone_set($DTZ);

$today      = date('Ymd');
$lines      = file($MNDIR . "/data/csv/$today.csv");
$contalines = count($lines);
if ($contalines > 2) {
    $val_last  = null;
    $val_first = null;
    $array     = preg_split('/,/', $lines[1]);
    if (isset($array[$METNUM])) {
        $val_first = trim($array[$METNUM]);
        settype($val_first, 'float');
    }
    $array = preg_split('/,/', $lines[$contalines - 1]);
    if (isset($array[$METNUM])) {
        $val_last = trim($array[$METNUM]);
        settype($val_last, 'float');
    }
    
    if (!empty($val_first) && !empty($val_last)) { // Meter
        if ($val_first <= $val_last) {
            $val_last -= $val_first;
        } else { // counter pass over
            $val_last += ${'PASSO' . $METNUM} - $val_first;
        }
    }
    
    if ($val_last > $MAXVAL) { // over
        exec("$MAXCMD", $output);
    }
    if ($val_last < $LOWVAL) { // under
        exec("$LOWCMD", $output);
    }
}
?>
A simple script to command something if a meter or a sensor exceeded a live value. (eg over 150W)

Code: Select all

<?php
// A simple script to command something if a meter or a sensor exceeded a live value.

$MNDIR  = '/srv/http/metern'; // Path to meterN
$METNUM = 1; // meter/sensor number
$MAXVAL = 150; // eg 150 W
$MAXCMD = ''; // Do something

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$METNUM.php");
include("$MNDIR/config/memory.php");
date_default_timezone_set($DTZ);

if (file_exists($LIVEMEMORY)) {
    $data   = file_get_contents($LIVEMEMORY);
    $array  = json_decode($data, true);
    $nowutc = strtotime(date('Ymd H:i:s'));
    
    if ($nowutc - $array['UTC'] < 15 && isset($array['UTC']) && $array["${'METNAME'.$METNUM}$METNUM"] > $MAXVAL) {
        exec("$MAXCMD", $output);
    }
}
?>
A simple script to command something if a house production meter's is over a house consumption's meter. (eg to command a boiler)

Code: Select all

<?php
// A simple script to command something if a house production meter's is over a house consumption's meter.

$MNDIR      = '/srv/http/metern'; // Path to meterN
$CONSMETNUM = 1; // Consumption meter number
$PRODMETNUM = 4; // Production meter number

$OVERT   = 150; // over threshold
$OVERCMD = 'ls'; // Do something
$UNDERT  = 0; // under threshold
$UNDCMD  = ''; // Do something

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$CONSMETNUM.php");
include("$MNDIR/config/config_met$PRODMETNUM.php");
include("$MNDIR/config/memory.php");
date_default_timezone_set($DTZ);

if (file_exists($LIVEMEMORY)) {
    $data   = file_get_contents($LIVEMEMORY);
    $array  = json_decode($data, true);
    $nowutc = strtotime(date('Ymd H:i:s'));
    
    if ($nowutc - $array['UTC'] < 15 && isset($array['UTC'])) {
		$val = $array["${'METNAME'.$PRODMETNUM}$PRODMETNUM"] - $array["${'METNAME'.$CONSMETNUM}$CONSMETNUM"];
        if ($val >= $OVERT) {
            exec("$OVERCMD", $output);
        }
        
        if ($val <= $UNDERT) {
            exec("$UNDCMD", $output);
        }
    }
}
?>
Tell me if it fits to your needs :)

giulio
Posts: 22
Joined: Tue Aug 28, 2018 1:37 pm

Re: Some home automation

Post by giulio » Mon Nov 19, 2018 6:32 pm

Hi!
I'm trying to use the script to command something if a meter exceeds a real live value( electric withdrawals).

I have to use a sonoff with espeasy, which commands a 25 amp relay (NC) to detach a load when the meter exceeds the set value

so I modified the jeanmarc script:

Code: Select all

<? Php
// A simple script to command something if a meter or a sensor exceeded a live value.

$ MNDIR = '/ var / www / metern'; // Path to meterN
$ METNUM = 3; // meter / sensor number
$ MAXVAL = 3300; // eg 150 W
$ MAXCMD = 'http://192.168.1.139/control?cmd=GPIO,12,1'; // Do something

// No edit should be needed bellow
if (isset ($ _ SERVER ['REMOTE_ADDR'])) {
    die ('Direct access not permitted');
}
define ('checkaccess', TRUE);
It includes ( "$ MNDIR / config / config_main.php");
It includes ( "$ MNDIR / config / config_met $ METNUM.php");
It includes ( "$ MNDIR / config / memory.php");
date_default_timezone_set ($ DTZ);

if (file_exists ($ LIVEMEMORY)) {
    $ data = file_get_contents ($ LIVEMEMORY);
    $ array = json_decode ($ data, true);
    $ nowutc = strtotime (date ('Ymd H: i: s'));
    
    if ($ nowutc - $ array ['UTC'] <15 && isset ($ array ['UTC']) && $ array ["$ {'METNAME'. $ METNUM} $ METNUM"]> $ MAXVAL) {
        exec ("$ MAXCMD", $ output);
    }
}
?>
... I'm not an expert, I do not know how to start the script even when I restart the raspberry

.. also, I would like the script to do a check after 10 minutes and if the value of the meter is less than the set value, reactivate the load with another command to the sonoff, otherwise it remains in its state.

..it's possible ??
Thank you

jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Re: Some home automation

Post by jeanmarc » Mon Nov 19, 2018 7:40 pm

Hi,
thanks trying this one :)
1) You should make the script wide available ln -s /srv/http/comapps/over_script.php /usr/bin/over_script
Try over_script alone, if it work you can continue.
2) Set a systemd timer in /etc/systemd/system/over.timer

Code: Select all

[Unit]
Description=over_script timer

[Timer]
OnBootSec=5min
OnCalendar=*:0/10
Unit=over.service

[Install]
WantedBy=multi-user.target
3) A service file /etc/systemd/system/over.service

Code: Select all

[Unit]
Description=over_script

[Service]
Type=oneshot
ExecStart=over_script

[Install]
WantedBy=over.target
4)
systemctl start over.timer
systemctl enable over.timer

jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Re: Some home automation

Post by jeanmarc » Mon Nov 19, 2018 7:53 pm

Also if you wish to start if above power and stop if under, use this :

Code: Select all

<?php
// A simple script to command something if a meter or a sensor exceeded a live value.

$MNDIR  = '/srv/http/metern'; // Path to meterN
$METNUM = 1; // meter/sensor number
$MAXVAL = 150; // eg 150 W
$MAXCMD = 'http://192.168.1.139/control?cmd=GPIO,12,1'; // Do something
$UNDCMD = '';

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$METNUM.php");
include("$MNDIR/config/memory.php");
date_default_timezone_set($DTZ);

if (file_exists($LIVEMEMORY)) {
    $data   = file_get_contents($LIVEMEMORY);
    $array  = json_decode($data, true);
    $nowutc = strtotime(date('Ymd H:i:s'));
    
    if ($nowutc - $array['UTC'] < 15 && isset($array['UTC']) && $array["${'METNAME'.$METNUM}$METNUM"] > $MAXVAL) {
        exec("$MAXCMD", $output);
    } else {
        exec("$UNDCMD", $output);
    }
}
?>

giulio
Posts: 22
Joined: Tue Aug 28, 2018 1:37 pm

Re: Some home automation

Post by giulio » Tue Nov 20, 2018 7:18 pm

point 1)
tried alone over_script.php

this happens

JARVIS: MyScripts -> php over_script.php
sh: 1: http://192.168.1.139/control?cmd=GPIO,12,1: not found

jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Re: Some home automation

Post by jeanmarc » Tue Nov 20, 2018 8:21 pm

obviously ;)

Code: Select all

<?php
// A simple script to command something if a meter or a sensor exceeded a live value.

$MNDIR  = '/srv/http/metern'; // Path to meterN
$METNUM = 1; // meter/sensor number
$MAXVAL = 150; // eg 150 W
$MAXCMD = 'http://192.168.1.139/control?cmd=GPIO,12,1'; // Do something
$UNDCMD = '';

// No edit should be needed bellow
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}
define('checkaccess', TRUE);
include("$MNDIR/config/config_main.php");
include("$MNDIR/config/config_met$METNUM.php");
include("$MNDIR/config/memory.php");
date_default_timezone_set($DTZ);

if (file_exists($LIVEMEMORY)) {
    $data   = file_get_contents($LIVEMEMORY);
    $array  = json_decode($data, true);
    $nowutc = strtotime(date('Ymd H:i:s'));
    
    if ($nowutc - $array['UTC'] < 15 && isset($array['UTC']) && $array["${'METNAME'.$METNUM}$METNUM"] > $MAXVAL) {
	    $ch = curl_init($MAXCMD);
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$MAXCMD", $output);
    } else {
        //exec("$UNDCMD", $output);
    }
}
?>

giulio
Posts: 22
Joined: Tue Aug 28, 2018 1:37 pm

Re: Some home automation

Post by giulio » Tue Nov 20, 2018 10:24 pm

now it's okay if the value exceeds the threshold

but if the value is below the threshold nothing happens

I entered :
$UNDCMD = 'http://192.168.1.139/control?cmd=GPIO,12,0';

jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Re: Some home automation

Post by jeanmarc » Wed Nov 21, 2018 7:31 am

...

Code: Select all

} else {
	    $ch = curl_init($UNDCMD );
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 5000); // error
	    curl_exec($ch);
	    curl_close($ch);
        //exec("$UNDCMD", $output);
    }
Maybe it's wiser to make a continuous reading ? Also "prod_over_cons_script" is better to command someting if your production meter is over a consumption. But you need to know it ;)

giulio
Posts: 22
Joined: Tue Aug 28, 2018 1:37 pm

Re: Some home automation

Post by giulio » Fri Nov 23, 2018 3:20 pm

... I do not want to control the production, but the electric samples that do not have to exceed a maximum value before having to intervene the counter of the electricity supplier located on the road.
will the over_script.php script do a continuous check ??


step 2) Set a systemd timer in /etc/systemd/system/over.timer
..I would like to understand how it works, in case I want to modify it

I apologize for the questions too trivial, but I have to learn a lot

so also point 3) and 4) ... how they work

jeanmarc
Posts: 1600
Joined: Thu Aug 29, 2013 7:16 am

Re: Some home automation

Post by jeanmarc » Fri Nov 23, 2018 4:46 pm

I suppose your inverter will output an error if it exceed a certain grid max power.
over_script.php only check on request, if you need constant monitroing you better make a daemon from it.

3&4 it's the cron job remplacement, ckeck this for more details.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests