Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createState()/createStateAsync() issue, if 2nd param = initial value, and 3rd param = common obj #924

Closed
Acgua opened this issue Dec 15, 2021 · 1 comment

Comments

@Acgua
Copy link

Acgua commented Dec 15, 2021

Describe the bug

Following script from user drefree is returning "not found" in console, while documentation seems to allow an initial value as second parameter, and the common object as third parameter.

createState('javascript.0.variables.test', 'test init', {name: 'test Variable'}, function(){
    console.log(getState('javascript.0.variables.test').val)
})

When providing initial value as "def" in common, callback and async/await is working fine without errors:

const state123 = '0_userdata.0.test123';
createState(state123, {name:'test variable', def:'test init', type:'string', read:true, write:true, role:'state' }, (obj) => {
    log(`Value of ${state123}: '${getState(state123).val}'`);
})
 
// Und noch Test per async/await
test();
async function test() {
 
    try {
 
        const state456 = '0_userdata.0.test456';
        await createStateAsync(state456, {name:'test variable', type:'string', read:true, write:true, role:'state', def:'test init' });
        const r = await getStateAsync(state456);
        log(`Value of ${state456}: '${r.val}'`);
 
    } catch(error) {
        log(error.stack, 'warn');
    }
 
}

See ioBroker Forum Thread for details.

To Reproduce
See code above.

Expected behavior
Callback / await is working, i.e. state is created once callback function called or await createStateAsync() is processed.

Screenshots & Logfiles
See above and ioBroker Forum Thread

Versions:

  • Adapter version: 5.2.16
  • JS-Controller version: 3.3.21
  • Node version: 14.17.1
  • Operating system: Debian

Additional context
See ioBroker Forum Thread

@Acgua Acgua changed the title getState()/getStateAsync() issue, if 2nd param = initial value, and 3rd param = common obj createState()/createStateAsync() issue, if 2nd param = initial value, and 3rd param = common obj Dec 15, 2021
@Apollon77
Copy link
Contributor

Yes rthis can happen and if is a timing issue ... the create will trigger a "setState" internally but the "internal state array" is only filled when the state value is published which happens slightly async. So there is a chance that in the callback the state value publish is not yet received.

In fact we would need to set the value directly in the internal array directly and do not rely on publish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants