Get states for single entity via websocket API

I am able to fetch the states of a single entity using the REST API using http://homeassistant.local:8123/api/states/light.kitchen. Is it possible to do the same thing via the WebSocket API? I have tried a number of different things, however, it hasn’t worked. I do not want to get the states for all entities at once.

2 Likes

I ran into the same issue because my receiver cannot handle large messages. Did you find some solution?

Best regards
Patrick

@patman Unfortunately, no.

I just had a look at the source, I think patching the API so that it accepts a parameter with wildcards should not be too difficult. Probably I give it a try on the weekend.

2 Likes

So… how’d it go? Any luck?

1 Like

Sorry, I learned that I need to setup a development environment first, before I can patch the few lines of code. I didn’t try that yet due to lack of time. :frowning: Need to find some …

1 Like

Looking at the source code of version 2023.11.2 in /components/websocket_api/commands.py this is possible.
There is a (currently) undocumented websocket command of ‘subscribe_entities’.
I’ve tested with the following websocket command it provides state updates for a limited set of entities.

{ “id”: 18,
“type”: “subscribe_entities”,
“entity_ids”: [“switch.test_lamp”]
}

2 Likes

That is some great news, hopefully documented or officially released soon. Much better than (also) receiving updated from all 8 power plugs and what not. I’ll check this today. Thanks for letting us know!

Hmm… it looks like it has been there for some time now :sweat_smile: (Mar 12, 2022)

I’ve checked it out. I would be nice if it will get documented soon, as the responses are not fully clear yet.

Tested the normal subscribe_events and the reported subscribe_entities and monitored it for one minute.

Method Messages/min Data usage/min
subscribe_events 584 592 kB
subscribe_entities 10 1.2 kB

58 times less messages, 493 times less data

Initial response:

{
  "id": 1,
  "type": "event",
  "event": {
    "a": {
      "sensor.neocoolcam7_energy_power": {
        "s": "79",
        "a": {
          "unit_of_measurement": "W",
          "device_class": "power",
          "friendly_name": "NeoCoolcam7 NeoCoolcam7 ENERGY Power"
        },
        "c": "01HF3ZMR6SPFK6Z6YA7W184ADG",
        "lc": 1699867156.697465
      }
    }
  }
}

Updates:

{
  "type": "event",
  "event": {
    "c": {
      "sensor.neocoolcam7_energy_power": {
        "+": {
          "s": "75",
          "lc": 1699867166.740093,
          "c": "01HF3ZN20M0WRXTSQW7NZWYWWF"
        }
      }
    }
  },
  "id": 1
}
1 Like