Bing Chat and Groovy

So I got access to Bing Chat, and have been asking it some Hubitat questions... and color me impressed.

The references are great. Let me know if you want me to ask it anything.

Here are a few examples:

How do I use Rule Machine?

A:

Rule Machine is a powerful automation tool for Hubitat that allows you to create rules and triggers based on various conditions and events1. To use Rule Machine, you need to follow these steps2:

  1. Navigate to the hub interface, then select Apps in the sidebar.
  2. Select Rule Machine from the list. (Or install Rule Machine with Add Built-In App first if you have not already.)
  3. Select Create New Rule, then follow the prompts to provide a name for your rule.
  4. Specify triggers, actions, and other components you need to make your rule work as intended.

You can find more details and examples on how to use Rule Machine in the Hubitat documentation2 or community forum1.

Learn more:1. community.hubitat.com2. docs2.hubitat.com3. docs2.hubitat.com4. community.hubitat.com

+1 more

Q: Can you help me create a rule in rule machine that turns on a light when a door opens, and turns it off when it closes, but doesn't turn it off if the door was already open?

A:

Sure, I can try to help you with that. Here is a possible rule that you could create using Rule Machine1:

  • Name: Light on when door opens
  • Trigger events: Door contact changed
  • Actions:
    • IF (Door contact open) THEN
      • Turn on light
      • Set Private Boolean True
    • ELSE IF (Door contact closed AND Private Boolean is True) THEN
      • Turn off light
      • Set Private Boolean False
    • END-IF

This rule uses a Private Boolean variable to keep track of whether the light was turned on by the door opening or not. If the door opens, it turns on the light and sets the Private Boolean to True. If the door closes and the Private Boolean is True, it means that the light was turned on by the door opening, so it turns off the light and sets the Private Boolean to False. If the door closes and the Private Boolean is False, it means that the light was already on before the door opened, so it does nothing.

You can find more examples and tips on how to use Rule Machine in this forum thread2.

Learn more:1. community.hubitat.com2. community.hubitat.com3. community.hubitat.com4. community.hubitat.com5. docs.hubitat.com

+2 more

4 Likes

I asked ChatGPT some stuff too:

Can you write some code in groovy for an app that will turn on lights at sunset and turn them off at sunrise?

Sure! Here's an example of a Hubitat app written in Groovy that turns on lights at sunset and turns them off at sunrise:

definition(
    name: "Sunset / Sunrise Lighting",
    namespace: "myapp",
    author: "Your Name",
    description: "Turn on lights at sunset and turn them off at sunrise.",
    category: "Convenience",
    iconUrl: "https://www.example.com/icon.png",
    iconX2Url: "https://www.example.com/icon@2x.png"
)

preferences {
    section("Sunset / Sunrise Lighting") {
        input "lights", "capability.switch", title: "Which lights?", required: true, multiple: true
        input "offset", "number", title: "Offset from sunset / sunrise (minutes)", required: true, defaultValue: 0
    }
}

def installed() {
    log.debug "Installed with settings: ${settings}"
    scheduleSunset()
    scheduleSunrise()
}

def updated() {
    log.debug "Updated with settings: ${settings}"
    unschedule()
    scheduleSunset()
    scheduleSunrise()
}

def scheduleSunset() {
    unschedule("sunsetHandler")
    def sunsetTime = location.sunset(timeZone).plusMinutes(offset)
    log.debug "Scheduling sunset at ${sunsetTime}"
    runDailyAtTime(sunsetTime, "sunsetHandler")
}

def scheduleSunrise() {
    unschedule("sunriseHandler")
    def sunriseTime = location.sunrise(timeZone).plusMinutes(offset)
    log.debug "Scheduling sunrise at ${sunriseTime}"
    runDailyAtTime(sunriseTime, "sunriseHandler")
}

def sunsetHandler() {
    log.debug "Turning on lights"
    lights.on()
}

def sunriseHandler() {
    log.debug "Turning off lights"
    lights.off()
}

This app uses the location.sunset() and location.sunrise() methods to determine the sunset and sunrise times for the Hubitat location, and then schedules events to turn the lights on and off at those times. The offset preference allows the user to adjust the time by a specified number of minutes.

6 Likes

That is awesome. Here is what Bing sent back, references to existing apps instead

I see. You want me to write some groovy code for a Hubitat app that will turn on lights at sunset and turn them off at sunrise.
One possible way to do that is to use a Simple Automation Rules app12 that can create rules based on time triggers such as sunrise and sunset. You can install this app from the Hubitat built-in apps list and configure it according to your needs.

Another possible way is to use a Circadian Daylight app3 that can adjust your lights based on the natural daylight cycle. You can install this app by using Hubitat Package Manager or manually copying and pasting the code from GitHub3.

A third possible way is to use a Turn On Before Sunset app4 that can turn on your lights before sunset and turn them off after sunrise. You can copy and paste the code from GitHub4 and modify it according to your needs.

I hope this helps you with your code. Do you have any other questions?

2 Likes

I asked ChatGPT something similar although I didn't actually test the code it generated. Even if it doesn't work as-is, the hard part is knowing what to do (and what it's called). I can figure out "how to do it" from examples and trial and error. So both of these answers are valuable and useful. I might just have to try groovy programming now rather than just writing rules.

1 Like

Careful, soon you'll have a stack of niche apps that you feel obligated to support for the 2-3 other people who use them. :wink:

More seriously, this is a great way to use these AI tools. Some other things that can be handy:

  • Explain how the following code works
  • Convert the following code to Groovy
4 Likes

Good point. ATM, the only one I'm considering is a combined washer/dryer handler - where it can remind me to "handle the laundry" when both the washer and the dryer are off (and at least one of them had been on), and then remind me every few minutes until I open the dryer door. It will use two current sensors in the breaker box, and a contact sensor for the door. It might be possible to do this with RuleEngine, but if I can write it in Delphi (Pascal), I should be able to get ChatGPT to translate it to groovy for me.

Wow, here comes more unemployed people...SMH.
Do you have to be a dev or granted access to use this, or just a top secret URL?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.