tomaae / homeassistant-mikrotik_router

Mikrotik router integration for Home Assistant
Apache License 2.0
292 stars 48 forks source link

WAN IP[Question] #308

Closed sygys closed 9 months ago

sygys commented 10 months ago

Is there a way to get the WAN ip in home assistant? I have an automation to check if the connection is lost or when ha reboots and send me the latest ip address by telegram when connection is restored. In the Fritzbox integration there was a way to get this ip. I can't find it in the mikrotik integration. Is there a way?

tomaae commented 10 months ago

that really depends on your network setup. it could be under interface you use for outside traffic. you could always make a script on your router to write current it into envinroment variables. simple automation in HA that detects the value change and do whatever you need to do with it. that would be network setup independent and reliable.

Extrapilot1 commented 10 months ago

You could use an existing integration- https://www.home-assistant.io/integrations/dnsip/

Another option would be to enable the IP/Cloud functionality (which is MT's native dynamic DNS service-free), which tracks your external IP as a property you can query with a script as:

:local cloud [/ip cloud get public-address];

You would need to determine how you want to handle this- it could behave as a function just returning text for example, that you parse as a template sensor, and have the Script option in the Integration enabled so you can trigger that as a switch.

It would probably be better to have the integration here do the lookup- where if Cloud was enabled, this value would be returned, since there could be more flexibility if there were multiple links, or if you wanted to have HA updated if the external IP changed etc.

tomaae commented 10 months ago

I mean, maybe? I will have to look up what /ip cloud is even about.

sygys commented 10 months ago

Many thanks so far guys. I appreciate all the options. I was looking for it in the mikrotik integration and never thought about an option to let another integration check the IP and send it. I guess it's even allot easier this way. I'm no mikrotik pro so writing scripts and stuff isn't really an option for me. It also isn't a big problem though I can try to get the IP from another integration. I do however have some problems with entities doing nothing in the integration and other entities that are becoming unavailable. But I think that's something for another time (thread)

tomaae commented 10 months ago

certainly, if there is a problem you notice, let me know. I'm transitioning my integration to new model to take advantage of all new HA features, so there may be some issues.

github-actions[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

github-actions[bot] commented 9 months ago

This issue was closed because it has been stalled for 5 days with no activity.

TV456 commented 9 months ago

@sygys It is certainly possible to create a script for the WanIP in the environment. @tomaae is right in this one. When the environment variable is set, the MikroTik Router Integration automatically picks it up. He is also right when it depends on the set up you have. In my case I have the MikroTik Router as a switch connected on the ISP browser. I my case the script is quite easily applied. In routerOS go to System -> Script, than add a script. The script I use to set the environmental variable wanIP:

:global wanIP; :set wanIP [/ip address get [/ip address find interface="....."] address;

On the dots of interface enter the name of the interface which is connected to your ISP. Then test the script by clicking Run Script. In the tab Environment you should no see the declare variable with the IP-address as it's value. If did not work, locate the desired variable and test reading this variable in the terminal using the MikroTik scripting.

Then it helps to set a schedule to run the script at the desired interval. Go to System -> Scheduler and add a schedule. Simple add in the On Event section:

/system script run ...

Enter the name of your scripts on the dots. You can set the start time on startup and the interval. More on MikroTik scheduling. Hope it helps.

sygys commented 9 months ago

Many thanks for this answer I will test this out. This would be awsome

TV456 commented 9 months ago

Many thanks for this answer I will test this out. This would be awsome

When testing I helped me to run the commands from the terminal in routerOS by using :put before it. The terminal retrieves the respectable values and prints in the screen. This way you will know if your command works. I.e.: :put [/ip address get [/ ip address find interface="...."] address ]

While working on my reply to you I tweaked my comment to get the address not from the IP Address List, but directly from the IP DCHP-client list. You can use the following command: [/ip dhcp-client get [/ip dhcp-client find interface="....."] address ]