Calculations in a script

In Homeseer, the scripting language was basically visual basic, so I could do pretty much any calculations, for example, I had a script that did different things based on the day of the week. I don’t see how I can do generic kinds of calculations I see that I can create a python script and expose it as an action, but I need the underlying HA script to change behavior. Can I pass a result back from python, or control devices from within the python script?

Hi Dan, it’s much easier to help if you explain what you want to achieve.

Hello ,
To actually help you it would be good to know what you are trying to accomplish.
How to help us help you - or How to ask a good question.

1 Like

As I mentioned above, I currently have a script that determines the day of the week, then does something different based on the day (I also have scripts that query the state of a device and do different things, but I might be able to accomplish that with multiple scripts and conditions):

Sub Main(sVal as string)
Dim count as integer
Dim numDate, val, div as long
Dim ival as integer
Dim sLabels() as string = {"SAM", "Party", "Romance", "Caribbean", "American", "California", "Royal", "Blue","Green","Red","White","Magenta","Hold","Recall"}


numDate = Now().toString("dd")
div = 14
Math.DivRem(numDate, div, val)
ival = cint(val) + 1
hs.WriteLog("Pool Light Control",  sLabels(iVal))


DeviceAction("Pool Light","On")
for count = 0 to ival - 1
	DeviceAction("Pool Light","Off")
	DeviceAction("Pool Light","On")
next


End Sub

Sub DeviceAction(Device As String, Level As String)
   Dim objCAPIControl As CAPIControl
   Dim DvRef As Integer = hs.GetDeviceRefByName(Device)
   If IsNumeric(Level) Then
      If Level < 0 Then Level = 0
         hs.CAPIControlHandler(hs.CAPIGetSingleControl(DVRef,false, Level, false, True))
   Else
      hs.CAPIControlHandler(hs.CAPIGetSingleControl(DVRef, True,Level.ToUpper,False,False))
   End If
End Sub

The most common method is going to be to use Jinja Templating within your automations and scripts:

But there are other options:

Python Scripts integration
Node-Red Add-on
Appdaemon Add-on

So depending on the date you have a different color in the pool?
That’s not very hard to do.

What is the entity you need to control

Dan, as I (and @Sir_Goodenough) already wrote: by telling what you want to achieve you might get answers with solutions and apparently in your case, even a better/easier one.

I would think the script and descriptions above would be sufficient. I want to calculate the day of the week, and depending on that day control a device in a different way (in my case, modify the number of on/off commands I send). If you could tell me what other information you need, I’d be glad to supply it.

With respect to templating, looking at the supplied links, I don’t see where they actually control a device. I see a lot about getting the state of the device, but not actually controlling it. Am I missing something or would I need to create some variable that is set in the template that is actually used elsewhere in the script?

No you are correct.
Templates is more or less just for logic and calculations, you then need the output of this to do the work, meaning service calls/actions.
Most fields can accept templates, so for your case you need to use the repeat action.

actions:
  - action: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.pool
  - repeat:
      count: "{{ (now().timestamp() | timestamp_custom('%d')) | int % 14 }}"
      sequence:
        - action: switch.turn_off
          metadata: {}
          data: {}
          target:
            entity_id: switch.pool
        - action: switch.turn_on
          metadata: {}
          data: {}
          target:
            entity_id: switch.pool

You probably need to add a delay between the on and off but you need to test what is needed.