I have two three wan providers (with more or less dynamic IPs) and one item which is called Network_WAN_IP (from How to monitor a dynamic WAN IP address) that is getting frequently updated. I would like to get the first three digits of the Network_WAN_IP into a new item. With the first three digits of the IP address I could map the ISP to it in the rules.
My question is how can I use regex or json transformation to get the first three digits of the IP address into a new item i.e. Network_WAN_IP_short?
i.e. Network_WAN_IP is 194.45.253.2, Network_WAN_IP_short would be 194. ISP would be “Sunrise” then
i.e. Network_WAN_IP is 77.25.25.1, Network_WAN_IP_short would be 77. ISP would be “upc” then
Is that a String type?
All you would need is a rule that triggered on that Item changing.
The rule could manipulate the string state, just a split() should do.
Then postupdate your target Item.
A MAP transform in the target label could convert 123 to ISPNAME for display.
Hello, Network_WAN_IP defind as string item I try now this one with split() and get() but it just returns a rule error zero. Please advise with any advise?
rule “ISP”
when
Item Network_WAN_IP changed
then
// perform any action needed
val ISP = Network_WAN_IP.state.toString.split(".").get(0)
if (ISP==“141”) {
ISP_Name.postUpdate(“PrivateLayer”)
} else {
ISP_Name.postUpdate(“hahaha”)
}
end
Thanks well spotted! I still get the error message:
2020-02-02 19:08:43.293 [vent.ItemStateChangedEvent] - Network_WAN_IP changed from 77.59.255.33 to 141.255.25.33
==> /var/log/openhab2/openhab.log <==
2020-02-02 19:08:43.692 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Assign ISP Name based on first three IP digits’: 0
Please advise with any thoughts. Is something with the string definition wrong? somehow the .split(".").get(0) does not return the first three digits of the Network_WAN_IP. If I change it to .split(" ").get(0), then the function returns the full IP address which is not helpful…
rule "Assign ISP Name based on first three IP digits"
when
Item Network_WAN_IP changed
then
val IP_short = Network_WAN_IP.state.toString.split(".").get(0)
if (IP_short=="141") {
ISP_name.postUpdate("PrivateLayer VPN")
logInfo("network.rules","141 - Private Layer - IP Short is" + IP_short)
}
else if (IP_short=="77") {
ISP_name.postUpdate("UPC")
logInfo("network.rules","77 - UPC - IP Short is" + IP_short)
}
else if(IP_short=="194") {
ISP_name.postUpdate("Sunrise")
logInfo("network.rules","194 - Sunrise - IP Short is" + IP_short)
}
else {
ISP_name.postUpdate("Unknown")
logInfo("network.rules","Unknown - IP Short is" + IP_short)
}
end