Pass2PHP

Moderator: leecollings

Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote:I'm terrible in writing complete manuals.

$d is the array holding all the information of the system.
So $d['device']['s'] is the status.
t is the timestamp of update.
m is set with the storemode function.
storemode('device', 0); for example.
I use that for dimmers, rollers etc.
0 is mostly automatically, 1 manual. Whatever you want.
That fine, thanks a lot I understanding what you are writing.

I was looking at swithing the light on only when there is movement and when the amount of Lux is below 100. But that can be done with [‘s’]==<100 I guess


Sent from my iPhone using Tapatalk
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Correct.
Example PIR:

Code: Select all

if ($status=="On"&&$d['auto']['s']=='On') {
    if ($d['Weg']['s']==0&&$d['inkom']['s']=='Off'&&$d['zon']['s']<100) {
        sw('inkom', 'On');
    }
}
So, if pir is on and the virtual switch auto is on and we're home and the light of inkom is off and the sun power is less than 100W switch the light on.
A lot of my automation uses the virtual switch auto. That way it's easy to disable all automatic functions when needed, like when painting or cleaning etc.
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote: Thursday 09 May 2019 7:44 Correct.
Example PIR:

Code: Select all

if ($status=="On"&&$d['auto']['s']=='On') {
    if ($d['Weg']['s']==0&&$d['inkom']['s']=='Off'&&$d['zon']['s']<100) {
        sw('inkom', 'On');
    }
}
So, if pir is on and the virtual switch auto is on and we're home and the light of inkom is off and the sun power is less than 100W switch the light on.
A lot of my automation uses the virtual switch auto. That way it's easy to disable all automatic functions when needed, like when painting or cleaning etc.
thats's a smart idea but for the "Bijkeuken" I think this will be sufficient.

Code: Select all

<?php
if($status=='On'&&$d['PIR-Bijkeuken-Lux']['s']<100) {
sw('Bijkeuken','On');
}
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Always include the device that you want to switch, otherwise you'll send unneeded switch commands.

Code: Select all

<?php
if($status=='On'&&$d['Bijkeuken']['s']=='Off'&&$d['PIR-Bijkeuken-Lux']['s']<100) {
sw('Bijkeuken','On');
}
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote: Thursday 09 May 2019 13:27 Always include the device that you want to switch, otherwise you'll send unneeded switch commands.

Code: Select all

<?php
if($status=='On'&&$d['Bijkeuken']['s']=='Off'&&$d['PIR-Bijkeuken-Lux']['s']<100) {
sw('Bijkeuken','On');
}
Great tip! thnx now it goed on at exactly the right moment but is now not going out anymore :) so have to look into that :)
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Either your cron isn't running, or there's a fault in it.
Check the code and the logfile.
Split the if statement and ad lg lines to it.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

I've put the code that fetches the data into the $d array in a function.
This way you can update the $d array easily:

Code: Select all

$d=fetchdata();
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote: Friday 10 May 2019 7:43 I've put the code that fetches the data into the $d array in a function.
This way you can update the $d array easily:

Code: Select all

$d=fetchdata();
This is what i've got:
in folder/var/www/html/secure I put _cron60, cron.php, cron.sh
they look like this:
_cron60 for switching the lightBijkeuken and Barageportaal off

Code: Select all

<?php
if ($d['Bijkeuken']['s']=='On'&&$d['PIR-Bijkeuken']['s']=='Off'&&past('PIR-Bijkeuken')>60) {
	sw('Bijkeuken', 'Off');
}
if ($d['Garageportaal']['s']=='On'&&$d['PIR-Garageportaal']['s']=='Off'&&past('Garageportaal')>60) {
	sw('Garageportaal', 'Off');
}
?>
PIR-Bijkeuken.php

Code: Select all

<?php
if($status=='On'&&$d['PIR-Bijkeuken-Lux']['s']<60) {
sw('Bijkeuken','On');
}
I do still have trouble watching where the lg line are going?
for example: i've put in the _cron60:
lg('_cron60test1');
but never saw any output anywhere, therefore i took it out again :roll:
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Look at the lg function for the location of the logfile:

Code: Select all

function lg($msg)
{
    $fp=fopen('/var/log/domoticz.log', "a+");
    $time=microtime(true);
    $dFormat="Y-m-d H:i:s";
    $mSecs=$time-floor($time);
    $mSecs=substr(number_format($mSecs, 3), 1);
    fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp);
}
Make sure the www-data user has rights to write to the file.
Use tail -f to watch the logfile, these lines or NOT shown in the domoticz GUI logviewer.
Once you have the lg function working it's a lot easier to debug.
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote: Friday 10 May 2019 11:14 Look at the lg function for the location of the logfile:

Code: Select all

function lg($msg)
{
    $fp=fopen('/var/log/domoticz.log', "a+");
    $time=microtime(true);
    $dFormat="Y-m-d H:i:s";
    $mSecs=$time-floor($time);
    $mSecs=substr(number_format($mSecs, 3), 1);
    fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp);
}
Make sure the www-data user has rights to write to the file.
Use tail -f to watch the logfile, these lines or NOT shown in the domoticz GUI logviewer.
Once you have the lg function working it's a lot easier to debug.
I am using tail -f for monitoring the logfile with a grep on "Garageportaal" and another view for "Bijkeuken"
it might be possible the www-data user does nog have the correct permissions, but how to set these?

I am not sure what to do with the code you posted :oops:
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Well, you have to verify that /var/log/domoticz.log is the logfile used by domoticz, check /etc/init.d/domoticz.sh for that.
Don't use a grep yet, or are there +100 lines each minute?
easiest way to set permissions is

Code: Select all

chmod 777 /var/log/domoticz.log
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Pass2PHP

Post by sincze »

sudo chown www-data:www-data /var/log/domoticz.log

This command hand feeds apache/nginx the Logfile :)
chmod works 100 percent as well

I believe domoticz.sh is using /var/log/domoticz.txt by default and pass2php
/var/log/domoticz.log

Spot the difference.

Any way you made it a long way. Don't give up :) hang on!

At the moment still no access to keyboard. I'll add some proof of that below.Image

Sent from my ONEPLUS A6003 using Tapatalk

Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius »

Have a nice vacation!
We're of to The Ardennes ;)

Could be that .txt is the default. Altough that wouldn't be logical to use a .txt as logfile when .log is the default.
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Pass2PHP

Post by sincze »

Muchas Gracias @egregius.
Hope it is dry and a nice Temperature in the Ardennen. ;)
Lots of Belgian people here.
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Egregius wrote: Friday 10 May 2019 13:18 Well, you have to verify that /var/log/domoticz.log is the logfile used by domoticz, check /etc/init.d/domoticz.sh for that.
Don't use a grep yet, or are there +100 lines each minute?
easiest way to set permissions is

Code: Select all

chmod 777 /var/log/domoticz.log
I think I allready made the right config for that.
2019-05-10 15_56_35-log - Domoticz-NEW! - WinSCP.png
2019-05-10 15_56_35-log - Domoticz-NEW! - WinSCP.png (2.04 KiB) Viewed 1293 times
monitoring the domoticz.txt looks the same as the domoticz log whereas the domoticz.log is more clearer.

I use the grep to select see what the function does.
I do have a lot of log rules as I poll the amount of Lux ever several minutes.

how do I use the lg function in the _cron or device files to see if its working?
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

sincze wrote: Friday 10 May 2019 13:28
Wow Sincze that's looking good. a beer must tast extra well over there ;)
I also thought @Egregius could use one ;)
User avatar
sincze
Posts: 1300
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Pass2PHP

Post by sincze »

Domoticz log as defined in domoticz.sh should have far more entries (log lines) compared to the pass2php generated log file.

My Cron 60 starts with

lg('this is __cron60');

So it shows up in the log Everytime cron60 is called. :)

Evey minute that is :)

Sent from my ONEPLUS A6003 using Tapatalk

Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

sincze wrote: Friday 10 May 2019 16:38 Domoticz log as defined in domoticz.sh should have far more entries (log lines) compared to the pass2php generated log file.

My Cron 60 starts with

lg('this is __cron60');

So it shows up in the log Everytime cron60 is called. :)

Evey minute that is :)

Sent from my ONEPLUS A6003 using Tapatalk
ok, well this is what I put in the code.

Code: Select all

<?php
lg('this is __cron60'); 
if ($d['Bijkeuken']['s']=='On'&&$d['PIR-Bijkeuken']['s']=='Off'&&past('PIR-Bijkeuken')>60) {
	sw('Bijkeuken', 'Off');
}
if ($d['Garageportaal']['s']=='On'&&$d['PIR-Garageportaal']['s']=='Off'&&past('Garageportaal')>60) {
	sw('Garageportaal', 'Off');
}
?>
I am monitoring both domoticz.log as wel as domoticz.txt and the apache log :)
unfortunately no "this is _cron60". I must be doing something wrong
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Trigun wrote: Friday 10 May 2019 19:13
sincze wrote: Friday 10 May 2019 16:38 [/code]
I am monitoring both domoticz.log as wel as domoticz.txt and the apache log :)
unfortunately no "this is _cron60". I must be doing something wrong
ok, now I know for sure that my cron is not working
because when I manually start the cron.sh script I get the lg line in the domoticz.log
so, back to the cron :)
Trigun
Posts: 390
Joined: Wednesday 30 November 2016 11:58
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Contact:

Re: Pass2PHP

Post by Trigun »

Trigun wrote: Friday 10 May 2019 19:28
Trigun wrote: Friday 10 May 2019 19:13
sincze wrote: Friday 10 May 2019 16:38
ok, now I know for sure that my cron is not working
because when I manually start the cron.sh script I get the lg line in the domoticz.log
so, back to the cron :)
ok, I think I got it.
2 things - the cron.sh was not executable so I did a chmod 777 to cron.sh
and I found out that I used "sudo crontab -e" instead of crontab -e.
now I see the script is running perfectly every minute :D

just keeping you all informed as it might help others as wel.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest