Announcement

Collapse
No announcement yet.

Please Pachube code

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

    Please Pachube code

    Hi,

    Someone use Pachube wih homeseer succesfull , can post code in the forum for a chart temp... i search in the web and find two scripts with issues .

    Thanks

    #2
    I know I wrote at least one of them, if not both but posted in two threads. I no longer use Pachube (however I know someone on here was) as they were making changes to their API which meant you had to pay (I did get an email the other day saying that it was now free however). The API has probably changed a fair bit since I wrote those scripts so I would not be surprised if they did not work anymore, however if its a simple error I may be able to resolve it - if you have tried the one I wrote what error do you get?

    Comment


      #3
      charts

      Mr happy, i am not a programmer... syntaxis error - the vbs wait )- but a i'm interested to have a graph chart with temp, humidity etc...
      Now what web or tool you use for charts?

      Comment


        #4
        It was a vb.net script so would be named a .vb file rather than .vbs/.txt.

        I still have a login for pachube so I will test the script and see if it still works, as to what I am using now I have written plugins for my temp sensors (using the KitsRUs 3145 project board and DS18B20's) and electricity sensors (Current Cost) that have all of the charting functions now built in so I just call a procedure from a script and it produces the Google Charts URL.

        There are other ways out there to chart/graph in HS, quite a frequent topic really - a search will show some threads about it. You can build Google Charts from a script with ease, I did intend to produce some sort of general purpose plugin that would do charting with Google Charts but I ended up sidetracked unfortunately...

        Comment


          #5
          There were some issues and I have rewritten it, the script I originally wrote whenever it was (over a year ago I am sure) was when Pachube only had one API. They now have a new API version and the old one is being depreciated so its days were numbered in any case, this script is pretty basic - there is the function in Pachube to create/delete/amend streams remotely however I don't see a particular value of being able to do this via HS as you may aswell do it from the Pachube site.

          I've changed how it works so now instead of needing to amend the script to accept a list of devices it now uses a parameter. So save this script as a .vb file in the \scripts directory and send a parameter using hs.runex like this;

          hs.runex("pachube.vb", "Main", "#5|100|0")

          Where the first parameter (#5 in this) is your device code, the middle is a value to divide the first parameter by (leave at 0 for no division) and the last parameter is the datastream ID you set up when you create the stream on the Pachube website.

          Change your APIKey and FeedID and you should be ready to go. Fire the script off from an event on a recurring interval.

          (I am slightly interested in Pachube again as there is a droid widget which might come in handy for me).

          Here is the script;

          Code:
          Sub Main(ByVal Parms As String)
          
          '##### Pachube Upload Script ##### 10/11/11
          'call this script from a recurring event with the parameters of;
          'hs.runex("pachube.vb", "Main", "#5|100|0")
          'where #5 is your device code, 100 is the number which you should device the value of #5 by [leave at 0 for no division], 0 is the stream ID that you have used in Pachube
          'this will support an unlimited number of devices however you will need to call this script for each device
          'Common Errors and possible solutions:
          'Error 401 (UnAuthorised) - Check your APIKey is correct
          '403 (Forbidden) - correct APIKey but wrong FeedID
          '404 (Not Found) - FeedID does not exist at this time or the internet/pachube is down
          
          ' <<<<< YOU WILL NEED TO AMEND THE FOLLOWING STRINGS >>>>>
          
          Dim APIKey As String = "481056f9241a3e078a2082c97cdd07f06c1c27f42bba01bda0a608a0ae784xxx"
          Dim FeedID As Long = "39510"
          
          ' <<<<<< YOU GET THOSE VALUES WHEN YOU CREATE A FEED IN PACHUBE AND WHEN YOU SIGN UP >>>>>
          
          Dim Param() As String
          Dim WebStr As String = ""
          
          Param = Parms.Split("|")
          
          If hs.deviceexists(Param(0)) <> -1 Then
          
          Try  
          
          Dim strURL = "http://api.pachube.com/v2/feeds/" & FeedID & ".csv" 
          
          Dim wR As System.Net.WebRequest = System.Net.WebRequest.Create(strURL) 
          wR.Headers.Add("X-PachubeApiKey: " & APIKey) 
          
          wR.Method = "PUT" 
          
          Dim oEncoder As New System.Text.ASCIIEncoding() 
          
          If Param(1) <> 0 Then 
          
          webstr = Param(2) & ", " & (hs.devicevalue(Param(0)) / Param(1))
          
          Else
          
          webstr = Param(2) & ", " & hs.devicevalue(Param(0))
          
          End If
          
          hs.writelog("Pachube", "Uploading: " & webstr)
          
          Dim bytes As Byte() = oEncoder.GetBytes(webStr) 
          
          Dim postStream As System.IO.Stream = wR.GetRequestStream() 
          
          postStream.Write(bytes, 0, bytes.Length) 
          postStream.Close() 
          
          Dim wResponse As System.Net.WebResponse = wR.GetResponse() 
          
          Dim ResponseText As String 
          
          Using SReader As New system.io.StreamReader(wResponse.GetResponseStream) 
          
          ResponseText = SReader.readtoend() 
          
          End Using
          
          wR = Nothing 
          
          Catch ex As Exception
          
          hs.writelog("Pachube Error", "Error uploading to Pachube: " & ex.message) 
          
          End Try 
          
          Else
          
          hs.writelog("Pachube Error", "Device Code Does Not Exist") 
          
          End If
          
          End Sub
          Any problems tell me...

          Comment


            #6
            Thanks &amp; Decimals values issue

            Mr happy first i want to thank you for the interest and post the code for pachube...

            You can see my charts in https://pachube.com/feeds/39290

            Comment


              #7
              Originally posted by nickneutrino View Post
              Mr happy first i want to thank you for the interest and post the code for pachube...

              You can see my charts in https://pachube.com/feeds/39290
              Yes that will confuse the script as it is a set of Comma Seperate Values and adding an extra comma (albeit this time as a seperator) will think that the '2' in here is a seperate piece of data and not part of '20'.

              I presume that Pachube does not support comma seperated device values (I looked on a local feed close to you and their charts are using the decimal point not the comma) and as such what I have done is replace the comma in the value with a decimal.

              Can you try this version please...

              Code:
              Imports System.Text.RegularExpressions
              
              Sub Main(ByVal Parms As String)
              
              '##### Pachube Upload Script ##### 10/11/11
              'call this script from a recurring event with the parameters of;
              'hs.runex("pachube.vb", "Main", "#5|100|0")
              'where #5 is your device code, 100 is the number which you should device the value of #5 by [leave at 0 for no division], 0 
              
              is the stream ID that you have used in Pachube
              'this will support an unlimited number of devices however you will need to call this script for each device
              'Common Errors and possible solutions:
              'Error 401 (UnAuthorised) - Check your APIKey is correct
              '403 (Forbidden) - correct APIKey but wrong FeedID
              '404 (Not Found) - FeedID does not exist at this time or the internet/pachube is down
              
              ' <<<<< YOU WILL NEED TO AMEND THE FOLLOWING STRINGS >>>>>
              
              Dim APIKey As String = "481056f9241a3e078a2082c97cdd07f06c1c27f42bba01bda0a608a0ae784xxx"
              Dim FeedID As Long = "39209"
              
              ' <<<<<< YOU GET THOSE VALUES WHEN YOU CREATE A FEED IN PACHUBE AND WHEN YOU SIGN UP >>>>>
              
              Dim Param() As String
              Dim WebStr As String = ""
              
              Param = Parms.Split("|")
              
              If hs.deviceexists(Param(0)) <> -1 Then
              
              Try  
              
              Dim strURL = "http://api.pachube.com/v2/feeds/" & FeedID & ".csv" 
              
              Dim wR As System.Net.WebRequest = System.Net.WebRequest.Create(strURL) 
              wR.Headers.Add("X-PachubeApiKey: " & APIKey) 
              
              wR.Method = "PUT" 
              
              Dim oEncoder As New System.Text.ASCIIEncoding() 
              
              If Param(1) <> 0 Then 
              
              webstr = Param(2) & ", " & regex.replace((hs.devicevalue(Param(0)) / Param(1)).ToString, ",", ".")
              
              Else
              
              webstr = Param(2) & ", " & hs.devicevalue(Param(0))
              
              End If
              
              hs.writelog("Pachube", "Uploading: " & webstr)
              
              Dim bytes As Byte() = oEncoder.GetBytes(webStr) 
              
              Dim postStream As System.IO.Stream = wR.GetRequestStream() 
              
              postStream.Write(bytes, 0, bytes.Length) 
              postStream.Close() 
              
              Dim wResponse As System.Net.WebResponse = wR.GetResponse() 
              
              Dim ResponseText As String 
              
              Using SReader As New system.io.StreamReader(wResponse.GetResponseStream) 
              
              ResponseText = SReader.readtoend() 
              
              End Using
              
              wR = Nothing 
              
              Catch ex As Exception
              
              hs.writelog("Pachube Error", "Error uploading to Pachube: " & ex.message) 
              
              End Try 
              
              Else
              
              hs.writelog("Pachube Error", "Device Code Does Not Exist") 
              
              End If
              
              End Sub

              Comment


                #8
                Thank you very much

                Thank you very much now works very good.

                https://pachube.com/feeds/39290


                Thanks a lot
                Nickneutrino

                Comment


                  #9
                  No problems - I can see on your feeds the data appears to have stopped at the same value (?), if that is a problem with the script please tell me and I will look into it.

                  Comment


                    #10
                    Ive been playing around with this script and its very cool.

                    Ive got a motion detector logging all status changes on pachube.

                    I have run into a bit of a dilemma, however. My graph only updates when there is a device status change and as a result the graph is has a saw tooth appearance. What I would like to have is a more square wave appearance.

                    Any ideas?

                    I would need to send data of device value just before it changes to the new value and then again after it changes. I cant find a way to do this. Homeseer doesnt store previous device values.

                    Also, I havent figured out how to do custom values for the y axis.... Motion/No Motion.
                    Attached Files

                    Comment


                      #11
                      Originally posted by prsmith777 View Post
                      Ive been playing around with this script and its very cool.

                      Ive got a motion detector logging all status changes on pachube.

                      I have run into a bit of a dilemma, however. My graph only updates when there is a device status change and as a result the graph is has a saw tooth appearance. What I would like to have is a more square wave appearance.

                      Any ideas?

                      I would need to send data of device value just before it changes to the new value and then again after it changes. I cant find a way to do this. Homeseer doesnt store previous device values.

                      Also, I havent figured out how to do custom values for the y axis.... Motion/No Motion.
                      I still upload data to Pachube but don't do alot with it apart from look at it now and again, I upload my heating set point which does not change gradually like a temperature reading and I guess this is sort of how you want to display it?



                      I think you are triggering the script on device status/value change? Can you try running it on a recurring interval instead, that way hopefully will then show straighter edges as at the minute Pachube is not being given the 0 values between the motion sensor triggers which will display the sawtooth style you are seeing. Pachube needs the data all of the time for it to plot the charts correctly.

                      Sorry I don't know how to specify a value for the y-axis, it might be a question to ask Pachube themselves if it is possible, I think they have a fairly active forum.
                      Last edited by mrhappy; May 23, 2012, 02:57 PM.

                      Comment


                        #12
                        yes, I am only sending data with device status change. I did it this way to avoid constant sending of data every minute or so. My ultimate plan is to have several dozen devices graphed and I thought that might overload homeseer and pachube. I would like to use the graphs as the device string for the devices in homeseer and ultimately use them in hstouch. Your graph is what I am after.

                        I was thinking of running a script which would trigger on device status change and send one data point the opposite of the current one first, then send the correct one a few seconds later. This would in effect give me the square graph and avoid the tons of data every minute. This only works for On/Off, Motion/NoMotion, Open/Closed data, but should work in theory. Ideally it would be best to have a hs function to send previousdevicevalue, but that is not available. Maybe HS3? I will fiddle around with your script tonight and report progress.

                        Comment


                          #13
                          Originally posted by prsmith777 View Post
                          yes, I am only sending data with device status change. I did it this way to avoid constant sending of data every minute or so. My ultimate plan is to have several dozen devices graphed and I thought that might overload homeseer and pachube. I would like to use the graphs as the device string for the devices in homeseer and ultimately use them in hstouch. Your graph is what I am after.

                          I was thinking of running a script which would trigger on device status change and send one data point the opposite of the current one first, then send the correct one a few seconds later. This would in effect give me the square graph and avoid the tons of data every second. This only works for On/Off, Motion/NoMotion, Open/Closed data, but should work in theory. Ideally it would be best to have a hs function to send previousdevicevalue, but that is not available. Maybe HS3? I will fiddle around with your script tonight and report progress.
                          The more advanced API methods of Pachube may be able to support something like this, I believe you can do a block update of data which might be better for you. I use the simplest method which is uploading the date/time, the stream ID and the value and Pachube does the rest of the hard work.

                          Whilst I understand your concerns re uploading of data I don't think there is going to be much in the way of performance/bandwith implications as the uploaded data is minimal and the script is not that complex to have any implications on HS. They used to support multiple uploads in one set of data which could be another option, I just amended the script to be a bit more user friendly and be able to have this data sent via parameter. Several dozen devices might be a little bit painful to implement in this script so I would be tempted to look at the API docs and see whether you can still send multiple points at any one time. The data is still going to be minimal to upload.

                          I did have an intention of writing a plugin that would do some charting using Google Charts (which is what Pachube essentially is) but hopefully HS3 will support it by default - who knows.

                          Comment

                          Working...
                          X