LUA script needed for checking 2 switches with time in between

Moderator: leecollings

Post Reply
NoClaim
Posts: 10
Joined: Sunday 17 May 2015 19:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2284
Location: the Netherlands
Contact:

LUA script needed for checking 2 switches with time in between

Post by NoClaim »

I need help as I can't script this with blocky and I have no LUA knowledge.

What do I want to achieve:
I have a KAKU doorcontact magnet with 2 variables: open/close (or on/off).
Each time when it is dark in house (no lights are switch on (anymore)) I want to switch a light on when we come home (based on the doorcontact).
This is simple to do with blocky.... so this is not the problem

But..... I also want this door switch to work as an alarm. But not everytime the door is opened a trigger must be set...

I have a dummy switch 'someone at home'. This switch is turned on when the smartphone of my wife or mine phone is pinged.
When both phones are not pinged the switch is set to 'off'.

So what would i like to achieve:
When the door is opened and within 1 (or 5) minute(s) the 'someone at home' switch is turn on there is nothing wrong.
But..... when the door is opened and within 5 minutes the swith 'someone at home' is not switched on some action needs to be taken (send an email, notification and also the doorbell needs to ring a few times and lights need to start going on or off....

How can I script something like this....?? (is it possible?

Sorry for the complex explanation......I didn't know an easier way :shock:
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Egregius »

Does the 'alarm' also needs to be enabled when the door is back closed within the 5 min period? Or only is the door is open longer than x minutes?
For the first option you'll need to loop thru the history of the switch, the second is easier.
NoClaim
Posts: 10
Joined: Sunday 17 May 2015 19:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2284
Location: the Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by NoClaim »

Door open is the first trigger.... when within 5 minutes the second trigger is not activatie (someone ate home - switch) then some kind of alarm needs to be set in motion.

But..... your point is a good point..... if the door is closed the motion must continue.... (so it must ignore 'door closed')
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Egregius »

Don't know how easy this would be in LUA.
In PHP I would do it like this:

Code: Select all

<?php
$ctx = stream_context_create(array('http'=>array('timeout' => 3,)));
$lastopen = 0;
$datas = json_decode(file_get_contents('http://127.0.0.1:8084/json.htm?type=lightlog&idx=252',true,$ctx),true);
$datas = array_reverse($datas['result']);
foreach($datas as $data) {
	if($data['Status']==='On') {
		$timestamp = strtotime($data['Date']);
		if($timestamp>$lastopen) $lastopen = $timestamp;
	}
}
if($lastopen > (time()-300)) {
	//Do action
}
That in combination with the state of another switch will get you going.

Edit: just thinking, I guess scripts like these must by scheduled. I mean: you can trigger a script when the door opens, but not that easy 5 minutes later. I run a big PHP script every minute, will implement above in mine for when my son get's home :)
Thanks for the idea!
Slinkos
Posts: 81
Joined: Thursday 10 December 2015 0:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Slinkos »

You can do this with a blocky. Make an extra dummy switch that you call "alarm" for example and then you do something like:

If someoneathome=off AND door=on
Set alarm=on AFTER 5 minutes
Elseif alarm=on AND someoneathome=off
Do Notification/doorbell/lights/whatever
Slinkos
Posts: 81
Joined: Thursday 10 December 2015 0:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Slinkos »

And also don't forget to add this:

Elseif someonehome=on
Do Set alarm=off

To reset is back to normal again. :)
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Egregius »

Isn't the 'Set alarm=on AFTER 5 minutes' gonna execute anyway?
Slinkos
Posts: 81
Joined: Thursday 10 December 2015 0:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Slinkos »

Yes it is, But the alarm switch itself doesn't do anything. But only if the alarm switch is on (after 5 minutes), and the someonehome switch is still off, then the stuff will happen. Maybe alarm isn't the best word for it. Just call it "delay" or something, i don't know :)

But you might also want to add a 5 minutes delay into setting "alarm" (or "delay" or whatever) to "Off" again so that putting it On and Off won't cross eachother, if that's what you meant.
User avatar
Egregius
Posts: 2589
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Egregius »

Maybe, but that's one off the problems with lua and blockly: you almost always need extra switches to handle stuff. In PHP you don't need that.
mrf68

Re: LUA script needed for checking 2 switches with time in between

Post by mrf68 »

I would create something like this:

1:
If door (= physical contact) is open
Then set delay (= virtual switch) is on

2:
If someone home (= ping result) is on AND delay is on
Then set delay is off

3:
If someone home is off AND delay is on AND status lastupdate delay > 5 minutes
Then set alarm (= the action that needs to take place for you to notice that something is wrong) is on

1 can be a blockly. Check 2 and 3 need to run every minute. I would create a script_time_something.lua for both.
NoClaim
Posts: 10
Joined: Sunday 17 May 2015 19:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2284
Location: the Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by NoClaim »

I will try the blocky advise.... will put the feedback here on the outcome.

I thought scripting was the only way to check a switch with a delay option. Luckily there are more ways to come to Rome :-)

... I also like the last provided option.... combination of blocky and lua scripts.....but need the script for it (I don't have any scripting experience.... :( )
NoClaim
Posts: 10
Joined: Sunday 17 May 2015 19:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2284
Location: the Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by NoClaim »

I have created the blocky event and played with it and it seems to work.
Only thing I think of now..... when my wife forgets her phone at home.... the event is useless....
But during holidays.... this is a great and simple solution....

I did think of something else.... don't have the solution for it...(only in case I do it manually through the mobile app)...
When a neighbour is check the house or feeding the cat.... the event will send me a note, but the switch will not turn off automatically.... but at least I have the exact time of entering and leaving the house :-)
mrf68

Re: LUA script needed for checking 2 switches with time in between

Post by mrf68 »

NoClaim wrote:... I also like the last provided option.... combination of blocky and lua scripts.....but need the script for it (I don't have any scripting experience.... :( )
There are a few examples in the wiki. You can start reading them and try to find out what is being done. That's how I do it. But it takes time and effort to understand it, but it feels great when you make yourself a working script. Let me give you some info. Remember, these are my interpretations of things and maybe I make some mistakes or things can be done easier, but then again: if it works, I'm where I wanna be. ;)

First a script:

Code: Select all

s = otherdevices_lastupdate['PushOnButton']
t = os.time()

commandArray = {}
print (s)
print (t)
return commandArray
Change the PushOnButton with the name of one of your own switches. Save this script as script_time_timetest.lua and put in in the LUA dir of Domoticz. Then take a look at the logging. In my case I see this:

2016-03-17 22:51:00.302 LUA: 2016-03-09 15:43:19
2016-03-17 22:51:00.302 LUA: 1458251460

Now I can see when my PushOnButton is last updated. But when I look at the OS time I see something very different. The system can't compare those two values or do maths with it. So I need to reconstruct the lastupdate.
In the script, between s and t, I put the following:

Code: Select all

year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)

ts = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
What does it do? I tell the system to get the characters 1 through 4 of the string 's' (that is the output of the lastupdate request) to be the value of 'year', characters 6 and 7 to be the value of 'month', etc. Then I rebuild this (in a way the system could use it to compare or do math with the os time) as the value of 'ts', to tell it that 'ts' is an os.time constructed with the values year, month, etc.

In the script, between print(s) and print (t), put the following line in between:

Code: Select all

print (ts)
Then again, take a look at the log. In my case it shows this:

2016-03-17 23:03:00.051 LUA: 2016-03-09 15:43:19
2016-03-17 23:03:00.051 LUA: 1457534599
2016-03-17 23:03:00.051 LUA: 1458252180

I see the lastupdate in a way I can understand, the next line shows me after rebuilding it and the last line is the actual time. With those last two line, you can do maths with it. In the script, under thel line t = os.time(), put the following:

Code: Select all

diff = (os.difftime (t, ts))
.. and below the line print(t), put this:

Code: Select all

print ('Difference in seconds is:')
print (diff)
Again, I take a look at the log:

2016-03-17 23:08:00.254 LUA: 2016-03-09 15:43:19
2016-03-17 23:08:00.254 LUA: 1457534599
2016-03-17 23:08:00.254 LUA: 1458252480
2016-03-17 23:08:00.254 LUA: Difference in seconds is:
2016-03-17 23:08:00.254 LUA: 717881

Oh my god, it's a lot of info. I like to try these things out and put it here for you to examine it. Maybe it gets you interested in trying to learn LUA. Try to understand what is happening and how it could be of any use to you. ;)
Slinkos
Posts: 81
Joined: Thursday 10 December 2015 0:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: LUA script needed for checking 2 switches with time in between

Post by Slinkos »

NoClaim wrote:I have created the blocky event and played with it and it seems to work.
Only thing I think of now..... when my wife forgets her phone at home.... the event is useless....
But during holidays.... this is a great and simple solution....

I did think of something else.... don't have the solution for it...(only in case I do it manually through the mobile app)...
When a neighbour is check the house or feeding the cat.... the event will send me a note, but the switch will not turn off automatically.... but at least I have the exact time of entering and leaving the house :-)
I solved that by adding a RFID reader in my Domoticz :) Just give your neighbour a RFID key. Other option is that you tell your neighbour to turn on a specific light in your home or something, and make that a part of your blocky. That the alarm only will go off if that light is off.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest