Announcement

Collapse
No announcement yet.

Somfy MyLink Script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Somfy MyLink Script

    I have written a script to control Somfy motors using a MyLink. To set it up do the following:
    1. Setup the MyLink using the Somfy smart phone app (iPhone version shown). Make sure you have paired your devices and everything is working as it should.
    2. Go to the App’s menu and choose Integration. The following screen will appear:
    Click image for larger version

Name:	Integration Screen.png
Views:	1729
Size:	112.5 KB
ID:	1340293
    1. Choose Control4 and create a System ID. I chose “shutters”. Once you have created your System ID, select Get Integration Report.
    Click image for larger version

Name:	System ID Screen.png
Views:	827
Size:	269.3 KB
ID:	1340294
    1. A screen similar to the following will appear:
    Click image for larger version

Name:	Integration Report.png
Views:	975
Size:	249.9 KB
ID:	1340295
    You will use the IP address, System ID, and Target Ids as parameters to the script.
    1. Create a HomeSeer counter named “Somfy MyLink”. This counter is used to assure that the JSON transaction ids are unique. It will automatically increment and reset once it reaches 99.
    2. Copy the script to the HomeSeer scripts directory and name it somfy.vb or a name of your choosing.
    3. Create an event. Similar to the following:
    Click image for larger version

Name:	HomeSeer Event.png
Views:	822
Size:	21.4 KB
ID:	1340296

    The parameters are: IP address of the MyLink, System ID, the Target ID of the motor, and the operation. The operation can be either Up, down, or STOP. The operations are not case sensitive.

    Code:
    Public Sub Main(parms as object)
    'parameters are IP Address, System Name, Target ID, Operation(up,down,or stop)
        Dim parmArray(4) As String
        Dim tcpC as New System.Net.Sockets.TcpClient()
        Dim networkStream As System.Net.Sockets.NetworkStream
        Dim strResponse As String = "No response"
    'Change verbose to False to eliminate most of the logging
        Dim verbose As Boolean = False
        Dim jsonString As String
        Dim port As Integer = 44100
        Dim ipAddress as String
        Dim method As String
        Dim targetID As String
        Dim auth As String
        Dim action as String
        Dim cv as Integer = hs.CounterValue("Somfy MyLink")
    'Reset the counter once it reaches 99 so that it doesn't keep growing
        If (cv >= 99) Then
          hs.CounterReset("Somfy MyLink")
        End If
        hs.CounterIncrement("Somfy MyLink")
        Dim id as String = CStr(cv)
    
    'This is required because Events pass parameters as a single string
    'but HSTouch passes parameters as an array of strings
      If parms.GetType().ToString = "System.String" Then
    'Event
        parmArray = parms.ToString.Split(Convert.ToChar(","))
        ipAddress = parmArray(0)
        auth = parmArray(1)
        targetID = parmArray(2)
        action = parmArray(3).ToUpper()
        If (verbose) Then
          hs.WriteLog("MyLink", "Event: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth)
        End If
      Else
    'HSTouch
        ipAddress = parms(0).ToString()
        auth = parms(1).ToString()
        targetID = parms(2).ToString()
        action = parms(3).ToString().ToUpper()
        If (verbose) Then
          hs.WriteLog("MyLink", "HSTouch: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth)
        End If
      End If
    If action = "UP" Then
       method = "mylink.move.up"
    ElseIf action = "DOWN" Then
       method = "mylink.move.down"
    Else
       method = "mylink.move.stop"
    End If
        jsonString = "{""id"":"+ id + ",""method"":""" + method + ""","
        jsonString = jsonString + """params"":{""targetID"":"""+targetID + ""","
        jsonString = jsonString + """auth"":""" + auth + """}}"
        jsonString = jsonString +"{""jsonrpc"":""2.0"",""result"":true,""id"":" + id + "}"
    
    Try
        tcpC.Connect(ipAddress, port)
        networkStream = tcpC.GetStream()
    Catch e As Exception
        hs.WriteLog("MyLink", "Network Error: " + e.ToString())
        End Try
        If networkStream.CanWrite Then
           Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(jsonString)
           networkStream.Write(sendBytes, 0, sendBytes.Length)
           Dim bytes(tcpC.ReceiveBufferSize) As Byte
           networkStream.Read(bytes, 0, CInt(tcpC.ReceiveBufferSize))
           Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes)
           strResponse = "Device responded: " & CStr(returndata)
        Else
           If Not networkStream.CanWrite Then
           strResponse = "Error: Cannot write data to the device."
           End If
        End If
        If (verbose) Then
          hs.WriteLog("MyLink", "JSON=" + jsonString)
        End If
        hs.WriteLog("MyLink",strResponse)
        tcpC.Close()
    End Sub

    There is a Boolean variable named verbose that is set to False in the script. You may want to set it to True to receive additional logging messages for troubleshooting.

    The script connects to the MyLink, sends the JSON, reads the response and disconnects. A connection is not maintained.

    #2
    Hi Kirby, you put a lot of effort into this! Great work. For Android, similar I suppose? Will thus work for hard wired dry contact systems? RS485 or has to be RTS wireless motors? All of mine are hardwired Sonesse 30 into a distribution block. Thanks!

    Comment


      #3
      The script only communicates with Somfy's MyLink. The Mylink communicates using RTS wireless communications with the motors. You pair the motors to the MyLink using the MyLink app.

      Comment


        #4
        I just found this. It works perfectly. Thank you so much for sharing it

        Comment


          #5
          Originally posted by Kirby View Post
          The script only communicates with Somfy's MyLink. The Mylink communicates using RTS wireless communications with the motors. You pair the motors to the MyLink using the MyLink app.
          Kirby, what method does this script use to communicate with the mylink? Is it over wifi or RS232 or 485? I'm doing an installation of 4 phantom screens which run with Somfy Maestri motors. Presumably the Mylink would control them, and now I'm trying to figure out how to control the mylink with HS3.
          Finally can the script be modified to control multiple motors, or is designed for one script per motor?
          Thanks for this script and any advice and guidance you can share.

          Comment


            #6
            The script communicates using WIFI via the smart phone app. I have eight shutters (motors) that I control. You can control as many as you have set up in the app.
            The Target ID determines which motor or group is acted on.

            Comment


              #7
              Originally posted by Kirby View Post
              The script communicates using WIFI via the smart phone app. I have eight shutters (motors) that I control. You can control as many as you have set up in the app.
              The Target ID determines which motor or group is acted on.
              Thanks Kirby. Does it work locally, or have to go out to Somfy servers somewhere? If local, I would imagine actions would be quite timely and reliable.
              In my project the walls are all open right now, so I can put any wire in that I want very inexpensively. Would you suggest putting cat 5 in wall but likely never using it?

              Comment


                #8
                The MyLinks work locally. The first parameter of the script is the ip address of the my link. I have two but you can have as many as you want. I ran cat 6 in my walls. You won't use as many connections as you might think but I do use the hardwire for WAPs located through the house and also I use TIVO remotes for TV which need a hardwire connection. It is better to have too many CAT6 runs then not enough.

                Comment


                  #9
                  I think I get it now. The script stays the same and doesn't need to be modified. The parameters in the script running event define the mylink, shade and action of the shade. Can I assume that "stop" is a pre-defined location between up and down?
                  I probably will run cat 6 as backup, as I have to run the power lines to the motors anyway. But this script makes it appear that wires are unnecessary. Thanks for this! I look forward to getting my screens installed and using this.

                  Comment


                    #10
                    You are correct. The script stays the same. The MyLink is a plugin module that talks WIFI. It doesn't know or care whether it is the MyLink smart app or HomeSeer. When it receives a command, it communicates it to the appropriate motor(s) using RTS.
                    The stop works the same as if you pushed stop on your remote or smart app. It is not predefined. I use it to put a shutter partially down by first sending a down then having the event sleep for a few seconds and then sending a stop. It is not perfect but it is surprising how reproduceable it is. It just takes a little tinkering to get the timing set right.

                    Comment


                      #11
                      This sounds great. I assume this is one way communication only, so to open you just send an UP command (and presumably the motors know to stop when the shade is completely open), to close is a DOWN command (and the motor stops when completely closed. If you want a shade to be 50% open, perhaps you send an up command first to get to the known state of completely open, wait the amount of time it takes to completely open the shade from the closed state, then a down command followed by a STOP command timed such that the shade will be 1/2 closed. That way HS3 can get the shade 1/2 closed, regardless of the starting state. I'm eager to try it!

                      Comment


                        #12
                        Originally posted by jquincy View Post
                        I just found this. It works perfectly. Thank you so much for sharing it
                        Any idea if this works with HS4 too?

                        I tried and nothing happens... The logs show a success, but the shades don't move.

                        Comment


                          #13
                          Yes. I'm using it in HS4. I don't remember if I had to change anything. Here is the script that I'm using in HS4.

                          Code:
                          Public Sub Main(parms as object)
                          'parameters are IP Address, System Name, Target ID, Operation(up,down,or stop)
                          Dim parmArray(4) As String
                          Dim tcpC as New System.Net.Sockets.TcpClient()
                          Dim networkStream As System.Net.Sockets.NetworkStream
                          Dim strResponse As String = "No response"
                          'Change verbose to False to eliminate most of the logging
                          Dim verbose As Boolean = True
                          Dim jsonString As String
                          Dim port As Integer = 44100
                          Dim ipAddress as String
                          Dim method As String
                          Dim targetID As String
                          Dim auth As String
                          Dim action as String
                          Dim cv as Integer = hs.CounterValue("Somfy MyLink")
                          'Reset the counter once it reaches 99 so that it doesn't keep growing
                          If (cv >= 99) Then
                          hs.CounterReset("Somfy MyLink")
                          End If
                          hs.CounterIncrement("Somfy MyLink")
                          Dim id as String = CStr(cv)
                          
                          'This is required because Events pass parameters as a single string
                          'but HSTouch passes parameters as an array of strings
                          If parms.GetType().ToString = "System.String" Then
                          'Event
                          parmArray = parms.ToString.Split(Convert.ToChar(","))
                          ipAddress = parmArray(0)
                          auth = parmArray(1)
                          targetID = parmArray(2)
                          action = parmArray(3).ToUpper()
                          If (verbose) Then
                          hs.WriteLog("MyLink", "Event: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth)
                          End If
                          Else
                          'HSTouch
                          ipAddress = parms(0).ToString()
                          auth = parms(1).ToString()
                          targetID = parms(2).ToString()
                          action = parms(3).ToString().ToUpper()
                          If (verbose) Then
                          hs.WriteLog("MyLink", "HSTouch: ID=" + id + " IP Address= " + ipAddress + " auth= " + auth)
                          End If
                          End If
                          If action = "UP" Then
                          method = "mylink.move.up"
                          ElseIf action = "DOWN" Then
                          method = "mylink.move.down"
                          Else
                          method = "mylink.move.stop"
                          End If
                          jsonString = "{""id"":"+ id + ",""method"":""" + method + ""","
                          jsonString = jsonString + """params"":{""targetID"":"""+targetID + ""","
                          jsonString = jsonString + """auth"":""" + auth + """}}"
                          jsonString = jsonString +"{""jsonrpc"":""2.0"",""result"":true,""id"":" + id + "}"
                          
                          Try
                          tcpC.Connect(ipAddress, port)
                          networkStream = tcpC.GetStream()
                          Catch e As Exception
                          hs.WriteLog("MyLink", "Network Error: " + e.ToString())
                          End Try
                          If networkStream.CanWrite Then
                          Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(jsonString)
                          Dim numBytesRead as Integer
                          Dim bufsize as Integer = 100
                          networkStream.Write(sendBytes, 0, sendBytes.Length)
                          Dim bytes(bufsize) As Byte
                          numBytesRead = networkStream.Read(bytes, 0, bufsize)
                          Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes,0,numBytesRead)
                          strResponse = "Device responded: " & CStr(returndata)
                          Else
                          If Not networkStream.CanWrite Then
                          strResponse = "Error: Cannot write data to the device."
                          End If
                          End If
                          If (verbose) Then
                          hs.WriteLog("MyLink", "JSON=" + jsonString)
                          End If
                          hs.WriteLog("MyLink",strResponse)
                          tcpC.Close()
                          End Sub

                          Comment


                            #14
                            Thx Kirby for the script. I copied your script as written for HS4 but it doesn't seem to be working for me. Below is the first few lines of your script, my somfy integration report and a screen shot of my event and my error.

                            First Few lines of Script

                            Public Sub Main(parms as object)
                            192.168.114.105,Blinds,CC107FB3.1,up
                            Dim parmArray(4) As String
                            Dim tcpC as New System.Net.Sockets.TcpClient()

                            Integration report

                            System ID : Blinds
                            IP address : 192.168.114.105
                            System pin : 4029
                            CC107FB3 Shades

                            Target: CC107FB3.1
                            Name: Dining Roller Shade

                            Target: CC107FB3.2
                            Name: To Be Deleted

                            Target: CC107FB3.3
                            Name: Kitchen Shade - Final

                            Scenes

                            ID: 0003
                            Name: All Up Kitchen

                            ID: 0004
                            Name: All Down Kitchen

                            ID: 0001
                            Name: All Up Dining

                            ID: 0002
                            Name: All Down - Dining


                            This is my error:
                            Running script, script run or compile error in file: somfy.vb.txt1006:Expected ')' in line 1 More info: Expected ')'

                            Click image for larger version

Name:	Capture.PNG
Views:	534
Size:	427.3 KB
ID:	1417977







                            Attached Files

                            Comment


                              #15
                              Hey Kirby,

                              This is a great script. I have an account at the developer site, but their API documentation is a confusing mess - they want everybody to go through their cloud infrastructure. So how/where did you get the myLink command list? The reason I ask is that through the myLink App (POS) I can tell a blind to go to the "my" position which I programmed in all of the shades. However, with the Tahoma I found position and rotation commands that allow venetian blinds to be set to a specific rotation or shades to be set to an exact position. Do you know how I can do these with the myLink? The "my" position does not appear as a scene either.

                              Thanks

                              Tink
                              Regards,

                              Rick Tinker (a.k.a. "Tink")

                              Comment

                              Working...
                              X