Issues with long PHP script

Moderator: leecollings

Post Reply
Darthkatzs
Posts: 2
Joined: Wednesday 01 November 2017 10:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Issues with long PHP script

Post by Darthkatzs »

I have a PHP script that beeps my alarm every second when i open my front door and the alarm is armes. It is set to read the JSON to see if the alarm is still armed.
It works perfectly when I manually run it to test.
But when I set it up to run trough a LUA script or when the door sensor is triggered directly, then it screws up my whole system. It does not turn off when disarming and the alarm ends up going off too even when I disarm.
Could it be that the script not immediately sending a response causes this?
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Issues with long PHP script

Post by Egregius »

You should post your code...
And fork the script in the background so it doesn't block the Domoticz event system.
aalwash
Posts: 6
Joined: Saturday 06 January 2018 11:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9168
Contact:

Re: Issues with long PHP script

Post by aalwash »

Not sure if you're in the position to use MQTT?
Here is a (extra) idea.

Next to Egregius advice to run it in background, you can tell Domoticz to publish all changes to MQTT.
Then you can make a simple PHP script (that run "endlessly") and listens to the topic "domoticz/out"

So when the door sensor is triggered, you immediatly get a message from Domoticz and you can do your checks and beep the alarm if needed.
Advantage: No JSON calls are required (every X minute), since Domoticz will tell you when this happens. And your PHP script runs separately from Domoticz
RPi3 , Xiaomi Door Window sensors, Xiaomi Temp+Humidity sensors, Zigbee2MQTT (Zigbee CC2531), Hacked Toon
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Issues with long PHP script

Post by Egregius »

Can you post some example code for 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: Issues with long PHP script

Post by Egregius »

Nevermind the mqtt code. Had it operational for a small time but instantly noticed that a switch command thru mqtt takes 10 times longer than thru json.
aalwash
Posts: 6
Joined: Saturday 06 January 2018 11:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9168
Contact:

Re: Issues with long PHP script

Post by aalwash »

Not sure what you tested exactly? MQTT is fast and the publishes are instant.
Remember If you change something (turn switch on/off) and you don't see that the interface, doesn't mean the publish is slow. The interface refreshes every X seconds.

Below a simple test, to show you how fast it is.

Code: Select all

<?php

$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->subscribe('#', 1);

$client->loopForever();
$client->unsubscribe('#');

function connect($r, $message) {
        echo "I got code {$r} and message {$message}\n";
}

function message($message) {
        printf("%s %s -- message: %s" . PHP_EOL, date('Y-m-d H:i:s'), $message->topic, $message->payload);
}

function disconnect() {
        echo "Disconnected cleanly\n";
}

So now I start 2 terminals, one to run the test.php and one to publish.
Terminal #1

Code: Select all

$ php test.php
I got code 0 and message Connection Accepted.


Terminal #2

Code: Select all

mosquitto_pub -h localhost -m '{ "idx" : 6, "nvalue" : 1, "svalue" : "25.0" }' -t 'domoticz/in'


Back to terminal #1 (pay attention to the timestamps)

Code: Select all

2018-02-02 10:58:17 domoticz/in -- message: { "idx" : 6, "nvalue" : 1, "svalue" : "25.0" }
2018-02-02 10:58:17 domoticz/out -- message: {
   "Battery" : 255,
   "RSSI" : 12,
   "description" : "",
   "dtype" : "Light/Switch",
   "id" : "00014056",
   "idx" : 6,
   "name" : "iPhone Ali",
   "nvalue" : 1,
   "stype" : "Switch",
   "svalue1" : "25.0",
   "switchType" : "X10 Siren",
   "unit" : 1
}

As you can see, I'm changing the nvalue to 1 (and svalue 25, but you can ignore that for now).
The domoticz/out confirms that Domoticz did receive the message and is republishing it instantly.
When I go back to my interface and I refresh my 'Switch' page, I see that my changes are done correctly.


So what's the slow part exactly?

Same applies, if I click on a switch in the Domoticz interface to turn it on/off, I see a instant domoticz/out message
RPi3 , Xiaomi Door Window sensors, Xiaomi Temp+Humidity sensors, Zigbee2MQTT (Zigbee CC2531), Hacked Toon
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Issues with long PHP script

Post by Egregius »

I installed Mosquitto and added it as hardware.
I checked the functionality with a command like:

Code: Select all

mosquitto_pub -h localhost -m '{"command": "switchlight", "idx": 11, "switchcmd": "On" } -t 'domoticz/in'
Then I changed my pass2php switch command so it would publish that command instead of calling the JSON.

I only look at the timestamps in the logfile to determine response times.

This is with MQTT 100 msec:

Code: Select all

2018-02-02 06:32:28.856  (ZWAVE) Light/Switch (pirkeuken)
2018-02-02 06:32:28.956  MQTT: Topic: domoticz/in, Message: {"command": "switchlight", "idx": 11, "switchcmd": "On" }
2018-02-02 06:32:28.956  OpenZWave: Domoticz has send a Switch command! NodeID: 4 (0x04)
This is with JSON 8 msec:

Code: Select all

2018-02-02 06:39:43.479  (ZWAVE) Light/Switch (pirkeuken)
2018-02-02 06:39:43.487  User: Admin initiated a switch command (11/keuken/On)
2018-02-02 06:39:43.487  OpenZWave: Domoticz has send a Switch command! NodeID: 4 (0x04)
So in that test MQTT is (more than) 10 times slower.
I guess it all depends on how domoticz loops thru the eventsystem. I already suggested that it should be possible to choose which event system is active instead of going thru all of them everytime.
Now, as soon as you enable the eventsystem the database is checked for scripts, the filesystem is checked for lua, python etc scripts. Depending on the order of that loop one responds faster than the other.
I would really appreciate in the settings multiple checkboxes for each possible event system to be active or not. If I don't use database scripts it's no use to first query the database to see if a script exists. Same goes for python and lua.
Maybe, if I disable the whole eventsystem and only use the MQTT it would be faster, should test that...
aalwash
Posts: 6
Joined: Saturday 06 January 2018 11:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9168
Contact:

Re: Issues with long PHP script

Post by aalwash »

Oke, now I see what you mean.
Didn't check it on Domoticz logs level how much difference there is between JSON and MQTT.

In that case, JSON is idd faster.

Like you already mentioned, maybe it's the order process of Domoticz that is causing the big delay
RPi3 , Xiaomi Door Window sensors, Xiaomi Temp+Humidity sensors, Zigbee2MQTT (Zigbee CC2531), Hacked Toon
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest