TellStick + Python

Moderator: Telldus

Post Reply
lew_for
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

TellStick + Python

Post by lew_for »

Hello,

I have done some research on using Python with the TellStick, and have managed to get it working, with the tellcore-py wrapper. Although I don't entirely understand how to use it.

The code I have written is very bad, but it does the job for now. Could someone please help me optimise it. I can't find out how to use an existing device either, so my .conf file adds a new entry *every* time I run the script...

Here is the code:

from tellcore.telldus import TelldusCore

core = TelldusCore()
Switch = core.add_device("Switch", "arctech", "selflearning-switch", house=9, unit=1)
Switch.turn_on()

print "ON"

I have it on a webserver too, and it's connected to an 'On' button on the index page. Is there a way I can make the .py page close it's self after running the script? How do I call the device named 'Switch' without it creating a new device every time?

Do I even need the wrapper? All I want to be able to do, is visit a webpage and have it run a script in the background to turn on/off the power.
erijo
Posts: 22
Joined: Fri Mar 17, 2023 9:45 am

Re: TellStick + Python

Post by erijo »

I'm reposting the answer I sent to you in via mail, in case anyone else wonders:

To get all existing devices, you call core.devices(). This returns a list that you can iterate over to find the device you are interested in. E.g:

Code: Select all

devices = core.devices()
for device in devices:
    if device.name == "Switch":
        device.turn_on()
Or if you want all devices called Switch:

Code: Select all

switches = [d for d in core.devices() if d.name == "Switch"]
for switch in switches:
    switch.turn_on()
The tellcore_tool (installed if you install with pip, otherwise in the bin directory: https://github.com/erijo/tellcore-py/bl ... lcore_tool) contains a function named find_device that can locate a device based on name or id.

(Hint: To remove a device, call device.remove())

With that said, if you want a simple web interface you could try tellprox which uses tellcore-py internally. Or you can try another project of mine: tellive-py. With that you can connect your Tellstick to Telldus Live and control all devices via the web interface or a smart phone.
Python wrapper for Telldus Core: tellcore-py
Python wrapper for Telldus Live: tellive-py
lew_for
Posts: 3
Joined: Fri Mar 17, 2023 9:45 am

Re: TellStick + Python

Post by lew_for »

I'll reply here too then.

So if my device is called 'Switch' in the .conf file, the code you provided:

Code: Select all

devices = core.devices()
for device in devices:
    if device.name == "Switch":
        device.turn_on()
That will turn on Switch? So if I save that to on.py, that will find and turn on my switch? Thank you!

I didn't use pip, I cloned the GitHub repo, is there any benefit of using that? I bought the cheaper TellStick, so I'm not sure I have access to TellLive.

Lastly, after having my .conf filled with unnecessary devices, I deleted it and thought that the script would recreate it, but it didn't so I reinstalled it but broke the installation somehow, so I can only control it via tdtool and not in a Python script. I think I'm going to just reinstall the OS etc. but if I don't, is there an easy fix? I couldn't work out how to remove telldus-core completely.
erijo
Posts: 22
Joined: Fri Mar 17, 2023 9:45 am

Re: TellStick + Python

Post by erijo »

Sorry for not responding earlier.

You should be able to connect your Tellstick to Telldus Live as well. Either with TelldusCenter or my own software tellive_core_connector (see tellive-py link below).

Pip is probably eaiser, but with git you are sure to get the latest version.
Python wrapper for Telldus Core: tellcore-py
Python wrapper for Telldus Live: tellive-py
Post Reply