Windows powershell (CLI) and Tellstick Net

Moderator: Telldus

Post Reply
c8h10n4o2
Posts: 86
Joined: Thu Jan 18, 2018 11:08 am
Contact:

Windows powershell (CLI) and Tellstick Net

Post by c8h10n4o2 » Thu Jan 18, 2018 11:08 am

Just thought I could add a few examples on how to use the powershell cmdlets I wrote to controll Tellstick Net since there seems to be quite hard to do this from windows cli currently.

First example shows some simple temperature logging:

Code: Select all

Import-Module C:\Scripts\Telldus\Module\TelldusModule.psm1

$Username="my_email@telldus.com"
$Password="MySecretPassword"
$TempLogFile="C:\TelldusLogs\Temperature.csv"
$SensorData=Get-TDSensor -Username $Username -Password $Password | Get-TDSensorData -Username $Username -Password $Password

$SensorData | Export-Csv $TempLogFile -Append -Encoding utf8
This will logg temperature and humidity (if available) on all sensors associated with your account. Just schedule to execute with "powershell.exe" and use the "-file" argument to specify the script file, the results will be in the csv-file in the format:
"DeviceID","Name","LocationName","Temperature","Humidity","LastUpdate"
"123456","Balcony","Home","26.1","40","2013-06-07 14:40:19"
"234567","Home","Home","22.0",,"2013-06-07 14:38:32"

If you wan't to do something based on sensor data (for example, I want my plants to get some extra water when it's hot or dry on my balcony), but only if the sensor data is current, here is another example: (my pump starts if it get's switched off and then back on)

Code: Select all

Import-Module C:\Scripts\Telldus\Module\TelldusModule.psm1

$Username="my_email@telldus.com"
$Password="MySecretPassword"
# At what temperature (and above) should I start the pump?
$WhenToWaterTemp=25

# If the humidity get's below this value, I will also start the pump
$WhenToWaterHumidity=40

# How old could the sensor data be before I'll consider it invalid? (in hours)
$DataAgeLimitInHours=2

# Time zone difference compared to telldus servers.
$TimeZoneDifference=2

$BalconySensor=Get-TDSensor -Username $Username -Password $Password | Where-Object { $_.Name -eq "Balcony" }

$BalconySensorData=Get-TDSensorData -Username $Username -Password $Password -DeviceID $BalconySensor.DeviceID

# Alternative if you would rather like a oneliner to get the sensor data, and use Powershell v3 where-clause
# $BalconySensorData=Get-TDSensor -Username $Username -Password $Password | where Name -eq "Balcony" | Get-TDSensorData -Username $Username -Password $Password

$CurrentAgeLimit=(Get-Date).AddHours(-($TimeZoneDifference+$DataAgeLimitInHours))

if ($BalconySensorData.LastUpdate -gt $CurrentAgeLimit) {
    if ($BalconySensorData.Temperature -ge $WhenToWaterTemp -or $BalconySensorData.Humidity -le $WhenToWaterHumidity) {
        Write-Output "The plants are sweating (dry and/or hot)! `nI'm going to give them some water..."
        Get-TDDevice -Username $Username -Password $Password | Where-Object { $_.Name -like "Balcony Watering System*" } | Set-TDDevice -Username $Username -Password $Password -Action turnOff
        Get-TDDevice -Username $Username -Password $Password | Where-Object { $_.Name -like "Balcony Watering System*" } | Set-TDDevice -Username $Username -Password $Password -Action turnOn
    }
}
else {
    Write-Output "Sensordata is too old!"
}
I'm guessing (I know...) there is room for a lot of improvement on the cmdlets, hopefully these examples might make it easier to come up with them! (if anybody actually want's to use powershell to control their Tellstick) :)

You could skip the "Get-TDDevice" cmdlet and just hardcode the DeviceID on the "Set-TDDevice" cmdlet if you want, but doing it this way feels a bit more dynamic. (If I would add another pump called "Balcony Watering System 2", that pump would also get restarted without any changes to script).

The good thing with powershell is that a lot of things are already in place to connect it to other systems like sending e-mail, connecting to a db server, checking the weather online throught web requests etc. etc., so you can basically trigger your devices based on anything.

I'm for example waking up when the traffic is getting lighter by using online traffic information services :)
Another example is asking Siri (I know this is useless, just did it for fun...) to ask "a contact in my phonebook" to "water my plants", powershell reads the e-mail and triggers another script based on the keywords found (which are saved in a csv-file, so it's easy to add new ones) :)

If you have any ideas on what to add/change/fix please help me by replying! :wave:

For more info on the cmdlets, see:
http://www.telldus.com/forum/viewtopic.php?f=22&t=3367

Post Reply