Announcement

Collapse
No announcement yet.

Starting here - How do I do it with script, event or easytrigger

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

    Starting here - How do I do it with script, event or easytrigger

    I'm thinking aloud, not sure if it makes sense what I try to accomplish. Goal: I can read various setting in HS from my car, this includes, current miles, the current percent of charge, the ideal max charge and when I want to departure. What I would like to do with a script/event or easytrigger if possible is to calculate how many miles do I need to get to 80% from the current mile range. I know the car can charger at 30 Miles a hour so if I have 100 miles and 80% is 250 miles, I would need approximately 5 hours to charge. If I want to depart at 8 AM with full charge, then I would need to start charging at 3 AM by "Start" charging in the device under HS. I believe by doing it, I would not need to precondition the battery since it was charging and warm already. Any best way to accomplish it?

    #2
    Action a script that is triggered every minute. In the script calculate if charge will be sufficient if started now. If not then start the charger. If you want a changeable time when you want 80% the put the target time in a virtual device that is read by script.

    Comment


      #3
      I was able to put together a script ( I call it a miracle) currently from the script, I'm able to get the date and time where it should start charging, unfortunately I'm in a loss on what to do next, in the script I can show via the log when it should start (date/time) but not sure how to check for it and turn on the start charge device.

      Sent from my SM-G973U1 using Tapatalk

      Comment


        #4
        Assume you have the target date/time in variable startTime then you want a conditional if the current time is at least this time. If it is then turn on the charger.

        What you have not discussed is how you turn on the charger. Two options come to mind. First is you set a virtual device to true or false if you want the charger to be run. In another event the trigger is this virtual device and the action is the control of the device that represents the charger. Assume the ref for this virtual device is 1234.

        The second is that you use CAPI control in the action of the script that is computing the start time. This could be more involved depending upon how the charger device is controlled.

        The current time is "now" so the conditional expression is
        If startTime >= now then
        SetDeviceValueByRef(1234,100,true)
        else
        SetDeviceValueByRef(1234,0,true)
        End if

        if you use CAPI in the script then the easiest is when the charger device has ControlUse property defined for Off and On.. if you search hs3help.pdf for CAPI then you will see examples of how to use it and in particular its use with ControlUse.

        I am not at a computer so cannot provide code that I know provides an example.

        Anoher consideration is if you want to run the script all the time or only when the car is plugged in. This could be a conditional in the event trigger or logic in the script after the desired time has been computed.

        Comment


          #5
          Here is an untested example of a script that is run every minute

          Code:
          Sub Main(parms as object)
          . Dim currentMilageRef as Integer = 123
          Dim chargerRef as Integer = 456
          . Dim timeTarget as Date = #08:00#
          Dim chargeRate as Double = 30 'mile/hour
          . Dim milageTarget as Double = 250 'mile
          . Dim oCAPIControl as CAPIControl
          Dim currentMilage as Double = hs.DeviceValueEx(currentMilageRef) '100 mile
          Dim minutesToCharge as Double = (milageTarget - currentMilage) / chargeRate * 60
          Dim minutesTarget as Double = timeTarget.Hour * 60 + timeTarget.Minute
          Dim minutesNow as Double = now.Hour * 60 + now.Minute
          Dim startCharging as Boolean = (minutes Target- minutesNow) <= minutesToCharge
          . If startCharging then
          oCAPIControl = hs.GetSingleControlByUse(chargerRef,ePairControlUse._On)
          else
          oCAPIControl = hs.GetSingleControlByUse(chargerRef,ePairControlUse._Off)
          End if
          If oCAPIControl is Not Nothing then
          hs.CAPIControlHandler(oCAPIControl)
          End if
          End Sub

          Comment


            #6
            Hi Michael, thank you so much for this. I just saw this while I was going to show you what I was able to "create" Yours is very clean, mine is a hacking between other scripts. I may use your script but as learning experience, can you please take a look at mine/ I have a plan to run an event that runs every hour on the hour and when the time is zero, it should set this device. Right now when I did a test, it did not set the device to "Charge" but when I run it via the device status it does. I have appended the script and the device info.
            Attached Files

            Comment


              #7
              There are two things I see. Your script is storing to a DeviceValue to control the charger. You need to use CAPI to affect a physical device. Only virtual devices that have no interfaced hardware will work with DeviceValue. You can read DeviceValue to get Status of physical devices.

              Your screen shot shows two controls for start and stop with values of 1136 and 1137. The values are odd, but that does not matter. The control use is not specified so it makes it harder to control from script. I suggest you give each a control use of off and on.

              if you do not assign a control use then one of the other CAPIGet... methods will need to be used to get the CAPIControl. Hs3help.pdf shows options. I have not used the following, but should be close to an alternative.
              hs.CAPIGetSingleControl(2536,false,"Start",true false)

              Comment


                #8
                Thanks Michael, I will give it a try.

                Sent from my SM-G973U1 using Tapatalk

                Comment


                  #9
                  Hi Michael,
                  The code looks great. The only minor issue is that is not triggering when the time is the same. Another question, if I check for the "Start" today and the time to start is tomorrow, will it work? I added also the log. Thank you
                  11/16/2021 6:11:21 PM Script timeTarget 6:11:00 PM
                  11/16/2021 6:11:21 PM Script startCharging False
                  11/16/2021 6:11:21 PM Script minutesNow 1091
                  11/16/2021 6:11:21 PM Script minutesTarget 1091
                  11/16/2021 6:11:21 PM Script minutesToCharge 238
                  11/16/2021 6:11:21 PM Script Current Milage 131
                  11/16/2021 6:11:21 PM Script No time yet False

                  Code:
                  Sub Main(parms as object)
                  
                  Dim Debug=1
                  Dim currentMilageRef as Integer = 2500
                  Dim chargerRef as Integer = 456
                  Dim timeTarget as Date = #18:11#
                  Dim chargeRate as Double = 30 'mile/hour
                  Dim milageTarget as Double = 250 'mile
                  'Dim oCAPIControl as CAPIControl
                  Dim currentMilage as Double = hs.DeviceValueEx(currentMilageRef) '100 mile
                  Dim minutesToCharge as Double = (milageTarget - currentMilage) / chargeRate * 60
                  Dim minutesTarget as Double = timeTarget.Hour * 60 + timeTarget.Minute
                  Dim minutesNow as Double = now.Hour * 60 + now.Minute
                  'Dim startCharging as Boolean = (minutesTarget- minutesNow) <= minutesToCharge
                  Dim startCharging as Boolean = (minutesTarget- minutesNow) = minutesToCharge
                  
                  If startCharging then
                  hs.WriteLog("Start Charging now", startCharging)
                  hs.WriteLog("Current Milage ", currentMilage)
                  hs.WriteLog("minutesToCharge ", minutesToCharge)
                  hs.WriteLog("minutesTarget ", minutesTarget)
                  hs.WriteLog("minutesNow ", minutesNow)
                  hs.WriteLog("startCharging ", startCharging)
                  hs.WriteLog("timeTarget ", timeTarget)
                  
                  'oCAPIControl = hs.GetSingleControlByUse(chargerRef,ePairControlUse._On)
                  else
                  hs.WriteLog("No time yet", startCharging)
                  
                  'oCAPIControl = hs.GetSingleControlByUse(chargerRef,ePairControlUse._Off)
                  End if
                  
                  'If oCAPIControl is Not Nothing then
                  'hs.CAPIControlHandler(oCAPIControl)
                  'End if
                  
                  if Debug= 1 then
                  hs.WriteLog("Current Milage ", currentMilage)
                  hs.WriteLog("minutesToCharge ", minutesToCharge)
                  hs.WriteLog("minutesTarget ", minutesTarget)
                  hs.WriteLog("minutesNow ", minutesNow)
                  hs.WriteLog("startCharging ", startCharging)
                  hs.WriteLog("timeTarget ", timeTarget)
                  
                  end if
                  
                  
                  End Sub

                  Comment


                    #10
                    The only minor issue is that is not triggering when the time is the same.
                    I do not understand about the triggering. I'm guessing is that the charger is not started.

                    The code change the startCharging from <= to =. This is not a good technique because there is no guarantee that it will run exactly when you expect so you need script that is tolerant of execution scheduling variances. It should not be a problem to command the charging device to the start state multiple times, but if it is then add an additional check of the charging device not being in the start state. (i.e. hs.DeviceValue(chargerRef) <> chargingState, where charging state is likely somewhere between 0 and 4 based upon your screenshot. Since there is no text it is hard to tell what each of the state mean. The plugin provider should be able to tell you what it should be or even if commanding to start multiple times is a problem.)

                    Another question, if I check for the "Start" today and the time to start is tomorrow, will it work? I added also the log.
                    Your time target is 6:11 PM. This seems like an unusual time to be fully charged if the car is going to be sitting unused until 8 AM as state in the original post. If you return from work at 5 PM and want the car fully charged by 6:11 PM then the charger does not have sufficient capacity unless the battery are near full charge already.

                    You need to work through each of the use cases to assure the algorithm in the script works for all of them.

                    The use case I was considering is that you are charging the car before an 8 AM departure and the car was available to be charged earlier in the day with sufficient time to complete the charge.

                    You are now asking about a case where target time is later in the day. This should not matter since the time to charge does not change and if the car is not available early enough in the day to fully charge then it will not be fully charged at 6:11 PM, but it will be charged as much as possible.

                    The third use case is where the car is available for charge the prior day. Let us say Sunday at 5 PM and 120 minutes of charge are needed to fully charge. The script computes the start charging time will be 6:11 PM minus 120 minutes which is 4:11 PM (16:11 using 24 hour clock). This means that it will start charging at 4:11 PM on Sunday and will stop at 6:11 PM on Sunday and sit unused until 6:11 PM on Monday.

                    If it gets used earlier on Monday and returns then it will start charging again on Monday to try to get 80% by 6:11 PM Monday.

                    There are likely other use cases that should be worked through to know if the script will work in all cases.

                    Comment


                      #11
                      Michael so sorry to confuse you, my English is not my primary one. What I meant by "triggering" is that when the startcharging set in the condition ( example 8:00 am and currently is 8:00am), the if statement shows as false instead of true.
                      The time that you see in the script was set so I can test it without waiting until tomorrow morning.
                      The startCharging condition should only start one time, then the car will automatically stop charging when it get to the 250 miles or 80%.

                      As an FYI, when I get everything to work ok in the script, I plan to run it as follow.
                      I start the charger in the evening when I get back to work, normally around 5 pm. I depart in the morning around 8 so it should start charging based on your script calculations. I could start running the script every minute after midnight, that should not be a problem. In the event I can check if the charger is plugged in as well.

                      Thanks

                      Sent from my SM-G973U1 using Tapatalk

                      Comment


                        #12
                        By the way, right now I'm doing baby steps, I plan to see if the script logs when the startcharging condition is set to start charging. The next step is use that capi function to actually start the charger. (Currently commented out) I contacted the author of the Plugin, he said that he was going to look into it but so far no answer yet. I have few ideas, the last resort would be to run an event in the script to then start the device. In the meantime I was going to try the other two options you gave me with CAPI to see if they work but I want to make sure the script is fully functional before I do the next step.

                        Comment


                          #13
                          I wanted to thank Michael and all other people dedicated to help people like me. Not sure if I proxy other like me but without you guys it will be very hard to accomplish such things in HS.

                          Comment


                            #14
                            I am beginning to begin to wrap my head around CAPI. Now, however, I seem to be reading that CAPI has been superseded by something else.Have I got that right?

                            An example *as I understand it" is that CAPIGetSingleControl has been superseded by another command, but I can't find it, again, in the morass of semi-documentation. This is SO confusing.

                            Any clarification would be greatly appreciated.
                            HomeSeer Version: HS3 Pro Edition 4.2.6.0
                            Operating System: Microsoft Windows 10 Pro - Desktop

                            Enabled Plug-Ins
                            AK Google Calendar 3.0.0.45,AK Smart Device 3.0.0.6,AK Weather 4.0.1.77,AmbientWeather 3.0.1.9,Big5 1.38.0.0,BLBackup 2.0.63.0,BLGData 3.0.55.0,BLLock 3.0.38.0,BLPlex 2.0.22.0,BLUPS 2.0.26.0,Device History 3.2.0.2,EasyTrigger 3.0.0.74,HSBuddy 3.25.614.1,mcsMQTT 5.21.4.1,MeiHarmonyHub 3.1.0.22,NetCAM 3.0.0.14,PHLocation 3.0.1.109,Restart 1.0.0.7,SDJ-Health 3.1.0.3,SDJ-VStat 3.1.0.7,TPLinkSmartHome 19.10.7.1,UltraCID3 3.0.6681.34300,UltraSighthoundVideo3 3.0.5960.36744,,Z-Wave 3.0.2.0

                            Comment

                            Working...
                            X