Changing states of devices through external Websocket API JS client

In the Docs, it is said:

If you overwrite a state via the states dev tool or the API, it will not impact the actual device. If the device state is being polled, it will overwrite the state in the state machine the next polling.

So, how can I exactly change the values of devices’ states (on/off) and attributes (e.g. brightness or rgb on a WiFi LED Bulb) when I get triggered through a Websocket client (like this one) remotely (by a Firebase change state function in the same websocket client in my case)?

Sorry if it’s a dumb question. I’ve spent days searching in forums and still no answer. :frowning:

You need to invoke a service via the REST API as documented here.

1 Like

Thank you! Didn’t notice that you could change the color, brightness of the LED by using the turn_on service. Thanks!! :smile:

Don’t have to call REST API, you can call a service by websocket API see doc

1 Like

I read it but I can’t figure out how exactly implement it, I’ve tried:

// Function that gets triggered on a Firebase DB change
    var actuatorChanges = database.ref('actuador/a2');
    actuatorChanges.on('child_changed', function(data) {
        
        // Tried to apply this, but the socket closes immediately
        // I'm also sure data.val().brightness returns a number and not a string
        ws.send('{"id": 24,"type": "call_service","domain": "light","service": "turn_on", //Opt "service_data": {"entity_id": "light.yeelight_rgb_id", "brightness": '+data.val().brightness+'}}\n');
    });

Calling the REST API works great, but I have to do a HTTP request with JQuery. It’d be wonderful if I could use the Websocket to call the service.

remove this, it will invalid JSON format.

Thanks! That was a “// Optional” comment in the docs example. Should aclare that.

Did HA dropped suport for calling a service by websocket API?
The link https://developers.home-assistant.io/docs/en/external_api_websocket.html#calling-a-service is dead and i’m not able to find anything similar on the page :confused:

I know this is late, but I accidently came across your message. I don’t think support is dropped, but I have not used it: