Enerwave dimmer with smart meter


#1

i try to put new code for dimmer with energy meter, but failed.

please help me with it
metadata {
// Automatically generated. Make future change here.
definition (name: “Enerwave Dimmer Switch1”, namespace: “enerwave”, author: " enerwave ") {
capability “Energy Meter”
capability “Switch Level”
capability “Actuator”
capability “Indicator”
capability “Switch”
capability “Power Meter”
capability “Polling”
capability “Refresh”
capability “Sensor”

	attribute "energyCost","string"
    attribute "energy", "string"
    attribute "power", "string"
    
    command "reset"				
    				
	fingerprint inClusters: "0x26,0x32"		
}			
			
simulator {			
	status "on":  "command: 2003, payload: FF"		
	status "off": "command: 2003, payload: 00"		
	status "09%": "command: 2003, payload: 09"		
	status "10%": "command: 2003, payload: 0A"		
	status "33%": "command: 2003, payload: 21"		
	status "66%": "command: 2003, payload: 42"		
	status "99%": "command: 2003, payload: 63"		

}
for (int i = 0; i <= 10000; i += 1000) {
status “power ${i} W”: new physicalgraph.zwave.Zwave().meterV1.meterReport(
scaledMeterValue: i, precision: 3, meterType: 4, scale: 2, size: 4).incomingMessage()
}
for (int i = 0; i <= 100; i += 10) {
status “energy ${i} kWh”: new physicalgraph.zwave.Zwave().meterV1.meterReport(
scaledMeterValue: i, precision: 3, meterType: 0, scale: 0, size: 4).incomingMessage()
}

	// reply messages		
	reply "2001FF,delay 5000,2602": "command: 2603, payload: FF"		
	reply "200100,delay 5000,2602": "command: 2603, payload: 00"		
	reply "200119,delay 5000,2602": "command: 2603, payload: 19"		
	reply "200132,delay 5000,2602": "command: 2603, payload: 32"		
	reply "20014B,delay 5000,2602": "command: 2603, payload: 4B"		
	reply "200163,delay 5000,2602": "command: 2603, payload: 63"		
}			
			
tiles {			
	standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {		
		state "on", label:'${name}', action:"switch.off", icon:"st.switches.switch.on", backgroundColor:"#79b821", nextState:"turningOff"	
		state "off", label:'${name}', action:"switch.on", icon:"st.switches.switch.off", backgroundColor:"#ffffff", nextState:"turningOn"	
		state "turningOn", label:'${name}', icon:"st.switches.switch.on", backgroundColor:"#79b821"	
		state "turningOff", label:'${name}', icon:"st.switches.switch.off", backgroundColor:"#ffffff"	
	}		
valueTile("energyCost", "device.energyCost") { state("default", label: '${currentValue}', foregroundColor: "#000000", backgroundColor:"#ffffff") }			
        valueTile("power", "device.power", width: 1, height: 1,decoration: "flat") {				
		state "default", label:'${currentValue} W'	
	}		
	valueTile("energy", "device.energy", width: 1, height: 1,decoration: "flat") {		
		state "default", label:'${currentValue} kWh'	
	}		
	standardTile("reset", "device.energy", width: 1, height: 1,inactiveLabel: false, decoration: "flat") {		
		state "default", label:'reset kWh', action:"reset"	
	}		
	standardTile("configure", "device.power", width: 1, height: 1,inactiveLabel: false, decoration: "flat") {		
		state "configure", label:'', action:"configuration.configure", icon:"st.secondary.configure"	
}			
	standardTile("indicator", "device.indicatorStatus", inactiveLabel: false, decoration: "flat") {		
		state "when off", action:"indicator.indicatorWhenOn", icon:"st.indicators.lit-when-off"	
		state "when on", action:"indicator.indicatorNever", icon:"st.indicators.lit-when-on"	
		state "never", action:"indicator.indicatorWhenOff", icon:"st.indicators.never-lit"	
	}		
	standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {		
		state "default", label:"", action:"refresh.refresh", icon:"st.secondary.refresh"	
	}		
	controlTile("levelSliderControl", "device.level", "slider", height: 1, width: 3, inactiveLabel: false) {		
		state "level", action:"switch level.setLevel"	
	}		
			
	main(["switch","energyCost"])		
	details(["switch","energyCost","power","energy","refresh","reset", "indicator", "levelSliderControl"])		
}			
	preferences { input "kWhCost", "string", title: "\$/kWh (1)", defaultValue: "1" as String } 

def parse(String description) {
def item1 = [
canBeCurrentState: false,
linkText: getLinkText(device),
isStateChange: false,
displayed: false,
descriptionText: description,
value: description
]
def result
def cmd = zwave.parse(description, [0x20: 1,0x26:1,0x70:1, 0x32:1])
if (cmd) {
result = createEvent(cmd, item1)
}
else {
item1.displayed = displayed(description, item1.isStateChange)
result = [item1]
}
log.debug “Parse returned ${result?.descriptionText}”
result
}
def creatEvent(physicalgraph.zwave.commands.meterv1.MeterReport cmd) {
if (cmd.scale == 0) {
newValue = cmd.scaledMeterValue
if (newValue != state.energyValue) {
dispValue = String.format("%5.2f",newValue)+"\nkWh"
sendEvent(name: “energyDisp”, value: dispValue as String, unit: “”)
state.energyValue = newValue
BigDecimal costDecimal = newValue * ( kWhCost as BigDecimal)
def costDisplay = String.format("%5.2f",costDecimal)
sendEvent(name: “energyTwo”, value: “Cost\n$${costDisplay}”, unit: “”)
[name: “energy”, value: newValue, unit: “kWh”]
}
}
else if (cmd.scale == 1) {
[name: “energy”, value: cmd.scaledMeterValue, unit: “kVAh”]
}
else {
[name: “power”, value: Math.round(cmd.scaledMeterValue), unit: “W”]
}
}
def createEvent(physicalgraph.zwave.commands.basicv1.BasicReport cmd, Map item1) {
def result = doCreateEvent(cmd, item1)
for (int i = 0; i < result.size(); i++) {
result[i].type = “physical”
}
result
}

def createEvent(physicalgraph.zwave.commands.basicv1.BasicSet cmd, Map item1) {
def result = doCreateEvent(cmd, item1)
for (int i = 0; i < result.size(); i++) {
result[i].type = “physical”
}
result
}

def createEvent(physicalgraph.zwave.commands.switchmultilevelv1.SwitchMultilevelStartLevelChange cmd, Map item1) {
[]
}

def createEvent(physicalgraph.zwave.commands.switchmultilevelv1.SwitchMultilevelStopLevelChange cmd, Map item1) {
[response(zwave.basicV1.basicGet())]
}

def createEvent(physicalgraph.zwave.commands.switchmultilevelv1.SwitchMultilevelSet cmd, Map item1) {
def result = doCreateEvent(cmd, item1)
for (int i = 0; i < result.size(); i++) {
result[i].type = “physical”
}
result
}

def createEvent(physicalgraph.zwave.commands.switchmultilevelv1.SwitchMultilevelReport cmd, Map item1) {
def result = doCreateEvent(cmd, item1)
result[0].descriptionText = “${item1.linkText} is ${item1.value}”
result[0].handlerName = cmd.value ? “statusOn” : “statusOff”
for (int i = 0; i < result.size(); i++) {
result[i].type = “digital”
}
result
}

def doCreateEvent(physicalgraph.zwave.Command cmd, Map item1) {
def result = [item1]

item1.name = "switch"			
item1.value = cmd.value ? "on" : "off"			
item1.handlerName = item1.value			
item1.descriptionText = "${item1.linkText} was turned ${item1.value}"			
item1.canBeCurrentState = true			
item1.isStateChange = isStateChange(device, item1.name, item1.value)			
item1.displayed = item1.isStateChange			
			
if (cmd.value >= 5) {			
	def item2 = new LinkedHashMap(item1)		
	item2.name = "level"		
	item2.value = cmd.value as String		
	item2.unit = "%"		
	item2.descriptionText = "${item1.linkText} dimmed ${item2.value} %"		
	item2.canBeCurrentState = true		
	item2.isStateChange = isStateChange(device, item2.name, item2.value)		
	item2.displayed = false		
	result << item2		
}			
result			

}

def creatEvent(physicalgraph.zwave.commands.configurationv1.ConfigurationReport cmd) {
def value = “when off”
if (cmd.configurationValue[0] == 1) {value = “when on”}
if (cmd.configurationValue[0] == 2) {value = “never”}
[name: “indicatorStatus”, value: value, display: false]
}

def createEvent(physicalgraph.zwave.Command cmd, Map map) {
// Handles any Z-Wave commands we aren’t interested in
log.debug “UNHANDLED COMMAND $cmd”
}

def on() {
log.info “on”
delayBetween([zwave.basicV1.basicSet(value: 0xFF).format(), zwave.switchMultilevelV1.switchMultilevelGet().format()], 5000)
}

def off() {
delayBetween ([zwave.basicV1.basicSet(value: 0x00).format(), zwave.switchMultilevelV1.switchMultilevelGet().format()], 5000)
}

def setLevel(value) {
def level = Math.min(value as Integer, 99)
delayBetween ([zwave.basicV1.basicSet(value: level).format(), zwave.switchMultilevelV1.switchMultilevelGet().format()], 5000)
}

def setLevel(value, duration) {
def level = Math.min(value as Integer, 99)
def dimmingDuration = duration < 128 ? duration : 128 + Math.round(duration / 60)
zwave.switchMultilevelV2.switchMultilevelSet(value: level, dimmingDuration: dimmingDuration).format()
}

def poll() {
zwave.switchMultilevelV1.switchMultilevelGet().format()
zwave.meterV2.meterGet(scale: 0).format()
zwave.meterV2.meterGet(scale: 2).format()

}

def refresh() {
zwave.switchMultilevelV1.switchMultilevelGet().format()
zwave.meterV2.meterGet(scale: 0).format()
zwave.meterV2.meterGet(scale: 2).format()
}

def indicatorWhenOn() {
sendEvent(name: “indicatorStatus”, value: “when on”, display: false)
zwave.configurationV1.configurationSet(configurationValue: [1], parameterNumber: 3, size: 1).format()
}

def indicatorWhenOff() {
sendEvent(name: “indicatorStatus”, value: “when off”, display: false)
zwave.configurationV1.configurationSet(configurationValue: [0], parameterNumber: 3, size: 1).format()
}

def indicatorNever() {
sendEvent(name: “indicatorStatus”, value: “never”, display: false)
zwave.configurationV1.configurationSet(configurationValue: [2], parameterNumber: 3, size: 1).format()
}

def invertSwitch(invert=true) {
if (invert) {
zwave.configurationV1.configurationSet(configurationValue: [1], parameterNumber: 4, size: 1).format()
}
else {
zwave.configurationV1.configurationSet(configurationValue: [0], parameterNumber: 4, size: 1).format()
}
}
def reset() { sendEvent(name: “energyCost”, value: “Cost\n–”, unit: “”)
return [
zwave.meterV2.meterReset().format(),
zwave.meterV2.meterGet(scale: 0).format()
]
}
def configure() {
delayBetween([
zwave.configurationV1.configurationSet(parameterNumber: 101, size: 4, scaledConfigurationValue: 4).format(), // combined power in watts
zwave.configurationV1.configurationSet(parameterNumber: 111, size: 4, scaledConfigurationValue: 300).format(), // every 5 min
zwave.configurationV1.configurationSet(parameterNumber: 102, size: 4, scaledConfigurationValue: 8).format(), // combined energy in kWh
zwave.configurationV1.configurationSet(parameterNumber: 112, size: 4, scaledConfigurationValue: 300).format(), // every 5 min
zwave.configurationV1.configurationSet(parameterNumber: 103, size: 4, scaledConfigurationValue: 0).format(), // no third report
zwave.configurationV1.configurationSet(parameterNumber: 113, size: 4, scaledConfigurationValue: 300).format() // every 5 min
])
}