Pass2PHP

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 04 March 2018 19:25

Oh, that? Switching in pass2php based on Google Calendar items?
Just uploaded the code to github.
You probably should git clone the google api php client instead of downloading my version, but the gcal.php file should work once you have your client running.

Edit: Yes you should, I can't upload the files. There are 5950 files in the Google api and the GIT web interface only allows uploading 100 files.

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Wednesday 14 March 2018 23:08

Something else now :) ,

for all my energy consumption devices, the calculated used energy for today is a negative value by domoticz.
I have already posted this a couple of times on this on this forum, but no one can give me an answer.

So i will calculate it my own in a cron.
What i want todo is:

save the value of [Data] of the device at 00:01 (so 1 minute after a new day starts)
and subtract this from the [Data] at 23:59

Then i will get the used energy consumption of today for that energy device.
I will put this in the cron for 1 minute for example.
Just want to hear your opinion if this is the best way todo this? Or you have a better idea/solution?

Thanks!

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Thursday 15 March 2018 5:45

I would run it in my cron every 2 minutes and store the values in a MySQL database with a replace into query.
Advantage of that would be that you don't need to do calculations, you only update the value each time.
That of course is only possible if the consumption during the day is good calculated.
Another advantage would be that you don't risk anything when the cron is for whatever reason not executed at 0:01. You would only loose some data is the cron is down the latest hours of a day.

Edit: you could also do it in the consumption device script. Then you avoid the cron for it and have all data in realtime in MySQL available.

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Thursday 15 March 2018 10:25

So you mean i put my 'program' into for example: Verbruik Droogkast.php

Verbruik Droogkast.php is placed in \secure\pass2php\
so everytime there is energy used for Droogkast, the Verbruik Droogkast.php is called

There i save the [Data] into the mysql?
But then i will loose the event to save the start consumption at 00:01 (a new day)

Or am i thinking completely wrong? :oops:

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Thursday 15 March 2018 13:08

Yes, in the pass2php\Verbruik droogkast.php file.
Something like this:

Code: Select all

<?php
//Explode the status to array, kWh will be in second array item
$consumption=explode(';',$status);
//Place the second item to a variable
$consumption=$consumption[1];
//Set a var with the date
$date=date("Y-m-d");
//Set a var with the device name or idx or whatever
$device='Droogkast';
//Open the MySQL connection
$db=new mysqli('127.0.0.1','domotica','domotica','domotica');
if($db->connect_errno>0)die('Unable to connect to database ['.$db->connect_error.']');
//Define the query
$query="REPLACE INTO `verbruik` (`date`,`device`,`value`) VALUES ('$date','$device','$value');";
//execute the query
if(!$result=$db->query($query))die('There was an error running the query ['.$query .'-'.$db->error.']');
//close connection
$db->close();
This would only be triggered if the dryer is running and updates instantly the database.
Above code is tested with a kWh device.

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Thursday 15 March 2018 20:53

the $value will be $consumption i guess?

It is not 'replacing' the value, but it's adding everytime a new dataline in my mysqldata

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Thursday 15 March 2018 21:51

You have a primary index on the date column?

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Friday 16 March 2018 0:10

i have changed the date column to primary index now

what if i want all my kwh counters to be added?
if I all want to add them in the mysql table 'Verbruik'
for example i have: droogkast, vaatwas, wasmachine, aquarium, elektriciteit, gas, verlichting_buiten

and i want to know the energy used for today, i need to subtract the value from today-yesterday
?

ropske
Posts: 315
Joined: Tuesday 12 August 2014 5:37
Target OS: Raspberry Pi
Domoticz version: V3_8394
Location: Rumbeke,Belgium
Contact:

Re: Pass2PHP

Post by ropske » Friday 16 March 2018 0:48

because, now the latest updated energy device is overwriting (replace into) the current data (because date column is the primary key)

User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Sunday 29 April 2018 15:03

Just created an extra feature as Telegram was offline this morning due to power outage.
Next to the Telegram Notification also some Google Home notifications.

Based on the instructions here: http://easydomoticz.com/forum/viewtopic.php?f=17&t=5914

Pass2PHP

Code: Select all

function googlehomenotification($msg,$volume=0.5,$jingle=1){								
    if (cget('house_mode')=='Normal')
    {
    	$msg=str_replace('__',PHP_EOL,$msg);
    	shell_exec(domoticz_location.'scripts/pass2php/googlehomenotification.sh "'.$msg.'" "'.$volume.'" "'.$jingle.'" > /dev/null 2>/dev/null &');
 
    }
}
I know it is not a TRUE, PHP solution but it does the job :lol:
At least the system is now speaking to me.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 29 April 2018 15:25

You could call that function if Telegram is offline automatically ;)

User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Sunday 29 April 2018 15:34

Egregius wrote:
Sunday 29 April 2018 15:25
You could call that function if Telegram is offline automatically ;)
hahaha :lol I swear I could have thought of that myself. :lol: :lol: but I didn't.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 21 May 2018 18:27

As I had a faulty HD recently in my RAID configuration and was notified by DSM via e-mail I thought I could do better.
Notification via Telegram / Pushover with urgent notice :D

So I created a Pass2PHP function that could retrieve the status of the HD's but also read the status of the UPS and RAID status.
Synology Pass2PHP UPS.JPG
UPS
Synology Pass2PHP UPS.JPG (60.1 KiB) Viewed 1194 times
In addition I also added the temperature sensors with PHP instead of using the cronjob to do it for me :lol:
https://www.domoticz.com/wiki/NAS_Monitoring
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Monday 21 May 2018 20:54

Can you post the function? Sounds interesting :mrgreen:

User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 21 May 2018 21:22

Egregius wrote:
Monday 21 May 2018 20:54
Can you post the function? Sounds interesting :mrgreen:
You know I'm not 100% php programmer right so ;-) there will be room for improvement.

Code: Select all

function getStringBetween($str,$from,$to)
{
    $sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
    return substr($sub,0,strpos($sub,$to));
}

function snmp_nas()     /* https://global.download.synology.com/download/Document/MIBGuide/Synology_DiskStation_MIB_Guide.pdf    */
{    
    if (status('Synology')=='On')
    {
        $to_snmp = array(     
                        array( 'Device' => "Synology HD1 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.0", 'Type' => "Diskstatus" ),
                        array( 'Device' => "Synology HD2 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.1", 'Type' => "Diskstatus" ),
                        array( 'Device' => "Synology HD3 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.2", 'Type' => "Diskstatus" ),
                        array( 'Device' => "Synology HD4 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.3", 'Type' => "Diskstatus" ),
                        array( 'Device' => "Synology RAID Status", 'Address' => "1.3.6.1.4.1.6574.3.1.1.3.0", 'Type' => "Raidstatus" ),

                        array( 'Device' => "Synology Power Status", 'Address' => "1.3.6.1.4.1.6574.1.3.0", 'Type' => "Status" ),
                        array( 'Device' => "Synology Fan Status", 'Address' => "1.3.6.1.4.1.6574.1.4.2.0", 'Type' => "Status" ),

                        array( 'Device' => "Synology UPS Status", 'Address' => "1.3.6.1.4.1.6574.4.2.1.0", 'Type' => "UPSstatus" ),

                        array( 'Device' => "CpuUser",  'Address' => "1.3.6.1.4.1.2021.11.9.0", 'Type' => "Calculate" ),     // Calculate device only
                        array( 'Device' => "CpuSystem",  'Address' => "1.3.6.1.4.1.2021.11.10.0", 'Type' => "Calculate" ),  // Calculate device only
                        array( 'Device' => "Memtotal",  'Address' => "1.3.6.1.4.1.2021.4.5.0", 'Type' => "Calculate" ),     // Calculate device only
                        array( 'Device' => "MemShared",  'Address' => "1.3.6.1.4.1.2021.4.13.0", 'Type' => "Calculate" ),   // Calculate device only
                        array( 'Device' => "MemBuffer",  'Address' => "1.3.6.1.4.1.2021.4.14.0", 'Type' => "Calculate" ),   // Calculate device only
                        array( 'Device' => "MemCached",  'Address' => "1.3.6.1.4.1.2021.4.15.0", 'Type' => "Calculate" ),   // Calculate device only
                        array( 'Device' => "MemAvailable", 'Address' => "1.3.6.1.4.1.2021.4.6.0", 'Type' => "Calculate" ),  // Calculate device only

                        array( 'Device' => "Synology HD1 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.0" ),
                        array( 'Device' => "Synology HD2 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.1" ),
                        array( 'Device' => "Synology HD3 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.2" ),
                        array( 'Device' => "Synology HD4 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.3" ),
                        array( 'Device' => "Synology System Temperature",  'Address' => "1.3.6.1.4.1.6574.1.2.0" )

                    );

        foreach ($to_snmp as $snmp) 
        {
            $result = snmp2_get(snmp_ip_nas, snmp_community, $snmp['Address']);   // These results need cleaning ;-)
        
            if (strpos($result, "Opaque: Float: ") === 0)                         // First remove the floats
            {
                $result = str_replace("Opaque: Float: ","",$result);
                $result = substr($result, 0, strpos($result, "."));
            }
            elseif (strpos($result, 'STRING: "') === 0)                           // THe UPS Status is a string we need to retrieve the data here
            {
                $from = '"';
                $to = '"';
                $result = getStringBetween($result,$from,$to);

                if ($result === "OL") $result = '1:Online';                       // UPS status (OL=on line, OB=on battery, CHRG=charging, DISCHRG=discharging, etc
                elseif ($result === "OB") $result = '3:On battery';
                elseif ($result === "CHRG") $result = '2:Charging';
                elseif ($result === "DISCHRG") $result = '4:Discharging';
                else $result = '0:unknown';           
            } 
            else $result = preg_replace('/[^0-9]/', '', $result);               // For everything else strip all non Alpha characters so we have only intergers left
                
            $memory[$snmp['Device']] = $result;                                 // Create a new array with parsed results as we can use this later for calculation

            if (isset($snmp['Type']))                                           // For all devices that report numeric and we want to see a text as well
            {
                switch ($snmp['Type'])         
                {
                    case 'Status':                                              // Simple Status Normal / Failed
                        if ($result == 1) $result = '1:Normal'; 
                        else $result = '2:Failed';
                        break;
                    case 'Diskstatus':                                              // Synology disk status Each meanings of status represented describe below.
                        if ($result == 1) $result = '1:Normal';                       // Normal(1): The hard disk functions normally. 
                        elseif ($result == 2) $result = '2:Initialized';              // Initialized(2): The hard disk has system partition but no data.
                        elseif ($result == 3) $result = '3:NotInitialized';           // NotInitialized(3):   The hard disk does not have system in system partition.
                        elseif ($result == 4) $result = '4:SystemPartitionFailed';    // SystemPartitionFailed(4): The system partitions on the hard disks are damaged. 
                        else $result = '4:Crashed';                                   // Crashed(5): The hard disk has damaged.
                        break;
                    case 'Raidstatus':                                              // Synology Raid status Each meanings of status represented describe below. 
                        if ($result == 1) $result = '1:Normal';                       // The raid functions normally. 1-4 is used for Alert Sensor in Domoticz
                        elseif ($result == 2) $result = '2:Repairing';
                        elseif ($result == 3) $result = '3:Migrating';
                        elseif ($result == 4) $result = '3:Expanding';
                        elseif ($result == 5) $result = '3:Deleting';
                        elseif ($result == 6) $result = '3:Creating';
                        elseif ($result == 7) $result = '3:RaidSyncing';
                        elseif ($result == 8) $result = '3:RaidParityChecking';
                        elseif ($result == 9) $result = '3:RaidAssembling';
                        elseif ($result == 10) $result = '3:Canceling';
                        elseif ($result == 11) $result = '4:Degrade';                 // Degrade happens when a tolerable failure of disk(s) occurs. 
                        else $result = '4:Crashed';                                   // Raid has crashed and just uses for read-only operation.  
                        break;
                    default:
                        $result = $result;
                }
            }                                                                          // All data is set, we can now determine how to update the devices in domoticz

            if (!isset($snmp['Type']))                                                  // No type is set like for temperature sensors
            {
                if (status($snmp['Device'])!=$result)                                   // Retrieve the status from domoticz an compare it with current value
                {
                    ud($snmp['Device'],0,$result);
                    lg('SNMP: '.$snmp['Device'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result);
                }
            }
            elseif ($snmp['Type']=='Diskstatus' || $snmp['Type']=='Raidstatus' || $snmp['Type']=='UPSstatus' || $snmp['Type']=='Status')   // Specific types need different type of domoticz sensor
            {
                $result = explode(":",$result);                                        // We parsed a string like 1:Normal, we need to separate the values
                if (status($snmp['Device'])!=$result[1])                               // Retrieve the status from domoticz an compare it with current value
                {
                    lg('SNMP: '.$snmp['Device'].' with '.$snmp['Type'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result[0].' '.$result[1]);
                    ud($snmp['Device'],$result[0],$result[1]);
                }
                //else lg('SNMP: Debug: '.$snmp['Device'].' with '.$snmp['Type'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result[0].' '.$result[1]);
            }
            //else lg('SNMP: Nothing do do here for: '.$snmp['Device'].' with '.$snmp['Type']);

            //else lg('SNMP: '.$snmp['Device'].' The status seems to be the same as value is: '.$result.' the retrieved value is: '.status($snmp['Device']));
        }   // End of For each loop

        // --------------------------------------------------------------------------------
        // CPU Usage: Some values require calculation, we can do it here with the new array.
        $cpuuse = ($memory['CpuUser'] + $memory['CpuSystem']);
        if (status('Synology CPU Usage')!=$cpuuse) 
        {
            ud('Synology CPU Usage',0,$cpuuse);
            lg('SNMP: CPU usage of domoticz is: '.status('Synology CPU Usage').' the calculated result is: '.$cpuuse);
        }

        // Memory Usage Some values require calculation, we can do it here with the new array.
        $MemFREE = ($memory['MemAvailable'] + $memory['MemShared'] + $memory['MemBuffer'] + $memory['MemCached']);
        $MemUsepercent = (($MemFREE * 100) / $memory['Memtotal']);
        $MemUsepercent = substr($MemUsepercent, 0, strpos($MemUsepercent, "."));            // Round the percentage
        if (status('Synology Memory Free')!=$MemUsepercent) 
        {
            ud('Synology Memory Free',0,$MemUsepercent);
            lg('SNMP: Synology Memory free of domoticz is: '.status('Synology Memory Free').' the calculated result is: '.$MemUsepercent);
        }
        
    }
    else lg('SNMP: Synology is: '.status('Synology').' unable to execute!');
}
Disk usage seems a problem with my 412+ pfff IOD not found things like that bleah.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Tuesday 22 May 2018 7:54

And you think I'm a programmer? :lol:
You did pretty good, got it working in just a few minutes.
Only thing I changed is:

Code: Select all

$MemUsepercent=round($MemUsepercent,0);

User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Tuesday 22 May 2018 10:49

Egregius wrote:
Tuesday 22 May 2018 7:54
And you think I'm a programmer? :lol:
You did pretty good, got it working in just a few minutes.
Only thing I changed is:

Code: Select all

$MemUsepercent=round($MemUsepercent,0);
Thanks for the compliment. :D . I learned this by looking at examples on your Github and using Google.
I am unfortunately still unable to extract the amount or percentage of free HD space within the Synology.

I noticed as well that some changes can be applied.
Example: If the device does NOT exist as a file, Pass2PHP is just smart enough to store the value in the cache ;-) (idx can'be retrieved) so maybe I don't need the additional $memory[] array for the calculation.

I'll update my code as well with your suggestion. Thanks
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Tuesday 22 May 2018 17:58


User avatar
sincze
Posts: 989
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Tuesday 22 May 2018 19:48

Egregius wrote:
Tuesday 22 May 2018 17:58
Did you see this pdf of Syno? https://global.download.synology.com/do ... _Guide.pdf
Yes I knew about the document, but I did not know how to use snmpwalk :D
That was untill now.

It seems I need :D

HDTOTAL 1.3.6.1.2.1.25.2.3.1.5.51
HDUSED 1.3.6.1.2.1.25.2.3.1.6.51
Synology Space.JPG
Synology Space.JPG (42.7 KiB) Viewed 1137 times
I also started working on my UNIFI AP-LR SNMP trap :D
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2739
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Wednesday 23 May 2018 8:56

You need to multiply that by hrStorageAllocationUnits 1.3.6.1.2.1.25.2.3.1.4.51 ;)

I'm also trying to get volume and network read/writes but can't find a good number.
Should be these:

Code: Select all

array('Device'=>"nas Vol Read bytes",'Address'=>	"1.3.6.1.4.1.6574.102.1.1.12.1"),
array('Device'=>"nas Vol Write bytes",'Address'=>	"1.3.6.1.4.1.6574.102.1.1.13.1"),
array('Device'=>"nas ETH Received bytes",'Address'=>	"1.3.6.1.2.1.2.2.1.10.7"),
array('Device'=>"nas ETH Sent bytes",'Address'=>	"1.3.6.1.2.1.2.2.1.16.7"),
But both give about the same value for read/write and the number is way to high.
Image

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests