How to store variable value between 2 rules execution (CRON)?

Hello,
I started writing a CRON event based rule to handle my dynamic DNS change at home. Basically, few simple steps

  1. Get current external IP
  2. Check previous IP value to see if changes occured
  3. If any, call API with sendHttpPostRequest to update new IP on DDNS provider.

I’m stuck on step 2 : how can I store variable value on rule execution N and retrieve it on execution N+1 to compare both? I saw things to store Item values, but I wonder if I can do the same with variables.

Thanks for your replies !

Nicolas

Is there some reason you can’t have an Item (string perhaps) and update your value to it? It’s just there then, no further action needed.
If you persist that Item, it will survive across OH reboots too.

No reason at all, but the fact I’m a beginner and didn’t even notice there was a text Item.
Thank you for your suggestion, I’m sure that will do!
I let you know.

Cheers,

Nicolas

That did it !
Thank you again

Code if any other beginners interested (in dynamicDNS.rules) :

/* Check IP using external service */
rule "Dynamic DNS check"
    when
    	Time cron "0 0/3 * 1/1 * ? *"
    then
    	// Initiates variables
    	var String checkIpUrl = "http://ipecho.net/plain"
    	// Get current external IP
    	var ip = sendHttpGetRequest(checkIpUrl)

    	// compare last IP and new
    	if(wanIpAddress.state.toString != ip) {
    	   logInfo("ddns.status", "WAN IP changed : " + wanIpAddress.state.toString + " (previous) to " + ip + " (new)")
    	   postUpdate(wanIpAddress, ip)	
    	}    
end

/* Update DDNS IP entry to API */
rule "Dynamic DNS update"
    when
    	Item wanIpAddress received update
    then
    	var String hostname = "hostname"
    	var String username = "username"
    	var String password = "pass"
	
        // Update IP
    	var apiReturn = sendHttpGetRequest("https://api.dynu.com/nic/update?hostname=" + hostname + "&myip=" + wanIpAddress.state.toString + "&username=" + username + "&password=" + password)
    	logInfo("ddns.update", "API return : " + apiReturn)    	    
end
1 Like

Could you please post the definition of that String Item?

Thx,
Ferdinand