Page 5 of 76

Re: RE: Re: LUA Pass2php

Posted: Thursday 08 December 2016 22:42
by sincze
Egregius wrote:Maybe a difference between php5 & php7. Don't know.
$r is spaces in name replaced by underscores ;)
Yes that $r triggered me as I did see that one working. Well thank you very much for helping this noob :)

PhP and Google were my friend in finding the solution.

Finally I can now start constructing those functions. Probably I will post again with different questions Image

Sent from my SM-G925F using Tapatalk

Re: LUA Pass2php

Posted: Thursday 08 December 2016 22:56
by Egregius
You're welcome ;)

You can find lots of examples on https://egregius.be/2016/lua-pass2php-v ... te-script/
Especially look at the way to handle the lastupdatetime $t if you plan to use that..'
And yes, Google knows a lot about PHP.

Re: LUA Pass2php

Posted: Saturday 10 December 2016 20:32
by sincze
So just added:

Code: Select all

 sudo apt-get install php5-memcached
To see where I can use this.
Studying your examples now.

First doorsensors are up and running.

'Zon' also very nice trick :D If solar panels are not producing any energy or very limited.. it must be a bit dark :D :D clever.
I store a lot of info in uservariables.

Currently looking at the possibilities of "if($s['slapen']=='Off'&&$s['hall']=='Off'&&(time<strtotime('8:00')||$s['zon']<150)) " for my functions.

Re: LUA Pass2php

Posted: Tuesday 13 December 2016 20:55
by sincze
Egregius wrote:No, ON FOR X doesn't work.

But that shouldn't be a problem ;)
My script is also executed every minute by updating a virtual sun sensor. At night I just update it every minute with a value of 0.
Well a lot of functions are working now :D and I'm now looking at your zon() function. It's a big one.
Just a question as that I might be looking in the wrong place.

How did you accomplish:
Egregius wrote: My script is also executed every minute by updating a virtual sun sensor. At night I just update it every minute with a value of 0.
I assume this is done by domoticz... in that way ensuring the lua and php are executed?
tnx.

I'm also looking into the Telegram notifications :D plenty of work still has to be done.

Do you know if disabling the converted LUA events within domoticz is sufficient to accomplish any improvements in speed?
Or should I delete the converted events from the domoticz database?

tnx again for your help.

Re: LUA Pass2php

Posted: Tuesday 13 December 2016 21:45
by Egregius
I have a cron job that runs every minute.
The script is at https://github.com/Egregius/PHP-Custom- ... re/cron.sh

During the day it grabs a value out of a csv file generated by sbfspot.
At night I just sent 0.
The script also monitores if the reply of domoticz is good and restarts domoticz after 3 failed attempts.

Telegram is a great way for notifications, pictures and even movies. I should post also a script to clean the history of that, I only keep 1 day of notifications to avoid to much cache data on mobile devices. Cleaning is only done when I'm at home to avoid missed notifications while travelling.

I think there isn't any difference between a disabled script or a removed script in terms of speed. At least I wouldn't think that, perhaps if you have hundreds or thousands of them, but not for couple of scripts.

Re: LUA Pass2php

Posted: Tuesday 13 December 2016 22:25
by sincze
@Egregius: Nice Github repo ;-) more inspiration for me.

I'm sure it describes the how to use php my camera for a snapshot.
Everything is currently working just fine in .sh for me, but I want to convert it to php and add the date to the image as well to just only send 1 image. :D

I was also reading:

https://gathering.tweakers.net/forum/li ... 1650110/68
and
viewtopic.php?t=11345

Describing similar code 8-) .

Re: LUA Pass2php

Posted: Tuesday 13 December 2016 22:32
by Egregius
Will post my python and php script I use in pikrellcam tomorrow.
In the php script I send a picture with some text overlays on it.
Depending on the 'sleep' switch it arrives with or without sound.
The python script is very specific for pikrellcam as it monitores a states file where motion intensity, speed, direction etc are recorded. It has a thing built in for a wait time before sending next snapshot etc.

Re: LUA Pass2php

Posted: Tuesday 13 December 2016 22:42
by sincze
tnx.
currently I am using this BASH solution. Maybe PHP will be faster. :D

Code: Select all

#!/bin/bash
# Variables to avoid having to set local system information in individual automation scripts

DomoticzIP="192.168.2.10"
DomoticzPort="8080"
TempFileDir="/var/tmp/"
TelegramUrl="https://api.telegram.org"
TelegramToken="bot12345678:YTYYERTYeyrtertERTERTertertERTRET"
TelegramChatId="1111111111"

###################################################################
IP1=192.168.2.1                        # IP Camera Voordeur 1
IP2=192.168.2.2                        # IP Camera Voordeur 2
###################################################################
SnapFile1=$TempFileDir"snapshot1.jpg"
SnapFile2=$TempFileDir"snapshot2.jpg"
###################################################################

MESSAGE="text=De Brievenbus is Zojuist geopend!"
curl --data chat_id=$TelegramChatId --data-urlencode "$MESSAGE" ''$TelegramUrl'/'$TelegramToken'/sendMessage'

if ping -c 1 $IP1 > /dev/null ; then # if IPCAM is online then:
    wget -O - "$DomoticzIP":"$DomoticzPort"/camsnapshot.jpg?idx=1 > $SnapFile1
    curl -s -X POST ''$TelegramUrl'/'$TelegramToken'/sendPhoto' -F chat_id=$TelegramChatId -F photo="@$SnapFile1"
else
   MESSAGE="text=Domoticz: IP-cam 1 niet beschikbaar."
   curl --data chat_id=$TelegramChatId --data-urlencode "$MESSAGE" ''$TelegramUrl'/'$TelegramToken'/sendMessage'
fi

if ping -c 1 $IP2 > /dev/null ; then # if IPCAM is online then:
    wget -O - "$DomoticzIP":"$DomoticzPort"/camsnapshot.jpg?idx=2 > $SnapFile2
   curl -s -X POST ''$TelegramUrl'/'$TelegramToken'/sendPhoto' -F chat_id=$TelegramChatId -F photo="@$SnapFile2"
else
   MESSAGE="text=Domoticz: IP-cam 2 niet beschikbaar."
   curl --data chat_id=$TelegramChatId --data-urlencode "$MESSAGE" ''$TelegramUrl'/'$TelegramToken'/sendMessage'
fi

Re: LUA Pass2php

Posted: Wednesday 14 December 2016 23:35
by G3rard
@Egregius,
I am testing the pass2php script combined with the the LUA time script. This way I want the PHP script to run every minute (and use it for ping checks and that kinda stuff.)

I have the following LUA code
script_time_pass2php.lua

Code: Select all

JSON=loadfile('/home/gerard/domoticz/scripts/lua/functions/JSON.lua')()
base64=loadfile('/home/gerard/domoticz/scripts/lua/functions/base64.lua')()
s=base64.encode(JSON:encode(otherdevices))
t=base64.encode(JSON:encode(otherdevices_lastupdate))
v=base64.encode(JSON:encode(otherdevices_svalues))
os.execute('curl -s --data "s='..s..'&t='..t..'&v='..v..'" http://127.0.0.1/fp/php/secure/pass2php_time.php &')
commandArray={}
return commandArray
And part of the pass2php_time.php code

Code: Select all

<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
date_default_timezone_set('Europe/Amsterdam');
define('time',$_SERVER['REQUEST_TIME']);
$s=json_decode(base64_decode($_REQUEST['s']),true);        //otherdevices
$t=json_decode(base64_decode($_REQUEST['t']),true);        //otherdevices_lastupdate
$v=json_decode(base64_decode($_REQUEST['v']),true);        //otherdevices_svalues

$test=Test_switch();
//START USERFUNCTIONS
function Test_switch(){
    global $s,$t,$v;
    lg('Status test: '.$s['Test switch']);
    lg('Last update test: '.$t['Test switch']);
    lg('svalues: '.$v['Sonos kantoor - playing']);
    if($s['Test switch']=="Off"){
        lg('Test switch is off');
    } else {
        lg('Test switch is on');
    }
}
But I see that the values are not always correctly loaded in the PHP code.
See below: The status of the switch in Domoticz is not shown at 23:23. After that I have switched it On. Then it's shown on 23:24, but then the svalue of another switch is not shown :?
When I switch of the Sonos 23:33 (so svalues is Uit), then everything is shown correctly. But when I switch on the Sonos again (23:34), the svalue is missing.

Code: Select all

2016-12-14 23:23:00.231 => Status test: 
2016-12-14 23:23:00.231 => Last update test: 2016-12-14 10:27:26
2016-12-14 23:23:00.231 => svalues: Fat Freddy's Drop, Russia
2016-12-14 23:23:00.232 => Test switch is on
---
2016-12-14 23:24:00.098 => Status test: On
2016-12-14 23:24:00.098 => Last update test: 2016-12-14 23:23:22
2016-12-14 23:24:00.099 => svalues: 
2016-12-14 23:24:00.099 => Test switch is on
---
2016-12-14 23:31:00.519 => Status test: On
2016-12-14 23:31:00.519 => Last update test: 2016-12-14 23:23:22
2016-12-14 23:31:00.519 => svalues: Uit
2016-12-14 23:31:00.520 => Test switch is on
----
2016-12-14 23:33:00.296 => Status test: On
2016-12-14 23:33:00.296 => Last update test: 2016-12-14 23:23:22
2016-12-14 23:33:00.297 => svalues: 
2016-12-14 23:33:00.297 => Test switch is on
I have disabled the class xsMemcached part in the PHP, but that doesn't make a difference.
Any idea's what could be causing this?

Re: LUA Pass2php

Posted: Thursday 15 December 2016 4:22
by Egregius
The xsMemcached is only called when you use cset/cget function to store/read something from memCached.
Is there a specific device that you need the svalue for? As I can do everything with the otherdevices table.
Maybe a easier way to run every minute is just use the usual pass2php device script and trigger a switch every minute by a cron shell script?
At least, that's how I do it. Then everything runs in one script. You don't need to maintain 2 scripts and you can even call one function from within another.
Also, maybe check the current states in Domoticz at that time? You can see them all in the event edit section.

Re: LUA Pass2php

Posted: Thursday 15 December 2016 21:34
by sincze
Mmm $s not displaying the actual data, so it seems.

For testing purposes I retrieve the status of a KODI player..

Code: Select all

        $status1=($s['Kodi Studeerkamer']);
        $status2=($s['Kodi Woonkamer']);
        $status3=($s['Kodi Keuken']);
        $status4=($s['Kodi Slaapkamer']);
 
After that I print the results using lg(); so the domoticz log says:

Code: Select all

2016-12-15 21:25:37.109 => Kodi Status Studeerkamer: Off
2016-12-15 21:25:37.112 => Kodi Status Woonkamer: Off
2016-12-15 21:25:37.115 => Kodi Status Keuken: On
2016-12-15 21:25:37.118 => Kodi Status Slaapkamer: Off
However Domoticz says:
Kodi.png
Kodi.png (29.58 KiB) Viewed 3119 times
What am I missing? I want to use this in the following way.

Code: Select all


if($s['Kodi Studeerkamer']!="Off")
{
 curl("http://$ip:$port/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Addons.ExecuteAddon%22,%22params%22:{%22addonid%22:%22script.securitycam%22},%22id%22:%221%22}}");    
} 
 
This will show a live feed of the IP camera on my Kodi machine :D
But yeah... first it should see the Kodi machine as on, otherwise the function does not work ofcourse :o

Re: LUA Pass2php

Posted: Thursday 15 December 2016 22:10
by Egregius
You have 2 devices with exactly the same name, only one value is stored in the array. Rename one to for ex StudeerKamer (capital K) and it'll be ok.

This is a cleaner url example for kodi:

Code: Select all

http://192.168.2.7:1597/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}

Re: LUA Pass2php

Posted: Friday 16 December 2016 0:46
by G3rard
Egregius wrote:The xsMemcached is only called when you use cset/cget function to store/read something from memCached.
Is there a specific device that you need the svalue for? As I can do everything with the otherdevices table.
Maybe a easier way to run every minute is just use the usual pass2php device script and trigger a switch every minute by a cron shell script?
At least, that's how I do it. Then everything runs in one script. You don't need to maintain 2 scripts and you can even call one function from within another.
Also, maybe check the current states in Domoticz at that time? You can see them all in the event edit section.
Just tested with the device script and had the same issue. Looked into it and probably had to do with a <br>character in the value of the Sonos text switch which shows the artist and song which is playing. Removed that and all seems to be working now.
Indeed otherdevices_svalues doesn't seem to be necessary. Thought I needed this for the thermostat, but the value is also in otherdevices. Probably svalues is only needed when updating via JSON.

Re: LUA Pass2php

Posted: Friday 16 December 2016 4:50
by Egregius
More than half of my 42 msec response time goes to the encoding in lua. The less you need to encode the faster it'll be ;)

I've already searched a lot to speed up the base64 and json encode, or leave it out, or... But no luck so far :?

Re: LUA Pass2php

Posted: Friday 16 December 2016 9:40
by sincze
Egregius wrote:You have 2 devices with exactly the same name, only one value is stored in the array. Rename one to for ex StudeerKamer (capital K) and it'll be ok.

This is a cleaner url example for kodi:

Code: Select all

http://192.168.2.7:1597/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}
This is a cleaner url example for kodi:

Code: Select all

http://192.168.2.7:1597/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}
[/quote]

Hahaha sorry.
That is exactly the same device (look at the time stamp below the name) :D
It was just to show the device is working in Domoticz :D (left device is on, right device is playing a video), however the php code says it is off. Clearly not 100% true :o

Code: Select all

http://192.168.2.7:1597/jsonrpc?request={"jsonrpc":"2.0","id":1,"method":"Player.Stop","params":{"playerid":1}}
[/quote]

Much cleaner indeed, now let's see if I can rewrite it to call my IPCAMERA plugin.

Re: LUA Pass2php

Posted: Friday 16 December 2016 10:29
by Egregius
Well, the $s array comes from Domoticz, I have no control over that.
But, if you have 2 devices with the same name it won't work.
This is what happens in that case:
$s['Kodi Studeerkamer']='Video';
$s['Kodi Studeerkamer']='On';
So only the last will be available.
If that's not the right value it's something in Domoticz. I'm sure if you print the status in lua you'll get exactly the same. On my system I've never seen that behavior, alltough there are topics about it on the forum here.

Re: LUA Pass2php

Posted: Friday 16 December 2016 11:41
by sincze
Egregius wrote:Well, the $s array comes from Domoticz, I have no control over that.
But, if you have 2 devices with the same name it won't work.
This is what happens in that case:
$s['Kodi Studeerkamer']='Video';
$s['Kodi Studeerkamer']='On';
So only the last will be available.
If that's not the right value it's something in Domoticz. I'm sure if you print the status in lua you'll get exactly the same. On my system I've never seen that behavior, alltough there are topics about it on the forum here.
Yes I believe it is not an issue with your script.

It is working flawless ;-) for the rest with the motion sensor and cron-time * memcached :D .
Really happy with it. Thank you for your help.

All doors, motion, and lights are now in the pass2php file.
Next stop will be adding some notifications via PUSHOVER and TELEGGRAM :D
Checking the device status to see if somebody is at home.
Dit not bother to convert my thermostat into it .. yest.. :D

Did still see some 'old idx array styles' in your 2.2 example code I believe. : :lol:

Re: LUA Pass2php

Posted: Friday 16 December 2016 12:24
by Egregius
Telegram should work with the provided function

Code: Select all

telegram($msg,$silent=true,$to='Guy'){...}

//usage:
telegram('message');//will arrive silently (default is silent=true)
telegram('message',false);//will use a sound

//in my case:
telegram('message',false,'Kirby');//Will make sound and will be sent to me and my girlfriend.
The function has a built in check if the message is accepted by Telegram and retries when it fails.
Telegram has a limit on sent messages per second/minute/... Not clearly defined on their site.
I noticed that sometimes messages didn't arrive so made the retry in it. Since never missed a message anymore.


'old idx array'? Don't know, could be. Script is altered almost daily to finetune and/or add options. As far as I know everything works, so if it's with old style I don't really care :P

Newest addition from yesterday: change the wake-up interval of my Danfoss thermostat valves. Like the valves in the bedrooms, they are set to 4° from 5 till 19h, why let them wake up every 5 minutes?
Now, during the day, their wake up interval is set to 1800. A half hour before the schedule starts their wake up interval is set to 600.
Then, when the difference between temperature and setpoint is less than 1° the wake up interval is set to 180.
So, they only wake up when needed and otherwise they preserve battery usage :)

Re: LUA Pass2php

Posted: Friday 16 December 2016 13:04
by sincze
Updated daily, sounds familiar.
That means for latest inspiration I can always check your floorplan website / github

Re: LUA Pass2php

Posted: Friday 16 December 2016 20:34
by sincze
This is what I retrieve from Domoticz during playback.

Code: Select all

{
   "ActTime" : 1481916666,
   "ServerTime" : "2016-12-16 20:31:06",
   "Sunrise" : "08:42",
   "Sunset" : "16:30",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "A great Movie to Watch (2012), 3%",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 23,
         "HardwareName" : "Kodi",
         "HardwareType" : "Kodi Media Server",
         "HardwareTypeVal" : 55,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "0000010",
         "Image" : "Light",
         "IsSubDevice" : false,
         "LastUpdate" : "2016-12-16 20:30:48",
         "Level" : 0,
         "LevelInt" : 0,
         "MaxDimLevel" : 15,
         "Name" : "Kodi Studeerkamer",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "Video",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "AC",
         "SwitchType" : "Media Player",
         "SwitchTypeVal" : 17,
         "Timers" : "false",
         "Type" : "Lighting 2",
         "TypeImg" : "Media",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "225"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
Retrieve via Domoticz during 'no active player'.

Code: Select all

{
   "ActTime" : 1481917173,
   "ServerTime" : "2016-12-16 20:39:33",
   "Sunrise" : "08:42",
   "Sunset" : "16:30",
   "result" : [
      {
         "AddjMulti" : 1.0,
         "AddjMulti2" : 1.0,
         "AddjValue" : 0.0,
         "AddjValue2" : 0.0,
         "BatteryLevel" : 255,
         "CustomImage" : 0,
         "Data" : "",
         "Description" : "",
         "Favorite" : 0,
         "HardwareID" : 23,
         "HardwareName" : "Kodi",
         "HardwareType" : "Kodi Media Server",
         "HardwareTypeVal" : 55,
         "HaveDimmer" : true,
         "HaveGroupCmd" : true,
         "HaveTimeout" : false,
         "ID" : "0000010",
         "Image" : "Light",
         "IsSubDevice" : false,
         "LastUpdate" : "2016-12-16 20:39:30",
         "Level" : 0,
         "LevelInt" : 0,
         "MaxDimLevel" : 15,
         "Name" : "Kodi Studeerkamer",
         "Notifications" : "false",
         "PlanID" : "0",
         "PlanIDs" : [ 0 ],
         "Protected" : false,
         "ShowNotifications" : true,
         "SignalLevel" : "-",
         "Status" : "On",
         "StrParam1" : "",
         "StrParam2" : "",
         "SubType" : "AC",
         "SwitchType" : "Media Player",
         "SwitchTypeVal" : 17,
         "Timers" : "false",
         "Type" : "Lighting 2",
         "TypeImg" : "Media",
         "Unit" : 1,
         "Used" : 1,
         "UsedByCamera" : false,
         "XOffset" : "0",
         "YOffset" : "0",
         "idx" : "225"
      }
   ],
   "status" : "OK",
   "title" : "Devices"
}
And this from $s

Code: Select all

2016-12-16 20:32:35.024 => Kodi Status Studeerkamer: Off
In the code from domoticz i see: "Status" : "On", or "Status" : "Video", that should be $s right??