Pass2PHP

Moderator: leecollings

Post Reply
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Pass2PHP

Post by Egregius »

I published some code on http://egregius.be/2016/lua-pass2php/
A new way to handle events in PHP with all data that's available in LUA.
The greatest advantage is the reaction time. Where that was between 500 and 1200 msecs before, it's now steady at 80msec.
My system reacts lots faster now and I preserve all the freedom of programming in PHP, so me very happy :D

Edit: added a separate Github repository at https://github.com/Egregius/LUA-Pass2PHP-for-Domoticz
Last edited by Egregius on Tuesday 31 January 2017 6:56, edited 2 times in total.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

Published v2.0 on http://egregius.be/2016/lua-pass2php-v2-0/
Biggest change is a cleaner code, so easier to implement and understand :)
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: LUA Pass2php

Post by ben53252642 »

What a brilliant idea! I'll definitely use this on my system, I use quite a few php scripts already.

Thank you!
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
User avatar
gizmocuz
Posts: 2350
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: LUA Pass2php

Post by gizmocuz »

Did you know that you can use PHP now directly in domoticz ?
For example you could create a test.php file in the www folder
if you install php-cgi, and specify the location of this file at the domoticz startup parameters

php_cgi_path (for example /usr/bin/php-cgi)

then it should work...

(just some extra php info)
Quality outlives Quantity!
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

No, didn't know that yet. Great for other users, or RPi users.
I have PHP installed in Apache on Synology wich is also needed to run other web apps.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

ben53252642 wrote:I use quite a few php scripts already.
With this you can script everything in one script. No need to repeat bits of code in every script and stuff like that.
bxlouis
Posts: 45
Joined: Wednesday 16 December 2015 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Belgium
Contact:

Re: LUA Pass2php

Post by bxlouis »

gizmocuz wrote:Did you know that you can use PHP now directly in domoticz ?
For example you could create a test.php file in the www folder
if you install php-cgi, and specify the location of this file at the domoticz startup parameters

php_cgi_path (for example /usr/bin/php-cgi)

then it should work...

(just some extra php info)
Sorry if it sounds retarded but... how can I modify the domoticz startup parameters?

Then, how can I run pass2php using this method?

Thanks!
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

You need to install php5 and php5-cli
For pass2php you don't need a php enabled webserver so Domoticz doesn't need to be altered.
Then, create the lua and php file and you're good to go.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

bxlouis wrote:Hi Egregius,

Indeed, I understand PHP better, I see what it does.
I own a RPi, how can I install Pass2PHP v2.0 using gizmocuz method?

And how should I setup the pass2php.php file? I see that a lot of functions are present but they do not fit my home. Should I just put the NestAwayMode function there?

Thanks.
You only need the PHP code from http://egregius.be/2016/lua-pass2php-v2 ... planation/ and replace the 2 pir functions.
In the $events array you define what IDX triggers what function.
So, if your NestAwayMode has idx 23 you should add 23=>'NestAwayMode' and then create the NestAwayMode function with the code it has to handle.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

renerene wrote: Questions regarding http://egregius.be/2016/lua-pass2php-v2-0/:
1) Which PHP package do I need to install? Just "sudo apt-get install php5-cli"?
2) is this correct result of an extra added debug line?
print ('/home/pi/domoticz/scripts/php/test.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
result: /home/pi/domoticz/scripts/php/test.php "bnVsbA==" "eyIyMjB2IHR1aW5odWlzIjoiT2ZmIiwiQWxhcm0gTGV2ZWwgT04vT0ZGIjoiT2ZmIiwiQ ... [8 lines of symbols]
3) Do i need to perform 'chmod +x' on the test.php file? Other files too?
1) I would run "sudo apt-get install php5 php5-cli"
2) That will print the base64 encoded string, totally unreadable.
3) Normally not, I only use test.php to... test :lol:
renerene
Posts: 316
Joined: Wednesday 03 August 2016 11:36
Target OS: -
Domoticz version:
Contact:

Re: LUA Pass2php

Post by renerene »

It doesn't work. LUA gives no error in domoticz log file. I assume the PHP is executed. Cannot find any log file on the PHP actions, so I do not know if the PHP is executed or where the problem is.

Where are the log files?
Do I need to change the third line? --> define('api',"http://127.0.0.1:8084/");
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

Correct, lines 3-5 contain user settings.
api = url where domoticz is available, in this format: http://ip:port/
So it's very possible you'll have to change at least the port number.

On my syno, without doing anything special, all output generated by the php script comes in to default domoticz logfile.
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: LUA Pass2php

Post by deennoo »

Hi there, as promise i contact you here, my goal is to speed up domoticz event to improve WAF who don't want to wait for 4 second that a light bright !

Working a brand new VM with debian 8.5 + rflink + domoticz compiled from last source (3.5721), no tweak about sleep_second etc etc

Adding JSON.lua = ok
Adding ee5_base64.lua = ok
Adding script_device_pass2php with directory update = ok
"sudo apt-get install php5 php5-cli" = ok done

Now working on pass2php.php

I saw that you put file you use, ok cool, is this possible to get one without your device but wih comment, this way i can try to make mine (i'm a real newbie on PHP) ?

Edit : ok now working on your pir exemple.
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

On https://egregius.be/2016/lua-pass2php-v2-0/ there's a big example with all my stuff, that's quit hard to understand if you didn't created yourself.

On https://egregius.be/2016/lua-pass2php-v ... planation/ there's a small example with some inline comments.

The parts that needs adjusting are:

Code: Select all

$events=array(123=>'pirgarage',234=>'pirhall');
Fill the array with idx on wich you want to do something. Above 2 idx's of 2 pir devices, but can be anything you want. The function after => is called whenever that idx gets an update in domoticz.

Then create the functions with the same name as in the array:
Always start the function with global $a, $s, $i, $t to have all the arrays available inside the function.
$a is the status of the changed device
$s array of all device statusses. Ex: $s[‘pirhall’]
$i array of all device idxs. Ex: $i[‘hall’]
$t array of all last update times off devices. Ex: strtotime($t[‘hall’]) gives the unix timestamp.

Code: Select all

function pirgarage(){
  //This function is called when idx 123 is changed.
    global $a,$s,$i,$t;
    //if the new state of the device is "On"...
    if($a=="On"){
      //sw is the function name for a switch. $i is the array containing all idx's. So you could also use sw(210,'On') if you prefer using idx's instead of names
        sw($i['garage'],'On');
    }
}
function pirhall(){
    global $a,$s,$i,$t;
    if($a=="On"){
        sw($i['hall'],'On');
    }
}
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: LUA Pass2php

Post by deennoo »

Ok Done

I have to use sw(idx,'On'), name doesn't work then sw(idx,'') for toggle

Code: Select all

2016-10-07 00:30:59.518 (rflink) Light/Switch (InterLoisir)
2016-10-07 00:30:59.569 User: Admin initiated a switch command (9/PlafonnierLoisir/Toggle)
2016-10-07 00:30:59.569 RFLink Sending: 10;TriState;08000a;10;OFF
2016-10-07 00:31:00.103 (rflink) Light/Switch (PlafonnierLoisir)
What a change ! 1/2 sec for this case who was mini 4 secondes before (lol now problem is that RFlink + aurel mid 5 rtx are to slow to switch from recieve to send...)

thanks for your script, and advice ! it looks like what i have seen before that using HTML API, Domoticz is so faster, I have done some event using my Voice reconition system using html api, and irexec (IR Reciever who run HTML curl)

Something a littel more tricky ?

My lua script is :

Code: Select all

commandArray = {}
 if (devicechanged['Inter loisir'])  then
     commandArray['Plafonnier Loisir'] = 'Toggle'
 end
 return commandArray
As you can see i don't care about which state is the switch "interloisir", i want that each time his status change light toggle o worry about On or Off

Toggeling light is ok i get it, will search for a way to just chech if switch state change


Thanx for your help
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

Doing a toggle whenever the function is called isn't difficult, just remove the if($a=... statement:

Code: Select all

function InterLoisir(){
    global $i;
    sw($i['Plafonnier Loisir']); //OR sw(123); if name doesn't work. Should check that the space in it is the issue. I never use spaces in my devicenames
} 
Then you can also just use 'global $i' to just have the array of idx's. Why load the other arrays if you don't need them, right? Maybe another msec faster :lol:

1/2 Second is indeed lots faster dan 4 seconds but still to slow, it should be less than 100msec to have a great WAF :roll:
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: LUA Pass2php

Post by deennoo »

Thx,

About name, on my testing machine i didn't put space on name, i was thinking about issue to.

Using idx is ok for me, don't know how Domoticz search on is own database order, following IDX or ability to store by name.

Faster and faster is great but now i'm hardware dependant...will check with a second rflink base on separate rx a ne tx rf module if it can works faster as domoticz says.

Second element will be ability to drive scene and groupe : got some dimable bulb, and various "ambiance", playing with them using rf switch or remote or pir.

Remember this script :

commandArray = {}
 if (devicechanged['Inter plaf salon tv'] and otherdevices['Plafonnier Salon'] == 'Off')  then
     commandArray['Group:Plafonnier salon']='On'
 elseif (devicechanged['Inter plaf salon tv'] and otherdevices['Plafonnier Salon'] ~= 'Off')  then
     commandArray['Plafonnier Salon']='Off'
 end
 return commandArray
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

There shouldn't be much difference in using idx or name. The names and idx's are stored in a array anyway.
I do find it strange that a rflink can't sent directly, I don't see that behavior with zwave.
My rfxcom is only used for Somfy and some simple remotes.

I also don't use groups or scenes in Domoticz as I create them directly in my script, gives more flexibility on how to handle the group in different cases.

Code: Select all

function Interplafsalontv(){
  global $a,$s,$i;
  if($s['PlafonnierSalon']=='Off'){
     if($s['firstlightofgroup']=='Off')sw($i['firstlightofgroup'],'On');
     if($s['fsecondlightofgroup']=='Off')sw($i['fsecondlightofgroup'],'On');
     if($s['fsecondlightofgroup']=='Off')sw($i['fsecondlightofgroup'],'On');
  }else{
     if($s['firstlightofgroup']!='Off')sw($i['firstlightofgroup'],'Off');
     if($s['fsecondlightofgroup']!='Off')sw($i['fsecondlightofgroup'],'Off');
     if($s['fsecondlightofgroup']!='Off')sw($i['fsecondlightofgroup'],'Off');
  }
}
Another advantage of this is that only the commands that are nescesary are sent. When you switch a group/scene in domoticz all commands are sent.
With this, if for some reason one of the devices is already on the command won't be sent again wich saves again some time.
With this you can also create more intellegent scenes:

Code: Select all

function Interplafsalontv(){
  global $a,$s,$i;
  if($s['PlafonnierSalon']=='Off'){
     if($s['firstlightofgroup']=='Off')sw($i['firstlightofgroup'],'On');
     if($s['fsecondlightofgroup']=='Off'&&time>strtotime('18:00')sw($i['fsecondlightofgroup'],'On');
     if($s['fsecondlightofgroup']=='Off'&&$s['lightmeter']<200)sw($i['fsecondlightofgroup'],'On');
  }else{
     if($s['firstlightofgroup']!='Off')sw($i['firstlightofgroup'],'Off');
     if($s['fsecondlightofgroup']!='Off')sw($i['fsecondlightofgroup'],'Off');
     if($s['fsecondlightofgroup']!='Off')sw($i['fsecondlightofgroup'],'Off');
  }
}
  
Second light will only go on if hour of day is later than 18:00, thirth light only if a luxmeter is lower than 200.
Let your imagination go ;)
deennoo
Posts: 784
Joined: Wednesday 10 December 2014 13:06
Target OS: Linux
Domoticz version: beta
Location: Bordeaux France
Contact:

Re: LUA Pass2php

Post by deennoo »

Egregius wrote:Doing a toggle whenever the function is called isn't difficult, just remove the if($a=... statement:

Code: Select all

function InterLoisir(){
    global $i;
    sw($i['Plafonnier Loisir']); //OR sw(123); if name doesn't work. Should check that the space in it is the issue. I never use spaces in my devicenames
}
Then you can also just use 'global $i' to just have the array of idx's. Why load the other arrays if you don't need them, right? Maybe another msec faster :lol:

1/2 Second is indeed lots faster dan 4 seconds but still to slow, it should be less than 100msec to have a great WAF :roll:
Made a test With a 2 modules RFLink (1 rx and 1 tx) and a EMW100 remote + your toggle way :

Got this result :

Code: Select all

2016-10-07 11:25:01.931 (rflink) Light/Switch (test)
2016-10-07 11:25:01.958 User: Admin initiated a switch command (9/PlafonnierLoisir/Toggle)
2016-10-07 11:25:01.958 RFLink Sending: 10;TriState;08000a;10;OFF
27ms between reception and action, that fast enought !
Domoticz stable 3.5877 for real & Domoticz beta for test
Rfxtrxe / RFLink / Milight / Yeelight / Tasmota / MQTT / BLE / Zigate
http://domo-attitude.fr
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Pass2php

Post by Egregius »

Wow, that's nice!
That's faster than a blink of your eyes, so fast enough :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest