Device blink

From Domoticz
Jump to navigation Jump to search

Purpose

This script will turn on a device with a virtual switch to let a certain device blink for several time.
It can be used for an alarm system for example, to let your lights flicker when motion is detected/doors are opened when there is nobody home.
This script is only about letting the lights blink, it is up to you how you use this.

Installing Python

The Python scripts needs 'python' to be installed. You can install this by running:

sudo apt-get install python

from the terminal on the Pi.
If it says that 'python' is already installed, you can continue further with this tutorial.

Domoticz Setup - creating virtual hardware dummy and virtual switches

This script needs a virtual switch for each device you want to poll.

Create virtual dummy hardware

  • To create a virtual switch, you first have to create a virtual hardware device.

Go to Setup > Hardware. Type in a name (I use 'DUMMY'), choose 'Dummy (Does nothing, use for virtual switches only)' from the drop-down. Leave 'Data timeout' disabled. Click on 'Add'

Create virtual switch(es)

To create a virtual switch, go to the 'Switches' tab. Click the 'Manual Light/Switch' button in the upper left corner. Choose the dummy hardware from the drop-down. Enter a name ('Blink Led'). Switch type 'On/off'. Type 'X10'. For 'House code' and 'Unit code', choose some random, doesn't really matter. As 'Main Device'.

Now you need to know the ID of the switch which you want to let blink. This can be found by going to 'Setup' --> 'Devices'. Find the switch you created in the list, and remember the IDX. Remember/write down this number, because you will need it later on

Create the script

Create a new file in the \home\pi\domoticz\scripts\ folder. Name the file 'Blink.py'. Copy the contents from the script on this page to the file, and save it.

Make script executable

In the terminal, go to cd /home/pi/domoticz/scripts
Execute the command chmod +x blink.py
I am not sure if executing this command is necessary, but it doesn't harm anything ;)

Personal changes

- Change "domoticzurl = "http://192.168.1.133:8080" in your own system

- Change "domoticzdeviceid_kaku = 87" change in the IDX which you want to blink

- Change total blinktime "stop = time.time()+300" where 300 is in seconds

- Change Off/on time "time.sleep(5) where 5 is in seconds

In the virtual switch set the on command to script:///home/pi/domoticz/scripts/blink.py


Python script

  #!/usr/bin/python
import sys
import json
import urllib2
import re
import time
import datetime
import httplib, urllib

def open_port():
    pass

def close_port():
    pass


class Domoticz():
    
    def __init__(self, url):
        
        self.baseurl = url
        
    def __execute__(self, url):

        req = urllib2.Request(url)
        return urllib2.urlopen(req, timeout=5)
       
    def set_device_on(self, xid):
        """
        Get the Domoticz device information.
        """
        url = "%s/json.htm?type=command&param=switchlight&idx=%s&switchcmd=On" % (self.baseurl, xid)
        data = json.load(self.__execute__(url))
        return data

    def set_device_off(self, xid):
        """
        Get the Domoticz device information.
        """
        url = "%s/json.htm?type=command&param=switchlight&idx=%s&switchcmd=Off" % (self.baseurl, xid)
        data = json.load(self.__execute__(url))
        return data

def flashing(url, device_id):

    	stop = time.time()+300
	while time.time() < stop: 
		on = Domoticz(url).set_device_on(device_id)
    		time.sleep(5)
    		off = Domoticz(url).set_device_off(device_id)
		time.sleep(5)

domoticzurl = "http://192.168.1.133:8080"
domoticzdeviceid_kaku = 87

flash = flashing(domoticzurl, domoticzdeviceid_kaku)

Source: https://github.com/scns/Domoticz-blinking-light

All credits goes to Schmm, Thanks