Add ack flag handling to onStateChange in adapter templates
Summary
This PR addresses a recurring issue during adapter reviews by adding proper ack flag handling to the onStateChange method in both JavaScript and TypeScript adapter templates.
Problem
The current templates don't include any ack flag checking in the onStateChange handler, which leads to a common mistake where adapters process all state changes regardless of their origin. This causes adapters to potentially respond to their own state updates as if they were user commands, leading to unnecessary processing or even infinite loops.
Solution
Added an explicit check for state.ack === false within the onStateChange method to filter for user commands that should be processed by the adapter. The implementation includes:
-
ACK flag validation: Only processes state changes when
ack === false(user commands) -
Clear documentation: Comments explain the difference between user commands (
ack=false) and adapter state updates (ack=true) - Developer guidance: TODO comment prompts developers to add their control logic in the appropriate place
Example
Before
onStateChange(id, state) {
if (state) {
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
} else {
this.log.info(`state ${id} deleted`);
}
}
After
onStateChange(id, state) {
if (state) {
this.log.info(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
// The state was changed by the user (ack=false) and not by the adapter
if (state.ack === false) {
// This is a command from the user (e.g., from the UI or other adapter)
// and should be processed by the adapter
this.log.info(`User command received for ${id}: ${state.val}`);
// TODO: Add your control logic here
}
} else {
this.log.info(`state ${id} deleted`);
}
}
Impact
- New adapters will have proper ack flag handling out of the box
- Adapter reviews will have fewer issues related to missing ack flag checks
- Better practice encourages developers to distinguish between user commands and state updates from the start
Testing
- ✅ All 50 baseline tests passing
- ✅ All lint checks passing
- ✅ Template generation tests passing
- ✅ TypeScript type checking passing
Fixes the issue mentioned in the original request where missing ack flag handling is a recurring problem during adapter reviews.
Original prompt
This section details on the original issue you should resolve
<issue_title>Update template to handle ack flag at onStateChange</issue_title> <issue_description>The template(s) seem to have not ack flag handling coded at onStateChange. (i.e. https://github.com/ioBroker/create-adapter/blob/0a55d4214496e96e33c40ce51dc012f2ee946220/templates/main.js.ts#L75)
I would suggest to enhance the template as during adapter review missing ack flag handling is a recurring issue.
(As soon as the change is confirmed I can try to create a PR but I will not start before some "ok we want this enhancement" statement)</issue_description>
<agent_instructions>Add a check with state.ack and document that only these are "control actions by users" that should be processed. ack== false are "just" state changes usually done by the adapter that should not lead to any control action</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@AlCalzone Sounds good!
Fixes ioBroker/create-adapter#1058
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.