0

I have an input_number configured in HomeAssistant like this in the configuration.yalm:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

input_number:
  box1:
    name: some_number
    min: 0
    max: 100
    step: 1
    mode: box
    unit_of_measurement: "%"

And I would like to get that value from a NodeMCU board with MicroPython. And I'm trying to get that value from the REPL. First I connect to the network:

>>> import network
>>> ssid = "ssid"
>>> password = "password"
>>> sta_if = network.WLAN(network.STA_IF)
>>> ap_if = network.WLAN(network.AP_IF)
>>> sta_if.active(True)
>>> sta_if.connect(ssid, password)

And after I few seconds I check if sta_if.isconnected() returns True. Then I try to get the value from the API with:

>>> import urequests
>>> url = "http://homeassistant.local:8123/api/states/input_number.box1"
>>> token="token"
>>> headers = {"Authorization": f"Bearer {token}", "content-type": "application/json"}
>>> response = urequests.get(url, headers=headers)

But I get the following error:

>>> response = urequests.get(url, headers=headers)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "urequests.py", line 116, in get
  File "urequests.py", line 55, in request
OSError: -2
>>> 

To create the token I went to homeassistant web > Profile > Long-Lived Access Tokens > Create Token. No more.

Any idea?

Thank you a lot!

Alexgar
  • 21
  • 3
  • Can you establish a connection to a random public domain name `e.g. google.com`? – Oluwafemi Sule Mar 03 '23 at 20:03
  • 1
    It looks like you're trying to connecto `homeassistant.local`. Domains name ending in `.local` typically mean you're using multicast-DNS (mDNS), which isn't supported by micropython. What happens if you replace `homeassistant.local` with the actual ip address of your server? Can you register an appropriate hostname with your local DNS server? – larsks Mar 04 '23 at 01:28
  • @larsks Even though I didn't understand half of what you said, I replaced 'homeassistant.local' with the ip address of the virtual machine and it worked perfectly. Thank you very much! – Alexgar Mar 04 '23 at 09:06

0 Answers0