Telldus.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
function Connect-TelldusLive
{
    <#
    .SYNOPSIS
    Connects to Telldus Live! using the specified credentials.
 
    .DESCRIPTION
    This function connects to Telldus Live! using the specified credentials.
 
    .EXAMPLE
    Connect-TelldusLive
 
    .EXAMPLE
    Connect-TelldusLive -Credential $PSCredentialObject
 
    .PARAMETER Credential
    Specifies a PSCredential object. For more information about the PSCredential object, type Get-Help Get-Credential.
 
    .PARAMETER SaveCredential
    Add to switch to save the credential you specify (encrypted).
     
    You can then connect by simply using the -UseSavedCredential switch. The credential is saved using your logon session.
 
    .PARAMETER UseSavedCredential
    Specify this switch to use a saved credential instead of specifying one.
 
    .NOTES
    Thank you Ispep (automatiserar.se) for coming up with the idea of saving credentials!
 
    #>


    [cmdletbinding(DefaultParameterSetName='SpecifyCredential')]
    param(
              [Parameter(Mandatory=$True, ParameterSetName='SpecifyCredential')]
              [System.Management.Automation.PSCredential] $Credential,

              [Parameter(Mandatory=$false, ParameterSetName='SpecifyCredential')]
              [Switch] $SaveCredential,

              [Parameter(Mandatory=$false, ParameterSetName='SavedCredential')]
              [Switch] $UseSavedCredential
          )


    $LoginPostURI="https://login.telldus.com/openid/server?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.mode=checkid_setup&openid.return_to=http%3A%2F%2Fapi.telldus.com%2Fexplore%2Fclients%2Flist&openid.realm=http%3A%2F%2Fapi.telldus.com&openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&openid.sreg.required=email&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select#"
    $turnOffURI="https://api.telldus.com/explore/device/turnOff"
    $CredentialFolder = Join-Path -Path $($env:APPDATA) -ChildPath TelldusPowerShellModule
    $CredentialFilename = 'TelldusCredentialFile.xml'
    $CredentialFilePath = Join-Path -Path $CredentialFolder -ChildPath $CredentialFilename

    $TelldusWEB = Invoke-WebRequest $turnOffURI -SessionVariable Global:Telldus

    if ($PSCmdlet.ParameterSetName -eq 'SavedCredential') {
        if (Test-Path -Path $CredentialFilePath) {
            $CredentialFromDisk = Import-Clixml -Path $CredentialFilePath
            # Load some credentials
            $Username = $CredentialFromDisk.Username
            # Get the password from the file
            $Password = $CredentialFromDisk.Password | ConvertTo-SecureString

            # Build the credential
            $Credential = New-Object System.Management.Automation.PsCredential($Username,$Password)
        }
        else {
            throw "Didn't locate any saved credentials. Please run this command with the 'SaveCredential' switch first to store the credentials."
        }
    }

    $form = $TelldusWEB.Forms[0]
    $form.Fields["email"] = $Credential.UserName
    $form.Fields["password"] = $Credential.GetNetworkCredential().Password

    try {
        $TelldusWEB = Invoke-WebRequest -Uri $LoginPostURI -WebSession $Global:Telldus -Method POST -Body $form.Fields -ErrorAction Stop
    }
    catch {
        throw "Failed to logon to Telldus Live! The error was: $($_.ToString())"
    }

    # Let's verify that we are logged in
    try {
        $FetchDevices = Get-TDDevice -ErrorAction Stop
    }
    catch {
        throw "Failed to call Telldus Live! Verify that your credentials are correct and that you can connect to the Telldus Live! service."
    }

    if ($SaveCredential.IsPresent) {

        if (-not (Test-Path -Path $CredentialFolder)) {
            $null = New-Item $CredentialFolder -Force -ItemType Directory
        }

        $SaveCredentialObj = [PSCustomObject] @{
            'Username' = $Credential.UserName
            'Password' = $Credential.Password | ConvertFrom-SecureString
        }

        $SaveCredentialObj | Export-Clixml $CredentialFilePath -Force
    }

    $form = $null

    [gc]::Collect()
}

function Get-TDDevice
{
    <#
    .SYNOPSIS
    Retrieves all devices associated with a Telldus Live! account.
 
    .DESCRIPTION
    This command will list all devices associated with an Telldus Live!-account and their current status and other information.
 
    .EXAMPLE
    Get-TDDevice
 
    .EXAMPLE
    Get-TDDevice | Format-Table
 
    #>


    [CmdletBinding()]
    Param()

    BEGIN { 
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }
    }

    PROCESS {
        $PostActionURI="https://api.telldus.com/explore/doCall"
        $Action='list'
        $SupportedMethods=19

        $request = @{'group'='devices';'method'= $Action;'param[supportedMethods]'= $SupportedMethods;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request

        $Results=$ActionResults.devices.ChildNodes

        foreach ($Result in $Results) {

            $PropertiesToOutput = @{
                                 'Name' = $Result.name;
                                 'State' = switch ($Result.state)
                                           {
                                                 1 { "On" }
                                                 2 { "Off" }
                                                16 { "Dimmed" }
                                                default { "Unknown" }
                                           }
                                 'DeviceID' = $Result.id;
                             

                                 'Statevalue' = $Result.statevalue
                                 'Methods' = switch ($Result.methods)
                                             {
                                                 3 { "On/Off" }
                                                19 { "On/Off/Dim" }
                                                default { "Unknown" }
                                             }
                                 'Type' = $Result.type;
                                 'Client' = $Result.client;
                                 'ClientName' = $Result.clientName;
                                 'Online' = switch ($Result.online)
                                            {
                                                0 { $false }
                                                1 { $true }
                                            }
                                 }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject | Select-Object Name, DeviceID, State, Statevalue, Methods, Type, ClientName, Client, Online
        }
    }

    END { }
}

function Set-TDDevice
{

    <#
    .SYNOPSIS
    Turns a device on or off.
 
    .DESCRIPTION
    This command can set the state of a device to on or off through the Telldus Live! service.
 
    .EXAMPLE
    Set-TDDevice -DeviceID 123456 -Action turnOff
 
    .EXAMPLE
    Set-TDDevice -DeviceID 123456 -Action turnOn
 
    .EXAMPLE
    SET-TDDevice -DeviceID 123456 -Action bell
 
    .PARAMETER DeviceID
    The DeviceID of the device to turn off or on. (Pipelining possible)
 
    .PARAMETER Action
    What to do with that device. Possible values are "turnOff", "turnOn" and "bell".
 
    .NOTES
    Thank you Ispep (automatiserar.se) for fixing "bell" support!
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,
      [Parameter(Mandatory=$True)]
      [ValidateSet("turnOff","turnOn", "bell", "down", "up")]
      [string] $Action)


    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }

        $PostActionURI = "https://api.telldus.com/explore/doCall"
    }

    PROCESS {

        $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request

        $Results=$ActionResults.device.status -replace "\s"

        Write-Verbose "Doing action $Action on device $DeviceID. Result: $Results."
    }
}

function Get-TDSensor
{
    <#
    .SYNOPSIS
    Retrieves all sensors associated with a Telldus Live! account.
 
    .DESCRIPTION
    This command will list all sensors associated with an Telldus Live!-account and their current status and other information.
 
    .EXAMPLE
    Get-TDSensor
 
    .EXAMPLE
    Get-TDSensor | Format-Table
 
    #>


    if ($Telldus -eq $null) {
        Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
        return
    }

    $PostActionURI="https://api.telldus.com/explore/doCall"

    $SensorListKeys = @{
        'group' = 'sensors'
        'method' = 'list'
        'field_includeIgnored' = ''
        'field_includeValues' = ''
        'field_includeScale' = ''
        'field_useAlternativeData' = ''
        'responseAsXml' = 'xml'
        'responseAsJson' = 'json'
    }

    $ActionResults=$null

    [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $SensorListKeys
    [datetime] $TelldusDate="1970-01-01 00:00:00"

    $TheResults=$ActionResults.sensors.ChildNodes

    foreach ($Result in $TheResults) {
        $SensorInfo=$Result

        $DeviceID=$SensorInfo.id.trim()
        $SensorName=$SensorInfo.name.trim()
        $SensorLastUpdated=$SensorInfo.lastupdated.trim()
        $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
        $clientid=$SensorInfo.client.trim()
        $clientName=$SensorInfo.clientname.trim()
        $sensoronline=$SensorInfo.online.trim()

        $returnObject = New-Object System.Object
        $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
        $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
        $returnObject | Add-Member -Type NoteProperty -Name LocationID -Value $clientid
        $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
        $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate
        $returnObject | Add-Member -Type NoteProperty -Name Online -Value $sensoronline

        Write-Output $returnObject
    }
}

function Get-TDSensorData
{
    <#
    .SYNOPSIS
    Retrieves the sensordata of specified sensor.
 
    .DESCRIPTION
    This command will retrieve the sensordata associated with the specified ID.
 
    .EXAMPLE
    Get-TDSensorData -DeviceID 123456
 
    .PARAMETER DeviceID
    The DeviceID of the sensor which data you want to retrieve.
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [Alias('id')] [string] $DeviceID)

    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }

        $sensorDataURI="https://api.telldus.com/explore/sensor/info"
        $PostActionURI="https://api.telldus.com/explore/doCall"
    }

    PROCESS {
        $request = @{'group'='sensor';'method'= 'info';'param[id]'= $DeviceID;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request
        [datetime] $TelldusDate="1970-01-01 00:00:00"

        $SensorInfo=$ActionResults.sensor
        $SensorData=$ActionResults.sensor.data

        $SensorName=$SensorInfo.name.trim()
        $SensorLastUpdated=$SensorInfo.lastupdated.trim()
        $SensorLastUpdatedDate=$TelldusDate.AddSeconds($SensorLastUpdated)
        $clientName=$SensorInfo.clientname.trim()
        $SensorTemp=($SensorData | ? name -eq "temp").value | select -First 1
        $SensorHumidity=($SensorData | ? name -eq "humidity").value | select -First 1

        $returnObject = New-Object System.Object
        $returnObject | Add-Member -Type NoteProperty -Name DeviceID -Value $DeviceID
        $returnObject | Add-Member -Type NoteProperty -Name Name -Value $SensorName
        $returnObject | Add-Member -Type NoteProperty -Name LocationName -Value $clientName
        $returnObject | Add-Member -Type NoteProperty -Name Temperature -Value $SensorTemp
        $returnObject | Add-Member -Type NoteProperty -Name Humidity -Value $SensorHumidity
        $returnObject | Add-Member -Type NoteProperty -Name LastUpdate -Value $SensorLastUpdatedDate

        Write-Output $returnObject
    }
}

function Set-TDDimmer
{
    <#
    .SYNOPSIS
    Dims a device to a certain level.
 
    .DESCRIPTION
    This command can set the dimming level of a device to through the Telldus Live! service.
 
    .EXAMPLE
    Set-TDDimmer -DeviceID 123456 -Level 89
 
    .EXAMPLE
    Set-TDDimmer -Level 180
 
    .PARAMETER DeviceID
    The DeviceID of the device to dim. (Pipelining possible)
 
    .PARAMETER Level
    What level to dim to. Possible values are 0 - 255.
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True,
                 ValueFromPipeline=$true,
                 ValueFromPipelineByPropertyName=$true,
                 HelpMessage="Enter the DeviceID.")] [Alias('id')] [string] $DeviceID,

      [Parameter(Mandatory=$True,
                 HelpMessage="Enter the level to dim to between 0 and 255.")]
      [ValidateRange(0,255)]
      [int] $Level)


    BEGIN {

        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }

        $PostActionURI="https://api.telldus.com/explore/doCall"
        $Action='dim'
    }

    PROCESS {

        $request = @{'group'='device';'method'= $Action;'param[id]'= $DeviceID;'param[level]'= $Level;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request

        $Results=$ActionResults.device.status -replace "\s"

        Write-Verbose "Dimming device $DeviceID to level $Level. Result: $Results."
    }
}

function Get-TDDeviceHistory
{
    <#
    .SYNOPSIS
    Retrieves all events associated with the specified device.
    .DESCRIPTION
    This command will list all events associated with the specified device
    .EXAMPLE
    Get-TDDeviceHistory
    .EXAMPLE
    Get-TDDeviceHistory | Format-Table
    #>


    [cmdletbinding()]
    param(
    [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
    [Alias('id')]
    [string] $DeviceID)

    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }
    }

    PROCESS {
        $PostActionURI="https://live.telldus.com/device/history?id=$DeviceID"

        $HistoryEvents = Invoke-RestMethod -Uri $PostActionURI -WebSession $Global:Telldus | select -ExpandProperty History

        foreach ($HistoryEvent in $HistoryEvents)
        {
            $PropertiesToOutput = @{
                                 'DeviceID' = $DeviceID
                                 'State' = switch ($HistoryEvent.state)
                                           {
                                                 1 { "On" }
                                                 2 { "Off" }
                                                16 { "Dimmed" }
                                                default { "Unknown" }
                                           }
                                 'Statevalue' = $HistoryEvent.statevalue
                                 'Origin' = $HistoryEvent.Origin;
                                 'EventDate' = (Get-Date "1970-01-01 00:00:00").AddSeconds($HistoryEvent.ts)
                                 }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject | Select-Object DeviceID, EventDate, State, Statevalue, Origin
        }
    }

    END { }
}


function Get-TDSensorHistoryData
{
    <#
    .SYNOPSIS
    Retrieves sensor data history from Telldus Live!
     
    .DESCRIPTION
    This command will retrieve the sensor history data of the specified sensor.
     
    .PARAMETER DeviceID
    The DeviceID of the sensor which data you want to retrieve.
 
    .PARAMETER After
    Specify from which date you would like to retrieve sensor history.
 
    Always use UTC time.
 
    .PARAMETER Before
    Specify the "end date" of the data samples.
    Default value is current date.
 
    Always use UTC time.
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456 | Format-Table
 
    .EXAMPLE
    Get-TDSensorHistoryData -DeviceID 123456 -After (get-date).AddDays(-1)
     
    Get's the history from yesterday until today
 
    #>


    [cmdletbinding(DefaultParameterSetName='AllData')]
    param(
        [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='AllData')]
        [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [Alias('id')]
        [string] $DeviceID,

        [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [DateTime] $After,

        [Parameter(Mandatory=$false, ValueFromPipelineByPropertyName=$true, ParameterSetName='DateRange')]
        [DateTime] $Before
    )

    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }
    }

    PROCESS {
        $PostActionURI="https://live.telldus.com/sensor/history?id=$DeviceID"

        if ($PSCmdlet.ParameterSetName -eq 'DateRange') {
            if (-not $Before) {
                $Before = (Get-Date).ToUniversalTime()
            }

            if ($Before -gt $After) {
                $FromDateToPost = [Math]::Floor((New-TimeSpan -Start '1970-01-01' -End $After).TotalSeconds)
                $ToDateToPost = [Math]::Floor((New-TimeSpan -Start '1970-01-01' -End $Before).TotalSeconds)

                $PostActionURI = $PostActionURI + "&fromdate=$FromDateToPost" + "&todate=$ToDateToPost"
            }
            else {
                throw 'The value for Before must be greater than the value for After.'
            }
        }

        $HistoryDataPoints = Invoke-RestMethod -Uri $PostActionURI -WebSession $Global:Telldus | Select-Object -ExpandProperty History

        foreach ($HistoryDataPoint in $HistoryDataPoints)
        {
            $PropertiesToOutput = @{
                                 'DeviceID' = $DeviceID
                                 'Humidity' = ($HistoryDataPoint.data | Where-Object { $_.Name -eq 'humidity' }).value
                                 'Temperature' = ($HistoryDataPoint.data | Where-Object { $_.Name -eq 'temp' }).value
                                 'Date' = (Get-Date "1970-01-01 00:00:00").AddSeconds($HistoryDataPoint.ts)
                                 }

            $returnObject = New-Object -TypeName PSObject -Property $PropertiesToOutput

            Write-Output $returnObject | Select-Object DeviceID, Humidity, Temperature, Date
        }
    }

    END { }
}


function Rename-TDDevice
{

    <#
    .SYNOPSIS
    Renames a device in Telldus Live!
 
    .DESCRIPTION
    Renames a device in Telldus Live!
 
    .EXAMPLE
    Rename-TDDevice -DeviceID 123456 -NewName MyNewDeviceName
 
    .PARAMETER DeviceID
    The DeviceID of the device to rename
 
    .PARAMETER NewName
    The new name for that device
 
    #>


    [CmdletBinding()]
    param(

      [Parameter(Mandatory=$True, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
      [Alias('id')]
      [string] $DeviceID,

      [Parameter(Mandatory=$True)]
      [string] $NewName)


    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }

        $PostActionURI = "https://api.telldus.com/explore/doCall"
    }

    PROCESS {

        $request = @{'group'='device';'method'= 'setName';'param[id]'= $DeviceID;'param[name]'= $NewName;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request

        $Results=$ActionResults.device.status -replace "\s"

        Write-Verbose "Doing action $Action on device $DeviceID. Result: $Results."
    }
}


function Get-TDEvent
{

    <#
    .SYNOPSIS
    List all events available in Telldus Live!
 
    .DESCRIPTION
    List all events available in Telldus Live!
 
    .EXAMPLE
    Get-TDEvent
 
    #>


    [CmdletBinding()]
    param()

    BEGIN {
        if ($Telldus -eq $null) {
            Write-Error "You must first connect using the Connect-TelldusLive cmdlet"
            return
        }

        $PostActionURI = "https://api.telldus.com/explore/doCall"
    }

    PROCESS {

        $request = @{'group'='events';'method'= 'list';'param[listonly]'= 1;'responseAsXml'='xml'}

        [xml] $ActionResults=Invoke-WebRequest -Uri $PostActionURI -WebSession $Global:Telldus -Method POST -Body $request

        $ActionResults.events.event
    }
}