I’ve made a rather simple scene that has been working for a long time.
But I noticed yesterday or the day before that the scene no longer does anything.
I added a message to see if it was triggering properly and I confirm that it is triggering.
I then replaced inverted with controlling a device and set the outlet to on and now it works.
I think the inverted action must have a bug somewhere but I have no info in the logs.
Has anyone had this kind of problem?
Thanks in advance for helping me debug ^^
Hi everyone!
This topic is now in development .
A PR has been opened to fix the inversion of the outlets that no longer followed the actual state of the device:
master ← claude/bug-inverse-prises-28peh7
ouvert 08:48AM - 04 Sep 26 UTC
### Description
A user reported on the forum that a scene using the **"Toggle s… witches"** (inverser) action stopped working on a plug, while replacing it with "Turn on switches" works. Nothing in the logs.
**Root cause.** Every store of the `StateManager` merges an update into the object it already holds (`Store.setState` does an `Object.assign`). When a device already loaded in RAM is saved again (edited in the UI, re-discovered by an integration, a feature added through `device.addFeature`...), `device.add` receives a fresh object from the DB:
- the `device` store merges it, so `device.features` now points to the **new** feature objects;
- the `deviceFeature` / `deviceFeatureById` / `deviceFeatureByExternalId` stores merge each new feature into the objects they held **from the first load**, and keep those.
From that moment the two views are different objects. `device.saveState` writes the new `last_value` only into the `deviceFeature` store, so anything reading a feature *through the device* (`stateManager.get('device', selector)` + `getDeviceFeature`) sees a `last_value` frozen at the time of the save, until the next restart. That is exactly what the scene toggle actions on switches and lights do (`last_value === 0 ? 1 : 0`): with a stale `last_value`, the toggle keeps sending the same value, and the plug never turns on. The voice switch/light commands and the blink action read the feature the same way.
**Fix.** `device.add` now merges each incoming feature into the object already in RAM (when there is one) and puts that object back into the device's `features` array, so every store shares a single object per feature. Nothing else changes: `device.create`, `device.init` and `device.addFeature` all go through `device.add`.
**Tests.** New `server/test/lib/device/device.add.test.js`: loads a device, adds it again from a fresh object, then simulates a new state as `device.saveState` stores it and checks the device seen from the `device` / `deviceById` stores reflects it (the test fails on `master`). It also covers a device re-added with its own objects and a brand new feature.
## Forum
Forum: https://community.gladysassistant.com/t/soucis-inverse-les-prises-ne-semble-pas-fonctionne/10790
### Checklist
- [x] Tests pass: `cd server && npm run coverage` (Codecov requires 100% coverage on changed lines) and Cypress (`npm run cypress:run`) if the UI changed
- [x] Linter and prettier pass on both front and server (`npm run eslint`, `npm run prettier`)
- [x] No undocumented breaking change
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01Qx6P7ZdwruxTUCnBAuC51T
---
_Generated by [Claude Code](https://claude.ai/code/session_01Qx6P7ZdwruxTUCnBAuC51T)_
## Summary by CodeRabbit
* **Bug Fixes**
* Improved device feature synchronization so updated feature values remain consistent across the device and feature stores.
* Preserved feature references when devices are re-added, preventing stale or mismatched state.
* Ensured newly added device features correctly receive subsequent state updates.
* **Tests**
* Added coverage for device and feature storage, indexing, re-adding devices, and state synchronization.
Feel free to follow the PR, test (optional, especially for small requests) and provide your feedback here if needed.