home-assistant / home-assistant-js-websocket

:aerial_tramway: JavaScript websocket client for Home Assistant

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to receive a result message on a subscribtion

Edwin-Luijten opened this issue · comments

I want to retrieve stats from a sensor, and am using below piece of code:

connection.subscribeMessage((msg: GridStats) => {
    console.log('stats data', msg);
}, {
    type: 'recorder/statistics_during_period',
    period: '5minute',
    start_time: (dayjs().subtract(12, 'h')).toISOString(),
    statistic_ids: [id],
    types: ['state', 'min', 'mean', 'max'],
});

When checking the messages returned in the network tab, I do see a result message with a corresponding id.
image

Tough, the callback is not triggered.

I also use a subscription for Browsermod and there it does work:

connection.subscribeMessage((msg: BrowserModCommand) => {
    console.log('browser mod', msg);
}, {
    type: 'browser_mod/connect',
    browserID: 'panel',
});

Any hints on how to track down why it's not working?

Subscriptions don’t get data from results. The result is just to confirm the subscription is in place. Subscriptions receive data from events which are forwarded to the callback.

Ah, ok, so is there a method available that allows us to do this, or do we need to build our own, or better use the rest api?

Just realized that the sendMessagePromise would suffice.