Rest API : invalid authentication

Hello,

I found this, wich seems to be what I need to match the status of my lights in ha with my home automation system : REST API | Home Assistant Developer Docs

So I created this :

  turn_lights_hallway_on:
    url: http://192.168.1.6:8123/api/states/light.lights_hallway
    method: POST
    headers:
      authorization: 'Bearer ABC'
      content-type: 'application/json'
    payload: '{"state":"on"}'

I replace ABC by a Long-Lived Access Token.

And do a rest command in my automation :

      - service: rest_command.turn_lights_hallway_on
        data: {}

But I get an error :

Logger: homeassistant.components.http.ban
Source: components/http/ban.py:84
Integration: HTTP (documentation, issues)
First occurred: 08:37:12 (1 occurrences)
Last logged: 08:37:12

Login attempt or request with invalid authentication from 192.168.1.6 (192.168.1.6). Requested URL: ‘/api/states/light.lights_hallway’. (HomeAssistant/2022.10.5 aiohttp/3.8.1 Python/3.10)

Logger: homeassistant.components.rest_command
Source: components/rest_command/init.py:137
Integration: RESTful Command (documentation, issues)
First occurred: 08:37:12 (1 occurrences)
Last logged: 08:37:12

Error. Url: http://192.168.1.6:8123/api/states/light.lights_hallway. Status code 401. Payload: b’{“state”:“on”}’

What did I wrong ?

You are using REST from your HA to the same HA, here, right?
I assume it’s for testing? If so, I would use another tool as the caller, e.g. curl or postman.

If not for testing, what are you trying to achieve?

Yes, indeed.

It is not for testing ! I have an old home automation system that controls all my lights and shutters (with physical push buttons which I don’t use offen anymore because my motion sensors are doing the job well). From HA I have the ability to pass commands via specific URLs to that home automation system, which works perfectly.

So this is a one-way street, so I don’t get any info from my home automation system back to HA.

In the first instance I now want to solve the following problem : when restarting HA my lights for HA are all off, while this is not necessarily the case. So I wrote an automation in HA that keeps track of the status of the lights. After restarting HA I want to restore that tracked state of the lights to HA (without effectively turning the light back on as they are already on).

Found it : seems I didn’t copy the whole token :crazy_face:

1 Like