Page 1 of 1

Enable/disable timer from events / LUA script

Posted: Thursday 31 October 2013 13:31
by rdebruijn
Option to enable / disable a (set of) timer(s) for a switch/device from within events (blockly) or via a LUA script would be very welcome.

Screenshot to illustrate:

Image

Re: Enable/disable timer from events / LUA script

Posted: Thursday 31 October 2013 14:19
by Paulyoh
+1

Im also looking for this. :)
Se this post for info: http://www.domoticz.com/forum/viewtopic ... 4323#p4287

Re: Enable/disable timer from events / LUA script

Posted: Saturday 10 October 2015 17:56
by asdfgh1234
I have an Arch Linux

It works with this workaround :-)

ENABLE

in LUA script (script_device_xxx.lua):

Code: Select all

os.execute ("/opt/domoticz/scripts/sql.sh 24 1")
24 is idx
1 is Activate

then in SH script (sql.sh):

Code: Select all

#!/bin/bash

sqlite3 -header /opt/domoticz/domoticz.db "UPDATE SetPointTimers SET Active = $2 WHERE DeviceRowID = $1 AND Active <> $2;"
DISABLE
in LUA script:

Code: Select all

os.execute ("/opt/domoticz/scripts/sql.sh 24 0")

Re: Enable/disable timer from events / LUA script

Posted: Saturday 10 October 2015 19:51
by Eduard
Also possible on Domoticz with JSON: (since august 27 :D )

Enable timer:

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=enabletimer&idx=1
Disable timer

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=disabletimer&idx=1
Enable scene-timer

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=enablescenetimer&idx=1
Disable scene-timer

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=disablescenetimer&idx=1
Show all timers:

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=gettimerlist
Show all scene-timers:

Code: Select all

http://<ip>:<port>/json.htm?type=command&param=getscenetimerlist
Have fun!

PS: Not advisable to edit the DataBase at runtime in my opinion, that why i wrote these extra JSON-calls ;)

Re: Enable/disable timer from events / LUA script

Posted: Sunday 17 January 2016 20:02
by clkoolen
Hi Eduard,

Could you elaborate on if and how to make these json URL's work with the blockly? I tried the setup in the image below:
Event with json.PNG
Event with json.PNG (26.58 KiB) Viewed 7924 times
It changes the state of the control variable. In the log I can see it tried to fetch the URL:
Log.PNG
Log.PNG (9.5 KiB) Viewed 7924 times


but there is no change of state in the timer list. If I try the same URL from my browser it works fine.
Entry Timerlist.PNG
Entry Timerlist.PNG (5.05 KiB) Viewed 7924 times
Help would be very much appreciated.

Casper

Re: Enable/disable timer from events / LUA script

Posted: Sunday 17 January 2016 22:39
by Eduard
Sorry, i can't tell you why the blockly is not working because i don't use them. I only use (bash/python) scripts to fire the json calls.


Verzonden vanaf mijn iPad met Tapatalk

Re: Enable/disable timer from events / LUA script

Posted: Monday 18 January 2016 16:52
by clkoolen
Hi Eduard,

As a complete NOOB to Domoticz (started two weeks ago) and pyton scripts could you elaborate in how to process a json call into a script?

My main intention in enabling and disabling timers is that I would like to create a "holidays" button that will activate and deactivate a set of timers that will turn lamps on and off in the house. Next to that I would also like to check the sunrise and sunset times to decide if the outdoor lamps should be turned on or not. Blockly appeared to be ideal for this, but unfortunately I cannot get it to work for me. Next to that the timer functions that can be associated to the buttons are very easy to use and versatile enough except for the problems described above.
Any help on calling the json commands in a python script is very welcome. After that I think I will be able to get it to work for me no problem.

Thanks for the help.

Re: Enable/disable timer from events / LUA script

Posted: Monday 18 January 2016 18:35
by Eduard
Here is a simple python example

Code: Select all

import urllib2

domoticzserver   = "127.0.0.1:8080"
domoticzusername = ""
domoticzpassword = ""

base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')

def DomoticzRequest (url):
  request = urllib2.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  response = urllib2.urlopen(request)
  return response.read()

url = "http://" + domoticzserver + "/json.htm?type=command&param=enabletimer&idx=1"

DomoticzRequest(url)
Hope it get's you started!

Re: Enable/disable timer from events / LUA script

Posted: Sunday 24 January 2016 17:45
by G3rard
clkoolen wrote:Hi Eduard,

As a complete NOOB to Domoticz (started two weeks ago) and pyton scripts could you elaborate in how to process a json call into a script?

My main intention in enabling and disabling timers is that I would like to create a "holidays" button that will activate and deactivate a set of timers that will turn lamps on and off in the house. Next to that I would also like to check the sunrise and sunset times to decide if the outdoor lamps should be turned on or not. Blockly appeared to be ideal for this, but unfortunately I cannot get it to work for me. Next to that the timer functions that can be associated to the buttons are very easy to use and versatile enough except for the problems described above.
Any help on calling the json commands in a python script is very welcome. After that I think I will be able to get it to work for me no problem.

Thanks for the help.
Other possibilty is to activate the Holiday timer. This option can be found at Setup-Settings-Other-Timer Plan. When Holiday timer is actived, you can set other timers then in the default plan.

Re: Enable/disable timer from events / LUA script

Posted: Sunday 31 January 2016 15:26
by clkoolen
Hi Eduard,

Thanks for the script example. I got it to work with the addition of import base64.
Now I will have to make it do my desired changes.

Thanks again.

Re: Enable/disable timer from events / LUA script

Posted: Saturday 06 February 2016 20:24
by clkoolen
Ended up creating a single script that would check a specific group is on or off.
Then depending on the state it enables or disables a set of timers by the id's.

Code: Select all

#!/usr/bin/python

import urllib2
import base64
import json

"""
General scrypt settings.
Set the appropriate username and password.
Enter the id of the group that will turn on and off the timers
Enter the ids of the timers that will be turned on and off
"""

domoticzserver   = "127.0.0.1:8080"
domoticzusername = "username"
domoticzpassword = "password"
group_id = "1"
timer_ids = [1, 2]

"""
Build the encoded password string
"""

base64string = base64.encodestring('%s:%s' % (domoticzusername, domoticzpassword)).replace('\n', '')

"""
Process the url request
"""

def DomoticzRequest (url):
  request = urllib2.Request(url)
  request.add_header("Authorization", "Basic %s" % base64string)
  response = urllib2.urlopen(request)
  return response.read()

"""
Request the group and scene list.
Filter out the status of the desired group id
set the json command based on the status
"""
url = "http://" + domoticzserver + "/json.htm?type=scenes"
data = json.loads(DomoticzRequest(url))
group_info = [obj for obj in data["result"] if(obj["idx"]== group_id)]
group_status = group_info[0]["Status"]
if group_status == "Off":
  json_command = "/json.htm?type=command&param=disabletimer&idx="
else:
  json_command = "/json.htm?type=command&param=enabletimer&idx="

"""
Send the json command for all desired timer ids
"""

for id in timer_ids:
  url = "http://" + domoticzserver + json_command + "%i" %id
  DomoticzRequest(url)