Noob tutorial for getting scripts to work on Synology/DSM

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
User avatar
Thijsvr
Posts: 21
Joined: Monday 04 January 2016 14:06
Target OS: NAS (Synology & others)
Domoticz version: 2.4075
Location: Amsterdam
Contact:

Noob tutorial for getting scripts to work on Synology/DSM

Post by Thijsvr »

Disclaimer: I’m not an expert in any way and I took a lot of time finding things out, much longer than it should’ve taken. But yeah I can only imagine I’m not the only one trying to make this script (and any script in general should work this way) working from a DSM / Synology system, so I figured I’d write a tutorial for others.

This tutorial is in addition to the very useful tutorial already in the Domoticz Wiki pages: https://www.domoticz.com/wiki/Presence_detection
Please read that first and do the steps until you reach the installation instructions for the script. From that point on I will explain how to get the script running on DSM/Synology systems.

My system: Domoticz (Jumbotroll package) on an HP Microserver Gen8 G1610T running DSM 5.1. I have the entire thing behind a VPN as an extra layer of security, but the steps should be the same for any Synology/DSM system.


1. Install Domoticz

You’ve probably already done this. I have the package installed by Jumbotroll (http://www.jadahl.com/). His work makes our lives very easy and he regularly updates his packages.

2. Install Python
The heroes of SynoCommunity have an easy to install Python package available. For instructions on how to install their packages go to: https://synocommunity.com/
Make sure to install Python, not Python3 or pyLoad. Just Python.


This is the easy part, the rest isn’t very hard, but it can easily become confusing if you’ve never done it before.

3. SSH
You will need to use SSH to get into the system files of your server/NAS. If you’re like me and you’ve never used SSH you’re in for some work. See, SSH is a command line type of system. So no easy clicking icons and stuff. It’s all keyboard when you’re using SSH. This is a handy site to keep open while doing this: http://sshcommands.co.uk/

I’m a Mac user so I’ve used Terminal to do everything, but PuTTY or WinSCP seem to be recommended on Windows. Linux users will probably know how to SSH, so no need to explain to them ;-)

You will be using the following commands:

cd - this will allow you to navigate folders
dir - this command will show you all the contents of a folder
mkdir - with this command you will make a folder in the folder you’re in at that point
vi - this command calls on the Vi editor. Lots of tutorials mention Nano or VIM, but Vi is already installed on your device and I have no idea how to install new programs through SSH. Doesn’t matter much though, the end result is the same.

4. So let’s turn on SSH on your Synology
Login to your NAS/server and open your control panel. Below you will see the option called ‘terminal’. Open it. There you can enable SSH. The standard port is 22. That’s fine, you can leave it at that. Then click save.
Make sure your firewalls allow you to access port 22.

5. SSH to your Synology
Open Terminal, PuTTY or WinSCP. You will see a black screen with the curser flashing. Type the following: ssh root@<ipadresstoyoursynology>

Press enter. You will now be asked to type your password. Note that while you enter it, the screen doesn’t show it. Just fill in your root password and press enter. Sometimes you will be promted with a message that this is the first time your IP is logging in. Just accept this.

Now you’ve SSH’d to your Synology and you can move around and do stuff. There’s a lot you’re now able to do (like screwing up your entire system). You can find this stuff out for yourself, but for now we’ll keep to making the presence check script work.

6. Create the location for the script
If you installed Domoticz through the package of Jumbotroll it’ll be place in /volume1/@appstore. You can check this by typing in the following: cd /volume1/@appstore

After the command press enter. The curser will jump to a new blank line and it’ll seem like nothing changed. On this line type the following: dir

When you press enter you’ll see a list of folders and files pop up. The command ‘dir’ shows you the contents of the folder you are currently in. You will see a folder called ‘domoticz’.

On Pi systems the scripts folder is located inside the domoticz folder. Often domoticz/scripts/python. However I’ve found that when you update the Domoticz folder it’ll delete the scripts folder. That means you would have to do this each time you update Domoticz. So I’ve decided not to install the scripts inside the Domoticz folder, but I’ve created a folder called ‘domoticzscripts’ in the @appstore folder.

You can do this by doing the following:
First make sure you are in the @appstore folder. You can do this with the following command: cd /volume1/@appstore
Give the ‘dir’ command to make sure you are inside the @appstore folder.

When you’re sure you’re in the right folder type the following: mkdir domoticzscripts and press enter. I’ve called my folder domoticzscripts, but you can name it anything you want.
Give the 'dir' command again to verify that your folder is now indeed there.

Go into this folder with the following command cd /volume1/@appstore/domoticzscripts and press enter.
You can give the dir command again to verify you are indeed in the right folder (the folder will be empty).

7. Creating the script

Now we'll be making the actual script. We'll be using VI. VI is an editor, but it works very differently than Word or Notepad for example. You have two modes. One is a command mode where you tell VI what you want it to do, and there is an editor mode. The commands you will be needing are as following:

ESC button - Enter command mode
I - Enter editor mode
x - remove character (backspace sort of)
set noautoindent - When you paste something in VI it'll automatically add a space to each line, this will cause problems with the script, so you want to turn it off
set autoindent - turn auto indent back on after you're done
:wq - Save and close file
:quit! - Close file without saving

Now go to Chopperrob's page and copy the script: https://www.chopperrob.nl/domoticz/5-re ... o-domoticz
Make sure to not copy the numbers. You only want the script.

When you've done this go back to Terminal/PuTTY/WinSCP and make sure you are in the domoticzscripts folder (cd /volume1/@appstore/domoticzscripts to make sure if you aren't). Then type: vi check_device_online.py
VI wil now open a new file called 'check_device_online.py'. You will see a blank page with some ~ on the left.

Now press the ESC button and type set noautoindent and press enter. It'll seem like nothing has changed, but you have now turned auto indent off. Now press the 'i' button on your keyboard. Again it'll look like nothing happened, but you are now in editor mode. Now right-click with your mouse on the top line and select paste. You will see that the script by Chopperrob is now pasted into VI. However it contains the wrong IP adress and possibly the wrong login details. We need to fix this. So use the arrow keys on your keyboard to move to the top of the window. There you will see on line 19, 20 en 21 that you need to fill in the details for your Domoticz server. If you're not sure what these are you can check them in your package center. Just click Domoticz and you can see the ip adress and port. To remove the old IP adress you can press the esc button to enter command mode and then press the x button to remove the details that are already present. To enter your own details again you'll have to press the letter 'i' on your keyboard allowing you to type.
If you have login details for Domoticz fill these in too. Otherwise just leave them blank, meaning there is nothing between the two quotation marks.

Once you've done that press the esc button and type :wq: and press enter.
You will be back in the domoticzscripts folder. If you put in the dir command you can see the folder contents and you will see your new script.

8. Testing the script
Now that you've done all this work you will want to make sure it works. You can do this by typing in python check_device_online.py <ipadress of device you want to check> <idx of your virtual switch> 10 30 and press enter. If everything works you will see a message that either your device is present or not. You can see the script work if you toggle the wifi on your device for example. If everything is working as it should you will also see the dummy switch in Domoticz switch between on and off. You will have to wait ten seconds for it to see it come back and 30 for it to go off again.

The hard work is done now and you can close Terminal/PuTTY/WinSCP.

9. Making sure the script keeps running and starts running automatically
In the original Wiki you have to set up a Cron job through SSH. Synology allows you to write Cron jobs through Task Scheduler. To do this login to your NAS and go to the control panel and click the task scheduler (it's under the Sytem tab). In this window click 'create' and select 'User-defined script'. This will open a window where you can do the following:

Task - This is the taskname, you can name this whatever.
User - This selects the user to run the script. Set this to 'root'
User-defined script - This contains everything you want Cron to start. You need to give in the full path to Python + the full path to your script + your arguments (ip adress, idx, intervals). If you have python installed on volume 1 and just the SynoPackage through package installer, your path to Python will likely be this '/usr/local/python/bin/python'. You separate the paths and arguments by spaces. So it should look like this:
/usr/local/python/bin/python /volume1/@appstore/<name_of_folder_you_made/check_device_online.py <ip_adress_of_device_you_want_to_check> <IDX> <Interval> <time-out>


This is what my task looks like:
/usr/local/python/bin/python /volume1/@appstore/domoticzscripts/check_device_online.py 192.168.1.1 6 10 120


Under schedule you can fill in how often you want the script to start. The original Wiki recommends to start it every ten minutes. Then just click save.

You can create the same task multiple times with different IP adresses if you want to check more than one device.


10. Wrapping up
After you're done I recommend restarting your NAS/server to see if everything starts running as it should when it comes back online. Just check it by turning off the wifi of the device you want to check, make sure your dummy switch is turned off in Domoticz also. Then restart the NAS/server and let it boot up along with Domoticz. Once everything is up and running turn on the wifi again on your device, wait a few seconds and see if the dummy switch turns on in Domoticz. If it does you're all set. If not, you've done something wrong. Just let me know and I'll see if I can help.

Good luck!
ThinkPad
Posts: 890
Joined: Tuesday 30 September 2014 8:49
Target OS: Linux
Domoticz version: beta
Location: The Netherlands
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DS

Post by ThinkPad »

As far as i know, i have never needed to install Python on the Synology. It was available already by default.

If you execute 'which python' on SSH, it will tell you the location.
I am not active on this forum anymore.
User avatar
Thijsvr
Posts: 21
Joined: Monday 04 January 2016 14:06
Target OS: NAS (Synology & others)
Domoticz version: 2.4075
Location: Amsterdam
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DS

Post by Thijsvr »

ThinkPad wrote:As far as i know, i have never needed to install Python on the Synology. It was available already by default.

If you execute 'which python' on SSH, it will tell you the location.

Thanks for your reply and thanks for the tip on how to find Python. I'm running DSM 5.1, the open source version of DSM. Maybe that doesn't come pre-installed with Python.
ViktorV
Posts: 3
Joined: Wednesday 14 September 2016 9:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by ViktorV »

Thanks for tutorial!
I tried to repeat your instruction, but 'VI' is too hardly for me ) , so i manually created file 'check_device_online.py'
And now i have error...

8. Testing the script
After "python /volume1/homes/admin/pi/domoticz/scripts/check_device_online.py 192.168.4.41 2 10 300" in PuTTy
i get:
Spoiler: show
pid file exist
Seems to be an old file, ignoring
script started
according to domoticz, 192.168.1.41 is offline
192.168.1.41 online, tell domoticz it's back
Is it correct reply?

9. Making sure the script keeps running and starts running automatically
i tried to create Task Scheduler:
"python /volume1/homes/admin/pi/domoticz/scripts/check_device_online.py 192.168.4.41 2 10 300"
But after start i get error on email:
sh: python /volume1/homes/admin/pi/domoticz/scripts/check_device_online.py 192.168.4.41 2 10 300: No such file or directory
User avatar
sincze
Posts: 1302
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by sincze »

Thanks for tutorial!
I had to figure it out tha hard way ;-)
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
ViktorV
Posts: 3
Joined: Wednesday 14 September 2016 9:04
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by ViktorV »

In my case, works followings:
I create file '/volume1/homes/admin/domoticz/scripts/PresenceDetection/check_device_online.py' in Windows and copy it to my Synology
I check its work from putty
9. Making sure the script keeps running and starts running automatically
Create file '/volume1/homes/admin/domoticz/scripts/PresenceDetection/Viktor_Phone.sh'

Code: Select all

#!/bin/sh
cd /volume1/homes/admin/domoticz/scripts/PresenceDetection
python check_device_online.py 192.168.1.45 5 20 60
Create shudele with '/volume1/homes/admin/domoticz/scripts/PresenceDetection/Viktor_Phone.sh'

Can i use one Shudele for many devices? I tried to use:

Code: Select all

#!/bin/sh
cd /volume1/homes/admin/domoticz/scripts/PresenceDetection
python check_device_online.py 192.168.1.45 5 20 60
python check_device_online.py 192.168.1.44 2 20 60
python check_device_online.py 192.168.1.42 3 20 60
But it works only for 192.168.1.45
User avatar
sincze
Posts: 1302
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Netherlands / Breda Area
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by sincze »

I've implemented this presence script on a Debian device without problems.
On my synology it just gives me devices 'Off'... however if i run the command manually.

Code: Select all

12:31:16- script started.
12:31:16- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.
12:31:16- according to domoticz, 192.168.1.47 is offline
12:31:16- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.
12:31:16- 192.168.1.47 online, tell domoticz it's back
So I switched the device off in Domoticz...

Code: Select all

12:46:40- 192.168.1.47 offline, waiting for it to come back
12:48:39- Error. Could not find switch idx in Domoticz response. Defaulting to switch off.
12:48:39- 192.168.1.47 offline, but domoticz already knew
If i Ping the device using a ping hardware device in Domoticz it will tell me the device is ON... so precense detecion not going so well.
The script has: check_for_instances = "pid"

What could go wrong??
I issued this command manually

Code: Select all

/volume1/homes/blabla/scripts/check_device_online.py 192.*.*.* 136 10 120
Pass2php
LAN: RFLink, P1, OTGW, MySensors
USB: RFXCom, ZWave, Sonoff 3
MQTT: ZIgbee2MQTT,
ZWAVE: Zwave-JS-UI
WIFI: Mi-light, Tasmota, Xiaomi Shelly
Solar: Omnik, PVOutput
Video: Kodi, Harmony HUB, Chromecast
Sensors: You name it I got 1.
MatthijsD
Posts: 45
Joined: Sunday 19 March 2017 11:48
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by MatthijsD »

Wow, 2016..
I hope there is still some support on this subject :)
Also, I noticed the script location website is down.. with the message "go away" :(

Anyway, I love the presence idea, so I followed the tutorial, but at the end, I get the following error:

Code: Select all

23:27:13- pid file exists
23:27:13- Seems to be an old file, ignoring.
23:27:13- script started.
Traceback (most recent call last):
  File "check_device_online.py", line 101, in <module>
    lastreported = domoticzstatus()
  File "check_device_online.py", line 79, in domoticzstatus
    json_object = json.loads(domoticzrequest(domoticzurl))
  File "check_device_online.py", line 96, in domoticzrequest
    response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 435, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 548, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 473, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 556, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
Anyone any idea whats up?
Or is there a new (and better?) way to the whole presence on Domoticz?
MatthijsD
Posts: 45
Joined: Sunday 19 March 2017 11:48
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Noob tutorial for getting scripts to work on Synology/DSM

Post by MatthijsD »

MatthijsD wrote:Wow, 2016..
I hope there is still some support on this subject :)
Also, I noticed the script location website is down.. with the message "go away" :(
.................
I remembered I have Android Automate installed and thought I could make a presence script on that; And yes, I could :)
I made my presence known through my Wi-Fi.

Connected? Yes? set my dummy switch On via JSON link. No? Set my dummy switch Off. Et voilà
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest