[SOLVED] Hostnames inplace of IP Address is Rules, Services, & Things

I’m fairly new to OpenHAB and have copied most of my Radio Thermostat CT50 code from others. That being said I’ve noticed some hardcoded IP addresses in a number of places, particularly in Rules, Services, and Things.

I’ve been unsuccessful in my attempts to replace these IP addresses with Hostnames. For instance, in my Rules file I have a link that looks like this

val output = sendHttpPostRequest("http://172.27.4.2/tstat/fmode", "application/json", '{"fmode":' + receivedCommand + '}')

I’d rather it look something like this

val output = sendHttpPostRequest("http://RA_CT50_Dn/tstat/fmode", "application/json", '{"fmode":' + receivedCommand + '}')

However, doing that throws an error in /var/log/openhab2/openhab.log

2018-11-19 15:22:26.042 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Set Fan': Invalid URI host: null (authority: RA_CT50_Dn)

Any way to get rid of the hardcoded IP addresses?

Edit your /etc/hosts file
Google it before your do anything stupid with it

This is mine:

127.0.0.1       localhost
127.0.1.1       home2
192.168.0.12    openhab
192.168.0.14    pitv
192.168.0.30    camera01
192.168.0.34    pizero
192.168.0.1     router
192.168.0.4     milights
192.168.0.36    printer
192.168.0.15    largebedroom_radiatorvalve
192.168.0.3     masterbedroom_hotblanket
192.168.0.181   sonoff4ch
192.168.0.200   btrouter

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

@vzorglub thanks for the idea but I don’t think that is addressing the issue I’m referring to.

I have IP addresses assigned dynamically by my router and it acts as a DNS server. I am able to successfully ping RA_CT50_Dn from the server

user@server:~$ ping RA_CT50_Dn
PING RA_CT50_Dn.blahblah.lan (172.27.4.2) 56(84) bytes of data.
64 bytes from RA_CT50_Dn.blahblah.lan (172.27.4.2): icmp_seq=1 ttl=255 time=1.40 ms

And successfully curl RA_CT50_Dn from the server

user@server:~$ curl http://RA_CT50_Dn/tstat
{"temp":68.50,"tmode":1,"fmode":0,"override":1,"hold":1,"t_heat":68.00,"tstate":0,"fstate":0,"time":{"day":0,"hour":19,"minute":48},"t_type_post":0}

However the Sitemap doesn’t work and the log shows the error I listed earlier when I replace the IP address with the hostname in my Rules file. So I think the issue lies within OpenHAB (or the way I’m coding things within OpenHAB) and not on the greater Linux Server.

you may have found a bug

try the following:

val output = sendHttpPostRequest("http://RA_CT50_Dn.blahblah.lan/tstat/fmode", "application/json", '{"fmode":' + receivedCommand + '}')

I’m using local hosts in my sendHttpPostRequest calls, and they work fine. For example:

val String response = sendHttpGetRequest("http://webswitch.my.domain.com/state.xml?relay1State=2")

The only difference I see is that I’m using fully qualified names (host.my.domain.com), not simple host names. I don’t know if your setup allows you the ability to use a fully qualified name or not, but that might be worth a try.

I also would suggest trying @vzorglub’s suggestion as a way to demonstrate that it will work using a locally defined host.

@Dim & @mhilbush: the fully qualified name didn’t help. Same error (but with the fully qualified name)

2018-11-19 22:13:56.135 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Set Fan': Invalid URI host: null (authority: RA_CT50_Dn.blahblah.lan)

@mhilbush & @vzorglub: I tried adding references to my /etc/hosts file. This didn’t work for

172.27.4.2 RA_CT50_dn

or

172.27.4.2 RA_CT50_dn.blahblah.lan

Same errors (with our without the .blahblah.lan) as before.

I wonder why your systems can operate off of hostnames and mine can’t (even though the underlying Linux understands the hostnames and other services like ping and curl can).

Really strange.

The only other difference I see is that your hostname has underscores. Really, this is a stretch, but I’m stumped why it’s not working for you.

Maybe put HttpUtil into debug mode to shed some additional light on what’s happening.

I think it’s in org.eclipse.smarthome.io.net

So in the console

log:set DEBUG org.eclipse.smarthome.io.net

Wikipedia says

While a hostname may not contain other characters, such as the underscore character ( _ ), other DNS names may contain the underscore.

2 Likes

You win the prize! That was it. I never knew of the restriction to a-z, A-Z, 0-9, and - (minus sign). After updating the hostnames on the router and changing the rule to

val output = sendHttpPostRequest("http://RA-CT50-Dn/tstat/fmode", "application/json", '{"fmode":' + receivedCommand + '}')

It worked.

Thanks for your help @mhilbush

Sure thing. I was not aware of the underscore issue either. Especially since it can be used in a domain name, but not a hostname!

Could you please mark the post above as the solution? That way it will be clear when someone else runs into the same issue.