- Platform information:
- Hardware: Pine64 aarch64/2gb/16gb SD
- OS: Ubuntu Bionic
- Java Runtime Environment: openjdk-11-jre 11.0.6+10-1ubuntu1~18.04.1 (arm64)
- openHAB version: openhab2 2.5.2-1
- Issue of the topic:
I have a rule that I use to save and reload scenes using storestates and restorestates.
var Map SceneState2 = null
rule "scene 2"
when
Item Scene_Buttons2 received command
then
if (Scene_Buttons2.state == OFF) {
SceneState2 = storeStates(RestoreBedroom)
}
else if (Scene_Buttons2.state == ON) {
restoreStates (SceneState2)
}
end
This rule works great, only pitfall is that if you edit the rule file or openhab restarts, your scenes are gone.
So i thought it would be ]simple enough to put the scenestate2 variable in a string item and persist that item in a mapdb. … BOY WAS I WRONG.
Here’s an attempt:
rule "test scene"
when
Item Scene_Buttons received command
then
if (Scene_Buttons.state == OFF) {
SceneString1.postUpdate(storeStates(RestoreBedroom).toString)
}
else if (Scene_Buttons.state == ON) {
restoreStates (SceneString1.state)
}
end
Saving the state tot the items works fine, but loading the string back into a map doesn’t work. I think i needs to cast .toMap … but cant find any info on how to get my string containing a map back into a map variable. Do i need to use another serializer ?
What it looks like
Sitemap lines involved
Group item=Scene_Switch label="Scenes" {
Switch item=Scene_Buttons label="Scene 1" mappings=[OFF="Save", ON="load"]
Switch item=Scene_Buttons2 label="Scene 2" mappings=[OFF="Save", ON="load"]
Switch item=Scene_Buttons3 label="Scene 3" mappings=[OFF="Save", ON="load"]
}
Switch item=Scene_Switch label="Scene" mappings=[1="1",2="2",3="3"]
Some items involved
Group RestoreBedroom "Group containing item channels to be restored as scene" <firstfloor> (Home) ["FirstFloor"]
Switch Scene_Buttons "save scene" <flowpipe> (scenetest)
Switch Scene_Buttons2 "save scene 2" <flowpipe> (scenetest)
Switch Scene_Buttons3 "save scene 3" <flowpipe> (scenetest)
Number Scene_Switch <flowpipe>
String SceneString1 "string containg scene data for persistence"
String SceneString2 "string containg scene data for persistence"
String SceneString3 "string containg scene data for persistence"
Logs whens saving … which seems to work …
2020-02-26 11:02:27.292 [vent.ItemStateChangedEvent] - Scene_Buttons changed from NULL to OFF
2020-02-26 11:02:28.709 [vent.ItemStateChangedEvent] - SceneString1 changed from NULL to {Wemos_Power3 (Type=SwitchItem, State=OFF, Label=Power3, Category=light, Groups=[Bedroom, Wemos, RestoreBedroom, Bedroom_Switch])=OFF, Cubes_Ledstrip_Power (Type=SwitchItem, State=OFF, Label=Power, Category=light, Groups=[Bedroom, Cubes_Ledstrip, RestoreBedroom, Bedroom_Switch])=OFF, LSC_White_Led_RestoreState (Type=StringItem, State=FF00, Label=null, Category=network, Groups=[RestoreBedroom])=FF00, Wemos_Power5 (Type=SwitchItem, State=OFF, Label=Power5, Category=light, Groups=[Bedroom, Wemos, RestoreBedroom])=OFF, Wemos_Power4 (Type=SwitchItem, State=OFF, Label=Power4, Category=light, Groups=[Bedroom, Wemos, RestoreBedroom, Bedroom_Switch])=OFF, Wemos_Power7 (Type=SwitchItem, State=OFF, Label=Power7, Category=light, Groups=[Bedroom, Wemos, RestoreBedroom])=OFF, Wemos_Power6 (Type=SwitchItem, State=OFF, Label=Power6, Category=light, Groups=[Bedroom, Wemos, RestoreBedroom])=OFF, Cubes_Ledstrip_RestoreState (Type=StringItem, State=2F00FF, Label=null, Category=network, Groups=[RestoreBedroom])=2F00FF, Sidelight_Power (Type=SwitchItem, State=OFF, Label=Power, Category=light, Groups=[Bedroom, Sidelight, RestoreBedroom, Bedroom_Switch])=OFF, Wemos_RestoreState (Type=StringItem, State=000000, Label=null, Category=network, Groups=[RestoreBedroom])=000000, LSC_White_Led_Power (Type=SwitchItem, State=ON, Label=Power, Category=light, Groups=[Bedroom, LSC_White_Led, RestoreBedroom, Bedroom_Switch])=ON, Sidelight_RestoreState (Type=StringItem, State=FF0A1E0000, Label=null, Category=network, Groups=[RestoreBedroom])=FF0A1E0000}
Log when trying to load the scene
2020-02-26 11:03:52.509 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test scene': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.restoreStates(java.util.Map) on instance: null
Not sure if SceneString1 contains all info to go back to a map and if the structure is still valid after .toString, neither how to ask openhab to make it a map.
So basically I’m looking for a way to make restoreStates (SceneString1.state)
work. I understand that string and map are not the same, but I cant find a way to convert between the twom or how else to persist a map.