Announcement

Collapse
No announcement yet.

How do I write to a file from within a script?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I write to a file from within a script?

    I want to start monitoring my Air Conditioner run time and I think the easiest way to do that is to monitor my ZWave thermostat, and simply log to a file whenever the AC turns on and off. I can easily create an event to call a script whenever the AC goes on or off, but I don't know how to actually append a line to my log file from within a script. Can someone give me an example of a script that will append a line to an existing file with the current time, date, and AC status. I'm thinking I'll try to do a comma delimited thing so I can pull it up in Excel. Something like:

    7/22/08,9:21,ON
    7/22/08,9:53,OFF

    Thanks much,
    Brett

    #2
    You can use a script like this to write a new line to a text file each time the event is run
    Code:
    sub main(sVal)
    Dim fso,f1
    Const OpenFileForReading = 1
    Const OpenFileForWriting = 2
    Const OpenFileForAppending = 8
    
    
    	Path = "ACRun.log"
    	Set fso = CreateObject("Scripting.FileSystemObject")
    	Set file = fso.OpenTextFile(path, 8,True)
    	
    	file.WriteLine( "AC on at " & now())
    	file.Close
    end sub
    Last edited by Rupp; July 22, 2008, 09:00 AM.
    💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

    Comment


      #3
      couldnt you also use powertrigger? free way to throw it into a csv file.. and can chart it.......or do a count within it.....
      HW - i5 4570T @2.9ghz runs @11w | 8gb ram | 128gb ssd OS - Win10 x64

      HS - HS3 Pro Edition 3.0.0.435

      Plugins - BLRF 2.0.94.0 | Concord 4 3.1.13.10 | HSBuddy 3.9.605.5 | HSTouch Server 3.0.0.68 | RFXCOM 30.0.0.36 | X10 3.0.0.36 | Z-Wave 3.0.1.190

      Hardware - EdgePort/4 DB9 Serial | RFXCOM 433MHz USB Transceiver | Superbus 2000 for Concord 4 | TI103 X-10 Interface | WGL Designs W800 RF | Z-Net Z-Wave Interface

      Comment


        #4
        Originally posted by TeleFragger View Post
        couldnt you also use powertrigger? free way to throw it into a csv file.. and can chart it.......or do a count within it.....
        Hmm... I probably could... at least I could if I knew what powertrigger was

        :: off to do a bit of research::

        Thanks TeleFragger and Rupp

        Brett

        Comment


          #5
          The first script i posted is a vb script. If you prefer a vb.net script this use this :
          Code:
          Sub Main(parm as object)
          
          Dim objReader As System.IO.StreamWriter
          Dim FullPath as String = "c:\temp\ACRun.txt"
          
                  Try
                      objReader = New System.IO.StreamWriter(FullPath)
                      objReader.Write("AC On: "  & now())
                      objReader.Close()
                  Catch Ex As Exception
                      System.Windows.Forms.Messagebox.show(Ex.Message)
          
                  End Try
          
          
          End Sub
          💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

          Comment


            #6
            Here is the VB.Net script I use. It is a bit more complicated but it also writes the data to the Homeseer log file and temperature data to a text file. It creates a new file every month.

            -----------------------------------------------------------------------------------
            ' VB.NET script
            'Air Conditioner status via DS10A sensor - 4/12/08
            Imports system.io
            Sub Main(ByVal Parm As Object)
            dim CurState as integer
            dim s,s2,t3,t7,t8,datenow,timenow,n1,n2 as string
            dim fso as object
            dim f as object
            CurState = hs.devicestatus ("\6")
            select case (CurState)
            case 2 ' OFF
            hs.writelog ("Air Conditioner", "OFF")
            s="OFF"
            case 3 'ON
            hs.writelog ("Air Conditioner", "ON")
            s="ON"
            case 4 ' DIM
            hs.writelog ("Air Conditioner", "DIM")
            case 17 'ERROR
            hs.writelog ("Air Conditioner", "ERROR")
            end select

            hs.waitsecs (1)
            hs.setdevicestring (("\6"),s,true)

            t3=hs.devicestring("r7") 'outside temperature
            t8=hs.devicestring("r1") 'Downstairs temperature
            t7=hs.devicestring("r9") 'furnace plenum temperature
            n1=year(now)
            n2=month(now)
            s2 = ("c:\temp\Air Conditioner log "&(n2)&"_"&(n1)&".txt")

            datenow=FormatDateTime(now,2)
            timenow=FormatDateTime(now,4)

            Const ForReading = 1, ForWriting = 2, ForAppending = 8
            fso = CreateObject("Scripting.FileSystemObject")
            f = fso.OpenTextFile(s2, ForAppending, True)
            f.writeline ((datenow)&" "&(timenow)&" "&"Air Conditioner"&" "&(s)&" "&fix(t8)&" "&fix(t3)&" "&fix(t7))
            end sub
            ---------------------------------------------------------------
            Steve Q
            HomeSeer Version: HS3 Pro Edition 3.0.0.368, Operating System: Microsoft Windows 10 - Home, Number of Devices: 373, Number of Events: 666, Enabled Plug-Ins
            2.0.83.0: BLRF, 2.0.10.0: BLUSBUIRT, 3.0.0.75: HSTouch Server, 3.0.0.58: mcsXap, 3.0.0.11: NetCAM, 3.0.0.36: X10, 3.0.1.25: Z-Wave,Alexa,HomeKit

            Comment


              #7
              I realized that I'm resurrecting a very, very old thread -- however, a question with these types of scripts -- if you append to a file that doesn't exist, does it automatically create the file first?

              Comment


                #8
                Originally posted by mikedr View Post
                I realized that I'm resurrecting a very, very old thread -- however, a question with these types of scripts -- if you append to a file that doesn't exist, does it automatically create the file first?
                The True in this argument creates the file if it doesn't exist.
                f = fso.OpenTextFile(s2, ForAppending, True)
                💁‍♂️ Support & Customer Service 🙋‍♂️ Sales Questions 🛒 Shop HomeSeer Products

                Comment


                  #9
                  Originally posted by Rupp View Post
                  The True in this argument creates the file if it doesn't exist.
                  f = fso.OpenTextFile(s2, ForAppending, True)
                  Great! Thank you so much.

                  Comment


                    #10
                    Originally posted by Rupp View Post
                    The first script i posted is a vb script. If you prefer a vb.net script this use this :
                    Code:
                    Sub Main(parm as object)
                    
                    Dim objReader As System.IO.StreamWriter
                    Dim FullPath as String = "c:\temp\ACRun.txt"
                    
                            Try
                                objReader = New System.IO.StreamWriter(FullPath)
                                objReader.Write("AC On: "  & now())
                                objReader.Close()
                            Catch Ex As Exception
                                System.Windows.Forms.Messagebox.show(Ex.Message)
                    
                            End Try
                    
                    
                    End Sub
                    I saw this and thought I would offer a solution that would eliminate the potential for file lock:

                    Code:
                     Sub Main(parm as object)
                     Dim objReader As System.IO.StreamWriter
                    Dim fullPath as String = "H:\Logs\garageLight.log"
                             Try
                                objReader = New System.IO.StreamWriter(fullPath)
                                objReader.Write("Light Change: "  & now())
                            Catch Ex As Exception
                                'System.Windows.Forms.Messagebox.show(Ex.Message)
                     Finally
                         objReader.Close()
                            End Try
                     
                    End Sub

                    Comment


                      #11
                      Thank you!!

                      Originally posted by UltimateInternet View Post
                      I saw this and thought I would offer a solution that would eliminate the potential for file lock:

                      Code:
                       Sub Main(parm as object)
                       Dim objReader As System.IO.StreamWriter
                      Dim fullPath as String = "H:\Logs\garageLight.log"
                               Try
                                  objReader = New System.IO.StreamWriter(fullPath)
                                  objReader.Write("Light Change: "  & now())
                              Catch Ex As Exception
                                  'System.Windows.Forms.Messagebox.show(Ex.Message)
                       Finally
                           objReader.Close()
                              End Try
                       
                      End Sub

                      Comment


                        #12
                        Rupp I am trying to use your script, verbatim, as offered in #5, above. I keep getting an error, that I guess is part of some change in .net. Can you tell me what I need to do to fix this? These are the errors thrown:
                        Compiling script I:\Program Files (x86)\HomeSeer HS3\scripts\WriteToTestLog.vb: 'Forms' is not a member of 'Windows'.
                        Compiling script I:\Program Files (x86)\HomeSeer HS3\scripts\WriteToTestLog.vb: Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.
                        HomeSeer Version: HS4 Pro Edition 4.2.19.0 (Windows - Running as a Service)
                        Home Assistant 2024.3
                        Operating System: Microsoft Windows 11 Pro - Desktop
                        Z-Wave Devices via two Z-Net G3s
                        Zigbee Devices via RaspBee on RPi 3b+
                        WiFi Devices via Internal Router.

                        Enabled Plug-Ins
                        AK GoogleCalendar 4.0.4.16,AK HomeAssistant 4.0.1.23,AK SmartDevice 4.0.5.1,AK Weather 4.0.5.181,AmbientWeather 3.0.1.9,Big6 3.44.0.0,BLBackup 2.0.64.0,BLGData 3.0.55.0,BLLock 3.0.39.0,BLUPS 2.0.26.0,Device History 4.5.1.1,EasyTrigger 3.0.0.76,Harmony Hub 4.0.14.0,HSBuddy 4.51.303.0,JowiHue 4.1.4.0,LG ThinQ 4.0.26.0,ONVIF Events 1.0.0.5,SDJ-Health 3.1.1.9,TPLinkSmartHome4 2022.12.30.0,UltraCID3 3.0.6681.34300,Z-Wave 4.1.3.0

                        Comment


                          #13
                          Change:


                          Code:
                          System.Windows.Forms.Messagebox.show(Ex.Message)
                          To
                          Code:
                          hs.writelog("Error","Error in script: " & Ex.ToString)
                          Jon

                          Comment


                            #14
                            Thanks, jon00 I still have one problem, It writes one line to the file each time activated, but it doesn't append to the file as expected, it overwrites the first line.

                            Also, not a big deal, but it doesn't create the file if it doesn't already exist.
                            HomeSeer Version: HS4 Pro Edition 4.2.19.0 (Windows - Running as a Service)
                            Home Assistant 2024.3
                            Operating System: Microsoft Windows 11 Pro - Desktop
                            Z-Wave Devices via two Z-Net G3s
                            Zigbee Devices via RaspBee on RPi 3b+
                            WiFi Devices via Internal Router.

                            Enabled Plug-Ins
                            AK GoogleCalendar 4.0.4.16,AK HomeAssistant 4.0.1.23,AK SmartDevice 4.0.5.1,AK Weather 4.0.5.181,AmbientWeather 3.0.1.9,Big6 3.44.0.0,BLBackup 2.0.64.0,BLGData 3.0.55.0,BLLock 3.0.39.0,BLUPS 2.0.26.0,Device History 4.5.1.1,EasyTrigger 3.0.0.76,Harmony Hub 4.0.14.0,HSBuddy 4.51.303.0,JowiHue 4.1.4.0,LG ThinQ 4.0.26.0,ONVIF Events 1.0.0.5,SDJ-Health 3.1.1.9,TPLinkSmartHome4 2022.12.30.0,UltraCID3 3.0.6681.34300,Z-Wave 4.1.3.0

                            Comment


                              #15
                              There are other methods to do this in VB.NET.

                              Try this one:

                              Code:
                              Sub Main(ByVal Parm As String)
                                  Dim FullPath As String = "c:\temp\ACRun.txt"
                                  My.Computer.FileSystem.WriteAllText(FullPath, "AC On: " & Now() & vbCrLf, True)
                              End Sub
                              This should create and append as necessary.
                              Jon

                              Comment

                              Working...
                              X