How to Use hubitat.device.HubAction from an Automation

I want to send a one byte UDP message from an automation rule. There will be no response. I have found some code that should work but I have never created an application or driver. Is there a template with instructions and an example I can follow?

Thanks!

Just have AI write you a driver that does it, and create a virtual device with it. Tell it to add a send command that you can use from in automation apps like RM or Webcore to send it on demand.

While it is worthwhile to learn how to write them from scratch, for this case I don't think it is necessary. You can learn from the example of the code it gives you.

This is what it gave me.

/**
 * Single Byte UDP Sender Driver for Hubitat Elevation
 *
 * Description: Sends a single-byte UDP packet to a configurable IP and Port.
 * 
 * Instructions:
 * 1. Go to "Drivers Code" in your Hubitat Hub, click "New Driver", paste this code, and click "Save".
 * 2. Go to "Devices", click "Add Virtual Device", give it a name, and select "Single Byte UDP Sender" as the Type.
 * 3. Open the newly created device, configure the IP, Port, and Hex Byte preferences, and click "Save Preferences".
 * 4. Click the "Send UDP Command" button to test.
 */

metadata {
    definition (
        name: "Single Byte UDP Sender",
        namespace: "custom",
        author: "AI Hubitat Developer"
    ) {
        capability "Actuator"

        // Exposes a button in the UI to trigger the message
        command "sendUdpCommand"
    }

    preferences {
        input name: "ipAddress", type: "text", title: "Target IP Address", description: "e.g., 192.168.1.50", required: true
        input name: "ipPort", type: "number", title: "Target Port", description: "e.g., 8080", range: "1..65535", required: true
        input name: "hexByte", type: "text", title: "Hex Byte to Send", description: "A single byte in hex format (e.g., 01, FF, A5)", defaultValue: "01", required: true
    }
}

// Log management helper
def logsOff() {
    log.info "Debug logging disabled."
    device.updateSetting("logEnable", [value: "false", type: "bool"])
}

// Triggered when preferences are saved
def updated() {
    log.info "Updating settings..."
    log.info "Target Address: ${ipAddress}:${ipPort}"
    log.info "Configured Hex Byte: ${hexByte}"
}

// Main command execution
def sendUdpCommand() {
    // Validate inputs
    if (!ipAddress || !ipPort || !hexByte) {
        log.error "Missing required preferences! Please check IP, Port, and Hex Byte configuration."
        return
    }

    // Clean up the hex string input (remove spaces or '0x' prefix if user typed them)
    String cleanHex = hexByte.replaceAll(/\s+/, "").replace("0x", "")
    
    if (cleanHex.length() != 2) {
        log.error "Invalid input: '${hexByte}'. 'Hex Byte to Send' must be exactly a single byte (2 hex characters, e.g., 'FF')."
        return
    }

    try {
        log.info "Sending UDP payload '0x${cleanHex}' to ${ipAddress}:${ipPort}"
        
        // Hubitat sends LAN messages using a HubAction object
        // The payload must be a raw hex string for standard network packets
        def myHubAction = new hubitat.device.HubAction(
            cleanHex,
            hubitat.device.Protocol.LAN,
            [
                type: hubitat.device.HubAction.Type.LAN_TYPE_UDPCLIENT,
                destinationAddress: "${ipAddress}:${hubitat.helper.HexUtils.integerToHexString(ipPort.toInteger(), 2)}"
            ]
        )
        
        sendHubCommand(myHubAction)
        
    } catch (e) {
        log.error "Failed to send UDP message: ${e.message}"
    }
}