Page 1 of 1
trying to make my first lua script
Posted: Tuesday 15 March 2016 21:45
by ms2076
Hi,
i am trying to make a script that should monitor the powerconsumption in domoticz (on raspberry). I know I cannot make this with the block events (well, I cannot find how to manage this), but also the lua script isnt working as expected (i know that i am doing something wrong).
what i want to establish is the following:
when powerconsuming is more then 2000watt for X minutes, i want a prowl message
when power after an hour is still >2000 watt, i again want a message that consumption is still more then 2000watt.
Also a message when power is < 2000 watt is what i want to have
I created a simple test script to start and test the (if more then 2000watt for 1 minute script) but it isnt working.
The Power2000 is a dummy switch, which in want to turn on when power is >2000 watt (thats working with blocks). For now i am turning it on manually.
i am really a new user, can someone help pointing me into the right direction?
Code: Select all
commandArray = {}
if (devicechanged['Power2000'] == 'On FOR 1' then
commandArray['SendNotification']='subject#body#extraData#0#bike'
end
return commandArray
Re: trying to make my first lua script
Posted: Tuesday 15 March 2016 22:20
by krazny
I had something like this to check if my (old) TV was on;
Code: Select all
print('<b style="color:ORANGE">Check TV Status</b>')
commandArray = {}
if otherdevices_svalues['PLUG-NAME'] >= '25' and otherdevices['TV_LivingRoom'] == 'Off' then
print('<b style="color:ORANGE">TV status = ON</b>')
commandArray['TV_LivingRoom']='On'
elseif otherdevices_svalues['PLUG-NAME'] < '25' and otherdevices['TV_LivingRoom'] == 'On' then
print('<b style="color:ORANGE">TV status = OFF</b>')
commandArray['TV_LivingRoom']='Off'
end
return commandArray
*Maybe not the best code out there, but it worked
If you want an event after xx seconds u can use this;
Code: Select all
commandArray={}
function timedifference (s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
if
otherdevices['TV_LivingRoom'] == 'On' then
print('<b style="color:Blue">TV_LivingRoom is ON.</b>')
commandArray['Lamp_Bank']='On'
elseif
otherdevices['Lamp_Bank'] == 'On' and
otherdevices['TV_LivingRoom'] == 'Off' and
timedifference(otherdevices_lastupdate['NAME-PLUG']) >= 120 then
print('<b style="color:Blue">TV_LivingRoom is Off. Switching Lamp_Bank Off</b>')
commandArray['Lamp_Bank']='Off'
end
return commandArray
Both examples should give you want you need.
Just copy/paste the right codes.
And to be sure; You DO need a device which measures your power consumption. Which one are u using? I'm using the Fibaro Wallplugs.
Contact me if you need help.
Re: trying to make my first lua script
Posted: Tuesday 15 March 2016 22:27
by ms2076
Thanks! i will try asap.
Re: trying to make my first lua script
Posted: Tuesday 15 March 2016 22:59
by ms2076
allright this is working,I am getting the message by prowl, but it gives me messages constantly every 2 seconds, i just want 1 message. Any idea? then i can continue building this script:
Code: Select all
commandArray={}
function timedifference (s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
if
otherdevices['Power2000'] == 'On' then
commandArray['SendNotification']='Huidig verbruik is meer dan 2000 watt'
Re: trying to make my first lua script
Posted: Wednesday 16 March 2016 9:30
by krazny
ms2076 wrote:allright this is working,I am getting the message by prowl, but it gives me messages constantly every 2 seconds, i just want 1 message. Any idea? then i can continue building this script:
Code: Select all
commandArray={}
function timedifference (s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
if
otherdevices['Power2000'] == 'On' then
commandArray['SendNotification']='Huidig verbruik is meer dan 2000 watt'
Think of it this way;
Example;
IF Lamp == ON ---> Send Message.
So Domiticz checks if the lamp is on; YES! So sends the message.
2 secs later domoticz again checks if the lamp is on; YES! So sends the message..
etc. ....etc
You need an extra check to prevent the message from being send every 2 secs.
I do know there are more people in the forum who've made some nice scripts for this. Maybe a search will help you further?
Or you can try something like this;
Code: Select all
commandArray={}
function timedifference (s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
if
otherdevices_svalues['Power2000'] > 2000 and
timedifference(otherdevices_lastupdate['Power2000']) >= 60 then
commandArray['SendNotification']='Huidig verbruik is meer dan 2000 watt'
Re: trying to make my first lua script
Posted: Wednesday 16 March 2016 19:16
by ms2076
Thanks it looks like thats not working, but i will search. thanks for pointing me into the right direction.
Re: trying to make my first lua script
Posted: Wednesday 16 March 2016 19:42
by ms2076
the search is not really helping me and i cannot find a good explained solution for my case (about notification interval with lua).
Also i think its weird that with its not implemented by default to make this with blockly (if switch - on for 20 seconds then... or whatsoever)
Anyway, i tried the following, but its not working. the smartmeter is called Power.
Code: Select all
commandArray={}
function timedifference (s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
if
otherdevices['Power2000'] == 'On' then
commandArray['SendNotification']='Huidig verbruik is meer dan 2000 watt'
otherdevices_svalues['Power'] > 100 and
timedifference(otherdevices_lastupdate['Power2000']) >= 10 then
commandArray['SendNotification']='Huidig verbruik is meer dan 100 watt voor 10 seconden'
Re: trying to make my first lua script
Posted: Thursday 17 March 2016 19:23
by ms2076
does someone know how to manage the time scripting? so that i only get one message in 10 minutes (of whatever) instead of every 2 seconds? after trial and error with krany his script, its not working. Also i cannot find the right solution in this forums.
Re: trying to make my first lua script
Posted: Thursday 17 March 2016 19:50
by Westcott
Hi Ms2076,
'Time scripts have to be named in a special way (script_time_yourname.lua ), or set as 'Time' in the script editor.
To make your code run every 10 minutes, do this -
time = os.date("*t")
if ((time.min % 10)==0) then
Put your code here
end