Save device states?

I am planning an automation where the light reacts to an Event like ringing the door and turns up the light to 100%, After that it should go back to it’s State before (E.g. Off, 30%, 50%…)

Before the turn off of the grovy API, I was able to do that with webcore. Now I am unsure if There is any way to save device states in order to Set lights later on back to the previous saved State.

I know There is this rule API, but even as an Programmer I have a hard time to get to work with it, since json isn’t a programming Language, and thefore it’s hard to follows the Syntax rules.

I don’t know about Rules API so hopefully someone can chime in.
I switched to Shaprtools, a 3rd party rule engine, when webcore access ended. This should work as long as your doorbell being pressed can be used as a trigger. Tagging @joshua_lyon from Sharptools who can provide more details.
Things to know:

  1. It is not local
  2. Requires a paid ($30/year) subscription to access variables
2 Likes

You can try these RulesAPI rules. The rules store the lamp state to the virtual lamp (dimmer) and restore the lamp state from the virtual lamp.

I tested rule using TAustin Virtual Devices V2

Store light state:

{
  "name": "vSwitch On > Light Store Level",
  "actions": [
    {
      "if": {
			"equals": {
				"right": {
					"device": {
						"devices": [
							"-- virtualSwitch ID --"
						],
						"component": "main",
						"capability": "switch",
						"attribute": "switch",
					}
				},
				"left": {
					"string": "on"
				}
			},
        "then": [
			{
			"if":
				{
					"equals": {
						"right": {
							"device": {
								"devices": [
									"-- Lamp ID --"
								],
								"component": "main",
								"capability": "switch",
								"attribute": "switch",
								"trigger": "never"
							}
						},
						"left": {
							"string": "off"
						}
					},
				  "then": [
					{
						"command": {
						  "devices": [
							"-- virtualDimmer ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switchLevel",
							  "command": "setLevel",
							  "arguments": [
								{
								  "device": {
									"devices": [
									  "-- Lamp ID --"
									],
									"component": "main",
									"capability": "switchLevel",
									"attribute": "level"
								  }
								}
							  ]
							}
						  ]
						}
					},
					{			
						"command": {
						  "devices": [
							"-- virtualDimmer ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switch",
							  "command": "off"
							}
						  ]
						}
					}				
					],
				  "else": [
					{
						"command": {
						  "devices": [
							"-- virtualDimmer ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switchLevel",
							  "command": "setLevel",
							  "arguments": [
								{
								  "device": {
									"devices": [
									  "-- Lamp ID --"
									],
									"component": "main",
									"capability": "switchLevel",
									"attribute": "level"
								  }
								}
							  ]
							}
						  ]
						}
					},
					{			
						"command": {
						  "devices": [
							"-- virtualDimmer ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switch",
							  "command": "on"
							}
						  ]
						}
					},					
					]
				}
			}
			]
		}
	}
	]
}

Restore light state:

{
  "name": "vSwitch Off > Light Restore Level",
  "actions": [
    {
      "if": {
			"equals": {
				"right": {
					"device": {
						"devices": [
							"-- virtualSwitch ID --"
						],
						"component": "main",
						"capability": "switch",
						"attribute": "switch"
					}
				},
				"left": {
					"string": "off"
				}
			},
        "then": [
			{
			"if":
				{
					"equals": {
						"right": {
							"device": {
								"devices": [
									"-- virtualDimmer ID --"
								],
								"component": "main",
								"capability": "switch",
								"attribute": "switch",
								"trigger": "never"
							}
						},
						"left": {
							"string": "off"
						}
					},
				  "then": [
					{
						"command": {
						  "devices": [
							"-- Lamp ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switchLevel",
							  "command": "setLevel",
							  "arguments": [
								{
								  "device": {
									"devices": [
									  "-- virtualDimmer ID --"
									],
									"component": "main",
									"capability": "switchLevel",
									"attribute": "level"
								  }
								}
							  ]
							}
						  ]
						}
					},
					{
                        "sleep": {
                            "duration": {
                                "value": {
                                    "integer": 2
                                },
                                "unit": "Second"
                            }
                        }
                    },
					{			
						"command": {
						  "devices": [
							"-- Lamp ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switch",
							  "command": "off"
							}
						  ]
						}
					}
					],
				  "else": [
					{
						"command": {
						  "devices": [
							"-- Lamp ID --"
						  ],
						  "commands": [
							{
							  "component": "main",
							  "capability": "switchLevel",
							  "command": "setLevel",
							  "arguments": [
								{
								  "device": {
									"devices": [
									  "-- virtualDimmer ID --"
									],
									"component": "main",
									"capability": "switchLevel",
									"attribute": "level"
								  }
								}
							  ]
							}
						  ]
						}
					}
					]
				}
			}
			]
		}
	}
	]
}

Rules are controlled by a virtual switch.

3 Likes