Please show me how I can ping an ip address from my driver

I too learned from hard copy manuals. The search tool is your new friend, with the underlying expectation that you use it a lot (context tends to be minimal on any given page in today’s docs, compared to the olden days). Don’t get too attached to that though, the new thing is to use LLMs :rofl:

You can look at the sample code @sburke781 linked to above.

For kicks I asked Microsoft Copilot, whose answer happens to reference @thebearmay ’s code ! But don’t take this answer at face value, LLMs tend to hallucinate stuff…. (for example you don't need an API key and Groovy is the only language available on HE). The references are good though.

Happy holidays

Microsoft Copilot's answer

The ping method provided in the hubitat.helper.NetworkUtils package is a different way of testing the connectivity and performance of your devices by sending an ICMP ping request to the Hubitat Elevation runtime environment. The ping method returns a PingData object with various statistics, such as round-trip time (RTT), packet loss percentage, packets transmitted, packets received, and minimum and maximum values¹.

To use the ping method, you need to have a custom app or driver that can access the hubitat.helper.NetworkUtils package. You can use any programming language that supports accessing Hubitat APIs, such as Groovy, Python, Java, etc. You also need to have a valid API key from the Hubitat Elevation runtime environment that you can obtain from your account settings².

Here is an example of how to call the ping method using Groovy:

// Import the hubitat.helper.NetworkUtils package
import hubitat.helper.NetworkUtils

// Define the API key
def apiKey = "your_api_key_here"

// Define the IP address of the device you want to ping
def ipAddress = "your_ip_address_here"

// Define the count of pings (optional parameter)
def count = 5 // Optional parameter to specify how many pings to send

// Call the ping method and get a PingData object
def pingData = NetworkUtils.ping(ipAddress, count)

// Print some statistics from the PingData object
println("Ping result:")
println("RTT average: ${pingData.rttAvg}")
println("RTT min: ${pingData.rttMin}")
println("RTT max: ${pingData.rttMax}")
println("Packet loss percentage: ${pingData.packetLoss}%")
println("Packets transmitted: ${pingData.packetsTransmitted}")
println("Packets received: ${pingData.packetsReceived}")

I hope this helps you understand how to use the ping method provided in the hubitat.helper.NetworkUtils package. If you have any other questions, please feel free to ask me. :blush:

Source: Conversation with Bing, 12/24/2023
(1) [2.2.7] ICMP Ping for Apps and Drivers - Developers - Hubitat. [2.2.7] ICMP Ping for Apps and Drivers.
(2) [Release] Hubitat Ping - :gear: Custom Apps and Drivers - Hubitat. [Release] Hubitat Ping.
(3) Network Diagnostic via Ping - Feature Requests - Hubitat. Network Diagnostic via Ping.
(4) NetworkUtils Object | Hubitat Documentation. NetworkUtils Object | Hubitat Documentation.
(5) undefined. https://raw.githubusercontent.com/thebearmay/hubitat/main/hubPing.groovy.

2 Likes