espeasy sonoff pulse counter Metern

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 11, 2019 9:53 pm

Yup all correct

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Tue Nov 12, 2019 7:25 am

:arrow: Well about UTC value, it should be higher a minute late. If it's the same, there is a problem.

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Thu Nov 14, 2019 5:20 pm

So, is it fixed now ? :shifty:

megamarco83
Posts: 97
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Thu Nov 14, 2019 10:05 pm

jeanmarc wrote:
Thu Nov 14, 2019 5:20 pm
So, is it fixed now ? :shifty:
hi, the part of metern that become stuck when req_espeasy.php is not responding seams to be solved, i mean that i never find till now metern stuck...:-)
the part of implementing "ping" of metern to discover if metern is stuck are on going. a friend of mine is help me i will come here in some days :-)

megamarco83
Posts: 97
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Fri Nov 15, 2019 1:16 pm

hi JM, to know if metern is stuck i'm lookin at:
/dev/shm/mN_LIVEMEMORY.json and extract the UTC parameter, if i will find same value for three times in three different minutes i send a notification.

now i would like to make some tests, but if i go in admin page and turn off meterN, the file mN_LIVEMEMORY.json disappear.
just a cofirmation, when meterN is stucked, this file still present, right?

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Fri Nov 15, 2019 3:04 pm

Hi,
If mN is stop the file won't exist, if it is ever stuck by a com. app. the UTC won't increase. I haven't test but it should do the trick

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		// do something

		}
} else { // ain't running
	die("Abording: mN not running \n");
}
?>

megamarco83
Posts: 97
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Mon Nov 25, 2019 4:09 pm

hi JM

i'm using this bast script:

Code: Select all

#!/bin/bash

## 
## This script checks the field UTC of mN_LIVEMEMORY.json
## If it is equal for 3 minutes it will send On to the domoticz switch signalSwitch
## if domoticz recognize this switch turned on, domoticz will send a notification at mobile phone.
## 
## create virtual switch in domoticz and remember id
## modify script using your settings and save this script in your bash script dir
## add
## @reboot sleep 600 && /var/www/MyScripts/ping_metern.sh   
## to crontab (sudo nano /etc/crontab)
##
## start the script initial manually with        nohup /var/www/MyScripts/ping_metern.sh &
##
##

## Enter your settings below this line

file=/dev/shm/mN_LIVEMEMORY.json
##file=/dev/shm/test.json
domoticzIP='192.168.0.105'
domoticzPort=8085
signalSwitch=152

## No changes required below this line

sameCounter=0
oldUTC=0
jsonKey=UTC
domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd="

function getJSONValue()
{
   awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$1'\042/){print $(i+1)}}}' | tr -d '"' | sed -n 1p
}

function signalDomoticz()
{
    curl $1$2
}

## crontab at boot

while true; do

    liveUTC=$(getJSONValue $jsonKey < $file)
    if [ $oldUTC == $liveUTC ]; then
       ((sameCounter++))
       echo same $(date -d @$liveUTC)
       if  ( ((sameCounter % 60 == 0 )) && (( sameCounter > 0 )) ) || [[ $sameCounter -eq 3 ]]  ;then  # after 3 times and every 60th time
         signalDomoticz $domoticzURL "On"
       fi
    else 
       # echo different $(date -d @$liveUTC)
       sameCounter=0
    fi  
    oldUTC=$liveUTC
    sleep 60

done
with this script i'm able to turn "On" a dummy device in domoticz, and with rules in domoticz, if this device is On i will send a notification at my phone.
this script works good, but sometimes i receive a notification that metern is stuck, but in reality meterN is not stuck.
it's strange but when it happen it's always in the afternoon...i do not think if it's linked to something or not

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Mon Nov 25, 2019 7:33 pm

Hi Marco,
I'am not too good at bash. Maybe save oldUTC and liveUTC values in a tmp file when it happen to see what could be wrong.

megamarco83
Posts: 97
Joined: Tue Apr 14, 2015 9:25 am

Re: espeasy sonoff pulse counter Metern

Post by megamarco83 » Thu Nov 28, 2019 3:18 pm

hi JM, and using PHP?

if i will create a PHP on folder /var/www/MyScripts/ping_metern.php

and i want that if metern has an old UTC i turn on a dummy device with this code:

http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd=On


can you please help me on setting this php?

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105'
$domoticzPort=8085
$signalSwitch=152
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd="

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo("switch on dummy device on domoticz");
		redirect("$domoticzURL"On)
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
i think that i need to modify crontab to run at every boot:

sudo nano /etc/crontab and add the php:

@reboot sleep 610 && php /var/www/MyScripts/ping_metern.php
that's correct or i should remove php?

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

Re: espeasy sonoff pulse counter Metern

Post by jeanmarc » Thu Nov 28, 2019 5:06 pm

Hi,
Something like this (haven't test)

Code: Select all

#!/usr/bin/php
<?php
if (isset($_SERVER['REMOTE_ADDR'])) {
    die('Direct access not permitted');
}

$domoticzIP='192.168.0.105'
$domoticzPort=8085
$signalSwitch=152
$domoticzURL="http://$domoticzIP:$domoticzPort/json.htm?type=command&param=switchlight&idx=$signalSwitch&switchcmd="
$log       = '/tmp/ping_metern.log';

if (file_exists('/dev/shm/mN_LIVEMEMORY.json')) {
	$data     = file_get_contents('/dev/shm/mN_LIVEMEMORY.json');
	$memarray = json_decode($data, true);
	$nowUTC   = strtotime(date("Ymd H:i:s"));
		if ($nowUTC - $memarray["UTC"] > 300) { // too old
		echo("switch on dummy device on domoticz");
		$remotedata = file_get_contents($domoticzURL);
		
		$stamp = date('d/m/Y H:i:s');
	    $data = "$stamp : $nowUTC - $memarray['UTC']\n";
	    file_put_contents($log, $data, FILE_APPEND);
		}
} else { // ain't running
	die("Aborting: mN not running \n");
}
?>
ln -s /srv/http/comapps/ping_metern.php /usr/bin/ping_metern and chmod +x ping_metern.php. Then simply call it with ping_metern.
Try it, erros should be logged in /tmp/ping_metern.log.

You can use crontab job or better a systemd timer, in /etc/systemd/system :
ping_metern.service :

Code: Select all

[Unit]
Description=ping_metern
After=metern.target

[Service]
Type=oneshot
ExecStart=ping_metern

[Install]
WantedBy=multi-user.target

Code: Select all

[Unit]
Description=ping_metern
ping_metern.timer : 
[Timer]
OnBootSec=1min
#OnCalendar=5min
OnUnitActiveSec=5min
Unit=ping_metern.service

[Install]
WantedBy=multi-user.target
systemctl enable ping_metern.timer & systemctl start ping_metern.timer. check with systemctl status ping_metern.timer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest