Set hub variable from driver?

Hey, all. I'm trying to write Rule Machine rules to do things based on an HTTP call to an external API. Either the Rule Machine rule will trigger at a time specified in the HTTP response, or some other Rule Machine rule will run conditionally on something in the HTTP response being "true". But I think the parsing and logic are a bit too complex to do it all in Rule Machine.

Here's what I've planned so far:

  • A custom driver that uses httpGet to call the API, parse its response, and assign the values to attributes of the virtual device using this driver
  • Hub variables representing the key parts of the HTTP response
  • Rule Machine stuff such as a Certain Time trigger based on Variable Time

The step that I haven't been able to figure out is how to get the info from the virtual device driver to somewhere where Rule Machine can use it in a trigger. Hub variables seem like the right place, so I guess my first question is "How do I use a driver to assign a value to a hub variable"?

But then my followup questions are "Is there a better way to do all this" and "Can Rule Machine look at a custom attribute of an arbitrary device to use as a time trigger and/or a conditional action condition" and "Should I be writing a custom app rather than a custom device driver"?

Thanks!

success=this.setGlobalVar("${varName}", "${varValue})

but RM should be able to do all but the most complex logic…

1 Like

Not sure to fully understand what you are trying to do, but yes, RM can be triggered from device custom attributes. When defining the trigger, simply choose custom attribute. I don't think you need to use global variables.

2 Likes

Equally, you do it all back in the driver as well with a scheduled job

EDIT - Actually, that depends what the rule does, but you could at least handle the scheduling of the trigger that way.

2 Likes

That works for "Trigger when this custom attribute changes in this way", but I don't think you can do it for Certain Time triggers, can you? "Attribute foo of device bar stores a datetime. When the current time is equal to that value, run this rule." I see how to do that with hub variables, but not custom attributes.

Ok, now I think I kind of understand what you are trying to do.

What may be easier to do, is to create an attribute that will become true at the time specified in your HTTP response using the runIn function in your custom driver.

With that custom attribute, you will be able to trigger the RM.

2 Likes

Oh, that's cool, I didn't know about that function, thank you! I think this gives me less flexibility and less abstraction than I want for this case (I really like rule machine's ability to set an offset, like, do something 20 minutes before sunset or before a variable time), but it's really good to know about and I may find some way to incorporate it into my project anyway.

Thank you all for your help! I have my answer. Read on if you want a little more info on what I'm doing, and I'm happy to hear more feedback if you like:

I'm Jewish, and I'm trying to make my Hubitat more aware of Jewish sabbath (Shabbat) and holiday observance. Getting 90% of the way there isn't hard: Shabbat begins at sundown Friday and ends about an hour after sundown Saturday. I have plenty of mode changes and rules based on these times. But I want to upgrade in 2 ways: (1) Be more precise about the end time, which varies based on date and location, and (2) Be aware of the holidays and treat them the same as Shabbat. And I want to keep the time logic and the rules separate so I can share the former. I've already, at some point in the past, written a web API that returns the relevant info.

So what I'm going to make is:

  • A custom device driver for a momentary switch that, when pushed, retrieves the info for that day based on hub latitude and longitude.
  • 3 virtual child devices produced by the driver. One will be turned on if it is Shabbat or a holiday that day (prior to sundown). One will be turned on if it is Shabbat or a holiday that night (1+ hours after sundown). And one will be turned on or off based on whether it is Shabbat or a holiday at that moment.
  • A parameter for the driver where a user can optionally specify the name of a Hub variable which will have that night's sunset time assigned to it, but only if Shabbat starts that day. Similar for Shabbat end time.
  • A Simple Automation Rule (edit: or a schedule within the device driver code) that pushes the button every morning a bit after midnight.
  • Whatever other RM rules and mode manager stuff I want.

I will share the device driver when I'm done if I'm happy with it!

I remember a conversation from a while ago now on someone else looking at automations throughout Shabbat. I naively weighed in early and then learnt a few things along the way :slight_smile: I remember @aaiyar being more knowledgeable than me on the topic. I'll see if I can track down the thread. It may not necessarily play into the technical question you raised, but may provide some other ideas.

2 Likes

@sburke781 linked to the appropriate thread, and I encourage you to go through it.

The answer might be for you to program all your automations using Rule Machine. And then use a Predicate Condition (now called Required Expression) to prevent any automations from running during the desired time period.

2 Likes

Thank you! Everyone's observance is a bit different, and my needs are a bit different than described in that thread. I don't care if a motion sensor sees me on Shabbat (as long as no rules actually get triggered from it), but I don't think I have a way currently to handle holidays the same as Shabbat.

2 Likes