trying to make my first lua script

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

trying to make my first lua script

Post 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
 
User avatar
krazny
Posts: 27
Joined: Saturday 01 November 2014 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

Re: trying to make my first lua script

Post 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.
Last edited by krazny on Tuesday 15 March 2016 22:29, edited 1 time in total.
Raspberry Pi, Aeon Z-Stick, Fibaro Motion Sensor, 4x Fibaro Wall Plug, 2x Fibaro Dimmer, Netatmo Weatherstation
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to make my first lua script

Post by ms2076 »

Thanks! i will try asap.
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to make my first lua script

Post 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'
User avatar
krazny
Posts: 27
Joined: Saturday 01 November 2014 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Netherlands
Contact:

Re: trying to make my first lua script

Post 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'
  
Raspberry Pi, Aeon Z-Stick, Fibaro Motion Sensor, 4x Fibaro Wall Plug, 2x Fibaro Dimmer, Netatmo Weatherstation
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to make my first lua script

Post by ms2076 »

Thanks it looks like thats not working, but i will search. thanks for pointing me into the right direction.
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to make my first lua script

Post 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'
ms2076
Posts: 9
Joined: Tuesday 15 March 2016 20:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: trying to make my first lua script

Post 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.
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: trying to make my first lua script

Post 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
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest