New ways to script execution from signals

If you have tried to script tasks for TellStick and TellStick Duo you know it is very easy using tdtool. But tdtool is limited to only transmitting signals. What if you want to react on incoming signals from TellStick Duo?

If you want to script execution on incoming signals you either need to script in a third party application such as NexaHome or EventGhost or you need to write your own deamon and listen for callback. Both variants has it quirks. For example you need Windows for EventGhost.

What do you do if you have an headless Linux computer? Or you just don't want the complicated setup with callbacks?

From version 2.1.2 we have added the possibility to add a script to a predefined directory. Any executable script found in those folders will be executed and the parameters are sent as environmental variables. Let's first show an example. Create the file /usr/local/share/telldus/scripts/sensorevent/myscript.sh with the following content and make it executable.

#!/bin/bash
if [ "${DATATYPE}" -eq 1 ]; then
        notify-send --expire-time 3000 "Temperature ${VALUE}°C"
fi

When a sensor is detected the currently temperature will be shown as a popup in my KDE environment.

All callbacks from the C API are available.

/usr/local/share/telldus/scripts/deviceevent/
Environmental variables: DEVICEID, METHOD, METHODDATA
C API equivalent: TDDeviceEvent

/usr/local/share/telldus/scripts/devicechangeevent/
Environmental variables: DEVICEID, CHANGEEVENT, CHANGETYPE
C API equivalent: TDDeviceChangeEvent

/usr/local/share/telldus/scripts/rawdeviceevent/
Environmental variables: RAWDATA, CONTROLLERID
C API equivalent: TDRawDeviceEvent

/usr/local/share/telldus/scripts/sensorevent/
Environmental variables: PROTOCOL, MODEL, SENSORID, DATATYPE, VALUE, TIMESTAMP
C API equivalent: TDSensorEvent

/usr/local/share/telldus/scripts/controllerevent/
Environmental variables: CONTROLLERID, CHANGEEVENT, CHANGETYPE, VALUE
C API equivalent: TDControllerEvent

After adding or removing a script you need to restart telldusd.

For now, this is a Linux only feature. Patches are welcome for other platforms.

Comments

1. Pavlin Mitev -- 11 years ago

Hi, How can I download telldus-core v2.1.2 ?

2. Micke Prag -- 11 years ago

v2.1.2 is not yet released.

3. Mårten Bohlin -- 10 years ago

Has v2.1.2 still not been released?

4. Daniel Tamm -- 10 years ago

Great functionality. I tested the functionality in v2.1.2_rc1 but did have some problems. In some cases I get an error like

Could not execute /usr/local/share/telldus/scripts/sensorevent/temp.sh (14): EFAULT (bad address)

I figure it's because the env variable isn't properly set before calling EventUpdateManager::executeScript in EventUpdateManager.cpp.

It works on some devices but I think the problem occurs when signals arrive from devices that aren't registered or some other variables aren't as expected.

I think it would be helpful if the errormessage was improved by also printing out the content of the env array.

I would do that change myself if it wasn't for the fact that I haven't programmed c++ in about 15 years... I'd have to read up on the syntax.

void EventUpdateManager::executeScript(std::string script, const std::string &name, char env) { #ifdef _LINUX

pid_t pid = fork(); if (pid == -1) {

Log::error("Could not fork() to execute script %s", script.c_str()); return;

}

if (pid == 0) {

char *n = new char[name.length()+1]; snprintf(n, name.length()+1, "%s", name.c_str()); static char * argv[] = { n }; execve(script.c_str(), argv, env); delete[] n; Log::error("Could not execute %s (%i): %s", script.c_str(), errno, strerror(errno)); exit(1);

}

#endif _LINUX