I am writing code to operate my sprinklers. It is an interface between my MainLobby and Homeseer. I have three programs in the application, and each one has a user-settable start time for the program to start. I am thinking of storing the start time in an event (so it is easily accessible from MainLobby). How do I store and manipulate a time in an event using a script? I looked at the DateAdd function but this doesn't do what I want because this is all date based. For example, the start time may be set to 7:00am, and the user has the option of changing it to 8:00am. I want to be able to retrieve the time from the device string, change it to 8:00am, and then save it. Elsewhere I am going to turn the sprinklers on at this time so I would need to read the time from the device. I haven't looked at that part yet but I assume if I can get the first part working that should be pretty easy. Please ask if I wasn't clear! Thanks.
Announcement
Collapse
No announcement yet.
Question about manipulating time in a device
Collapse
X
-
To be sure I understand, you simply need a storage place for a start time that can be accessed from other scripts? If so you can use a virtual device to store the time or an ini file to store the time. If you have a virtual device, say Z1, you can store the time like this:
hs.setDeviceString "Z1", "8:00" then to read the deviceString use
sVal = hs.deviceString("Z1")
sVal can be used in a script
-
Sorry about this - I knew I wasn't clear. My question surrounds manipulating the time. For example, let's say I have 8:00 stored in the string, I load it into my script, the user adds 20 minutes to this time. So I need to add 20 minutes to 8:00. With DateAdd I can do this but it does everything in date format. I want the output to be 8:20, and if they added 20 minutes to 23:50 it would return 00:10. Are their time functions like this? I guess I could use DateAdd and ignore the date?
Comment
-
You're better off storing the date and just ignoring it. The DateAdd functions handle rolling over days, DST, leap years, etc.
In VB.NET, you would use something like this:
PHP Code:Public Sub Main(ByVal Parms As String)
Dim oldVal As Date
Dim newVal As Date
If (hs.DeviceString("A1") Is Nothing) Then 'give it an initial value if blank
hs.SetDeviceString("A1", Now())
End If
oldVal = CDate(hs.DeviceString("A1"))
newVal = DateAdd(DateInterval.Minute, 20, oldVal) 'add 20 mins
hs.SetDeviceString("A1", newVal)
End Sub
HS4Pro on a Raspberry Pi4
54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI
HSTouch Clients: 1 Android
Comment
-
Thanks, I got this working and I see it stores a default date in 1900 each time which is fine. When I store the actual string I only want to put the time in there. I tried the FormatDateTime function but I can't seem to get it to do anything. I only want to store the time in HH:MM format. Are you familiar with the syntax of this? Here is my code:
Code:Program1StartTime = DateAdd("n", -60, Program1StartTime) FormatDateTime Program1StartTime, 4
Comment
-
I've not used FormatDateTime - but you could store the time as a string using the time functions:
dim s as string = cstr(hour(Program1StartTime)) & ":" & cstr(minute(Program2StartTime))HS4Pro on a Raspberry Pi4
54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI
HSTouch Clients: 1 Android
Comment
-
Originally posted by simonmason View PostThanks, I got this working and I see it stores a default date in 1900 each time which is fine. When I store the actual string I only want to put the time in there. I tried the FormatDateTime function but I can't seem to get it to do anything. I only want to store the time in HH:MM format. Are you familiar with the syntax of this? Here is my code:
Code:Program1StartTime = DateAdd("n", -60, Program1StartTime) FormatDateTime Program1StartTime, 4
Comment
-
I get an error using this. I think this is something to do with vbs vs. vb? I am not positive as I was never formally trained in VB, I just picked it up from my early programming training in older languages.
5/31/2010 2:49:16 PM - Error - Running script, script run or compile error in file: sprinkler.vbs13:Type mismatch: 'Program1StartTime' in line 126 More info: Type mismatch: 'Program1StartTime'
Comment
-
Yeah, the example I gave was VB.NET, so make sure you save it with a ".vb" extension.
You will need to Dim the variable:
Dim Program1StartTime as Date
Hope this helps...HS4Pro on a Raspberry Pi4
54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI
HSTouch Clients: 1 Android
Comment
-
Next dumb question. Can I get around this by not converting to .vb vs. .vbs. I renamed the file and threw errors all over the place. Not being that knowledgeable about the difference between the two I don't really know where to start in trying to convert my vbs script into a vb.net script? Any advice? Thanks.
Comment
-
Yeah, it can be a little daunting to go through and convert, but it may not be needed.
Post your code and we'll give it a shot at keeping it in .vbs for you...HS4Pro on a Raspberry Pi4
54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI
HSTouch Clients: 1 Android
Comment
-
It's quite simple. I have a device (Q46) that I want to store a start time for the sprinklers to turn on. In MainLobby I have a screen that allows me to adjust the start or end time of a program (see attached screen shot). So I want to retrieve the time (HH:MM) from the device, adjust it up of down (I have three programmable time intervals - 5, 15 or 60 mins) and then resave the time in the device. The basic logic for making the start time earlier is below. I haven't copied and pasted all of the code I have tried using DateAdd and other functions because none of it has worked.
Now that I am writing this I am not sure how I am going to achieve the second part of this. I need an event that triggers when the time is equal to the value in Q46. Is this possible? Thanks.
Code:Program1StartTime = hs.devicestring("Q46") case "STARTDN" if TimeInterval5 = 2 then Program1StartTime = Program1StartTime - 5 minutes elseif TimeInterval15 = 2 then Program1StartTime = Program1StartTime - 15 minutes else Program1StartTime = Program1StartTime - 60 minutes
Comment
-
Here's a VBS version based on the code above. I tested it out here and it seems to work ok.
First, make sure Q46 is setup as a virtual device.
Next, you'll need to edit this script to include your "timeInterval5" variable - not sure where that is coming from. I hard-coded some values to test.
PHP Code:Sub main()
Program1StartTime = hs.deviceString("Q46")
if(Program1StartTime="") then
Program1StartTime = now
end if
''test section
timeInterval5=3
select case timeInterval5
case 2 Program1StartTime=dateadd("n",-5,Program1StartTime)
case 3 Program1StartTime=dateadd("n",-15,Program1StartTime)
case 4 Program1StartTime=dateadd("n",-60,Program1StartTime)
end select
hs.setDeviceString "Q46",cstr(Program1StartTime),True
End Sub
On the event side, you might just have a recurring event setup and check the date/time inside the script when it runs - that may work for you.
Your ML screen looks cool - let us know if this works for you...
Thanks!
RobHS4Pro on a Raspberry Pi4
54 Z-Wave Nodes / 21 Zigbee Devices / 108 Events / 767 Devices
Plugins: Z-Wave / Zigbee Plus / EasyTrigger / AK Weather / OMNI
HSTouch Clients: 1 Android
Comment
Comment