@bravenel @mike.maxwell there is a bug in HE with the use of leftShift. According to this documentation, I should be able to use leftShift to update a value. This is what I was using in my NAS driver that prompted the OP question.
Since you cannot simulate the updates from my NAS UPS, I created the driver below so you can observe the issue.
Steps to reproduce:
- Install the driver and create a new virtual switch leveraging this custom "Simulated Dimmer Switch" driver
- Open up Logs in another tab to observe the debug output
- Initially I set the state.switchData to {level=100, switch=off} and with an off, on, or setLevel change, I duplicate the level and switch multiple times to demonstrate multiple items within the Map and the fact that all items aren't updated as they should.
- There is a preference to toggle the use of leftShift instead of a direct update, upon install leftShift is not turned on.
- With each on, off, or setLevel, the driver will log the value of switchData and after each state change, I press the Print State Variable button to log the value of switchData
- After installing, I press On and print the variable, Set Level to 50 and print the variable, and press off and print the variable. Without leftShift, all is good and all the values update correctly:
[dev:69] 2019-04-14 03:42:41.365 pm [debug] printStateVariable - state.switchData = [y.switch:off, z.switch:off, x.level:50, level:50, z.level:50, x.switch:off, y.level:50, switch:
off]
[dev:69] 2019-04-14 03:42:39.854 pm [debug] eventHelper (post update) - state.switchData = [y.switch:off, z.switch:off, x.level:50, level:50, z.level:50, x.switch:off, y.level:50, switch:off]
[dev:69] 2019-04-14 03:42:31.483 pm [debug] printStateVariable - state.switchData = [y.switch:on, z.switch:on, x.level:50, level:50, z.level:50, x.switch:on, y.level:50, switch:on]
[dev:69] 2019-04-14 03:42:29.769 pm [debug] eventHelper (post update) - state.switchData = [y.switch:on, z.switch:on, x.level:50, level:50, z.level:50, x.switch:on, y.level:50, switch:on]
[dev:69] 2019-04-14 03:42:17.313 pm [debug] printStateVariable - state.switchData = [y.switch:on, z.switch:on, x.level:99, level:99, z.level:99, x.switch:on, y.level:99, switch:on]
[dev:69] 2019-04-14 03:42:15.673 pm [debug] eventHelper (post update) - state.switchData = [y.switch:on, z.switch:on, x.level:99, level:99, z.level:99, x.switch:on, y.level:99, switch:on]
- Now toggle the leftShift preference and save preferences. This will reset the switchData back to the install state.
- Now perform the same actions again, turn on and print the variable, set level and print the variable, and turn off and print out the variable. Notice below the leftShift is duplicating values within the map prior to saving to disk but then when you print it, those duplicates go away but not all the values update correctly.
[dev:69] 2019-04-14 03:52:40.386 pm [debug] printStateVariable - state.switchData = [z.switch:off, y.switch:off, x.level:50, level:50, z.level:50, x.switch:on, y.level:50, switch:off]
[dev:69] 2019-04-14 03:52:38.899 pm [debug] eventHelper (post update) - state.switchData = [z.switch:on, y.switch:on, x.level:50, switch:off, z.switch:off, level:50, y.switch:off, z.level:50, x.switch:off, y.level:50, x.switch:on, switch:off]
[dev:69] 2019-04-14 03:52:36.495 pm [debug] printStateVariable - state.switchData = [z.switch:on, y.switch:on, x.level:50, level:50, z.level:50, y.level:50, x.switch:on, switch:off]
[dev:69] 2019-04-14 03:52:34.713 pm [debug] eventHelper (post update) - state.switchData = [z.switch:on, y.switch:on, level:100, y.level:50, level:50, x.switch:on, z.level:50, switch:off, x.level:50]
[dev:69] 2019-04-14 03:52:19.074 pm [debug] printStateVariable - state.switchData = [z.switch:on, y.switch:on, level:100, x.switch:on, switch:off]
[dev:69] 2019-04-14 03:52:17.678 pm [debug] eventHelper (post update) - state.switchData = [switch:on, z.switch:on, level:100, y.switch:on, x.switch:on, switch:off]
Here is the test driver:
metadata {
definition (name: "Simulated Dimmer Switch", namespace: "mlritchie", author: "Michael Ritchie") {
capability "Actuator"
capability "Sensor"
capability "Switch"
capability "Switch Level"
command "printStateVariable"
command "initialize"
}
preferences {
input("useLeftShift", "bool", title: "Use leftShift?", defaultValue: false)
}
}
def installed() {
initialize()
}
def updated() {
initialize()
}
private initialize() {
sendEvent(name: "switch", value: "off")
sendEvent(name: "level", value: 100)
state.switchData = ["switch":"off", "level": 100]
}
def on() {
eventHelper("switch", "on", null)
}
def off() {
eventHelper("switch", "off", null)
}
def setLevel(value) {
def intValue = value as Integer
def newLevel = Math.max(Math.min(intValue, 99), 0)
if (newLevel == 0) {
off()
} else {
if (device.currentValue("switch") != "on") {
on()
}
eventHelper("level", newLevel, "%")
}
}
def setLevel(value, duration) {
setLevel(value)
}
def eventHelper(name, value, unit) {
sendEvent(name: name, value: value, unit: unit)
if (useLeftShift) {
def switchValue = ["${name}" : value]
state.switchData << switchValue
switchValue = ["x.${name}" : value]
state.switchData << switchValue
switchValue = ["y.${name}" : value]
state.switchData << switchValue
switchValue = ["z.${name}" : value]
state.switchData << switchValue
} else {
state.switchData["${name}"] = value
state.switchData["x.${name}"] = value
state.switchData["y.${name}"] = value
state.switchData["z.${name}"] = value
}
log.debug "eventHelper (post update) - state.switchData = ${state.switchData}"
}
def printStateVariable() {
log.debug "printStateVariable - state.switchData = ${state.switchData}"
}