How to restore item values in openhab after power distribution?

  • Platform information:
    • Hardware: Raspberry Pi 4 4Gb
    • OS: openHabian
    • openHAB version: openHAB 3.2.0
    • Zigbee2MQTTversion: 1.24.0

What can I do so that openHab can restore the last state of a device connected via zigbee2mqtt? There are several dimmers YSR-MINI-01_dimmer (YSRSAI YSR-MINI-01_dimmer control via MQTT | Zigbee2MQTT). The problem is that when the electricity disappears and appears, they automatically turn on. Devices do not know how to save the last state.
How can I make it so that openhab can control them? If the dimmer was turned off before the power outage, then so that when the power comes on it would remain off. And vice versa.
I read about Persistence in the official documentation. In the \openHAB-conf\persistence folder, I created the rrd4j.persist file in which I specified the Items that should save their state. In the Settings->Persistence menu, I selected the item
RRD4j. But in a test power outage, it didn’t work.

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
    // for rrd charts, we need a cron strategy
    everyMinute : "0 * * * * ?"
}

Items {
    Bra_Left_Living_room_On_Off, Bra_Right_Living_room_On_Off, Countertop_Lighting_Kitchen_On_Off : strategy = everyChange, restoreOnStartup
}

Bra_Left_Living_room_On_Off, Bra_Right_Living_room_On_Off, Countertop_Lighting_Kitchen_On_Off these are items with a switch property.

As an experiment, I added another item of the switch type to this file, but it is not from the zigbee device, but from wi-fi. And he also did not return to the state in which he was before the disappearance of electricity. It seems to me that when loading openhabian, the zigbee2mqtt service does not have time to fully load and openhab can and tries to send a command to enable or disable, but it does not reach. With a wi-fi device, the same thoughts. I would like to solve this problem. Has anyone encountered a similar problem and how do you deal with it?

The persistence restoreOnStartup strategy only updates the internal state in openHAB, it doesn’t send commands to the devices, which means you have to use a different approach here. One thing that comes to mind is to create an extra item, something like Bra_Left_Living_room_On_Off_Last_Command which gets updated on every command to Bra_Left_Living_room_On_Off via a rule. This Item can then have persistence strategy everyChange, restoreOnStartup (and by the way, use mapdb for this, not sure rrd4j et al can restore every type of state correctly since it only stores numeric values). Finally, you can have a rule that runs on startup (but with a delay using a Timer) that sends the command to the real item.

You suggested an interesting idea with the control state and its tracking and further use in the rule. I’ll try to implement it. I also read about rrd4j Persistence. There is a mention of the data that this database can store. In my case, it fits.
P.S >NOTE: rrd4j is for storing numerical data only. It cannot store complex data types. The supported item types are therefore only Switch (internally mapped to 0/1), Dimmer , Number , Contact (internally mapped to 0/1), Rollershutter and Color (only brightness component stored)