Wemo switch and insight smart plug

Where is this in the code? It still doesn’t match up with what the wemo says is current wattage being used. Maybe I can mess with it.

There are two power-related events being created when a message comes in, one for daily energy and one for instaneous power. That happens near the bottom of the driver source.

What is it reporting for power now, and what does the WeMo app say it should be? Unfortunately the message format isn’t well documented, and I don’t have an Insight switch, so I’m piecing together information from various open source drivers. :slight_smile:

I found it and fixed it. Both the energy and WATT usage calculations were incorrect. Here is the updated code working correctly.

/**
 * WeMo Insight Switch driver
 *
 * Author: Jason Cheatham
 * Last updated: 2018-09-20, 21:27:31-0400
 *
 * Based on the original Wemo Switch driver by Juan Risso at SmartThings,
 * 2015-10-11.
 *
 * Copyright 2015 SmartThings
 *
 * Licensed under the Apache License, Version 2.0 (the 'License'); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

metadata {
definition(
    name: 'Wemo Insight Switch',
    namespace: 'jason0x43',
    author: 'Jason Cheatham'
) {
    capability 'Actuator'
    capability 'Switch'
    capability 'Polling'
    capability 'Refresh'
    capability 'Sensor'
    capability 'Power Meter'
    capability 'Energy Meter'

    command 'subscribe'
    command 'unsubscribe'
    command 'resubscribe'
}
}

def on() {
log.debug 'on()'
parent.childSetBinaryState(device, '1')
}

def off() {
log.debug 'off()'
parent.childSetBinaryState(device, '0')
}

def parse(description) {
log.trace 'parse()'

def msg = parseLanMessage(description)

def subscriptionData = parent.getSubscriptionData(msg)
if (subscriptionData != null) {
    log.trace "Updating subscriptionId to ${subscriptionData.sid}"
    updateDataValue('subscriptionId', subscriptionData.sid)
    log.trace "Scheduling resubscription in ${subscriptionData.timeout} s"
    runIn(subscriptionData.timeout, resubscribe)
}

def result = []
def bodyString = msg.body
if (bodyString) {
    try {
        unschedule('setOffline')
    } catch (e) {
        log.error 'unschedule("setOffline")'
    }

    def body = new XmlSlurper().parseText(bodyString)

    if (body?.property?.TimeSyncRequest?.text()) {
        log.trace 'Got TimeSyncRequest'
        result << timeSyncResponse()
    } else if (body?.Body?.SetBinaryStateResponse?.BinaryState?.text()) {
        def rawValue = body.Body.SetBinaryStateResponse.BinaryState.text()
        log.trace "Got SetBinaryStateResponse: ${rawValue}"
        result += createStateEvents(rawValue)
    } else if (body?.property?.BinaryState?.text()) {
        def rawValue = body.property.BinaryState.text()
        log.trace "Got BinaryState notification: ${rawValue}"
        result += createStateEvents(rawValue)
    } else if (body?.property?.TimeZoneNotification?.text()) {
        log.debug "Got TimeZoneNotification: Response = ${body.property.TimeZoneNotification.text()}"
    } else if (body?.Body?.GetBinaryStateResponse?.BinaryState?.text()) {
        def rawValue = body.Body.GetBinaryStateResponse.BinaryState.text()
        log.trace "Got GetBinaryResponse: ${rawValue}"
        result += createStateEvents(rawValue)
    } else if (body?.Body?.GetInsightParamsResponse?.InsightParams?.text()) {
        def rawValue = body.Body.GetInsightParamsResponse.InsightParams.text()
        log.trace "Got GetInsightParamsResponse: ${rawValue}"
        result += createStateEvents(rawValue)
    }
}

result
}

def poll() {
log.debug 'poll()'
if (device.currentValue('switch') != 'offline') {
    runIn(30, setOffline)
}
parent.childGetBinaryState(device)
}

def refresh() {
log.debug 'refresh()'
parent.childRefresh(device)
}

def resubscribe() {
log.debug 'resubscribe()'
runIn(10, subscribeIfNecessary)
parent.childResubscribe(device)
}

def scheduleResubscribe(timeout) {
runIn(resubscribeTimeout, resubscribe)
}

def setOffline() {
sendEvent(
    name: 'switch',
    value: 'offline',
    descriptionText: 'The device is offline'
)
}

def subscribe() {
log.debug 'subscribe()'
parent.childSubscribe(device)
}

def subscribeIfNecessary() {
log.trace 'subscribeIfNecessary'
parent.childSubscribeIfNecessary(device)
}

def sync(ip, port) {
log.debug 'sync()'
parent.childSync(device, ip, port)
}

def timeSyncResponse() {
log.debug 'Executing timeSyncResponse()'
parent.childTimeSyncResponse(device)
}

def unsubscribe() {
log.debug 'unsubscribe()'
parent.childUnsubscribe(device)
}

def updated() {
log.debug 'Updated'
refresh()
}

private createBinaryStateEvent(rawValue) {
// Insight switches actually support 3 values:
//   0: off
//   1: on
//   8: standby
// We consider 'standby' to be 'on'.
log.trace "Creating binary state event for ${rawValue}"
def value = rawValue == '0' ? 'off' : 'on';
createEvent(
    name: 'switch',
    value: value,
    descriptionText: "Switch is ${value}"
)
}

private createEnergyEvent(rawValue) {
log.trace "Creating energy event for ${rawValue}"
def value = (rawValue.toDouble() / 60000000).round(2)
createEvent(
    name: 'energy',
    value: value,
    descriptionText: "Energy today is ${value} WH"
)
}

private createPowerEvent(rawValue) {
log.trace "Creating power event for ${rawValue}"
def value = Math.round(rawValue.toInteger() / 1000)
createEvent(
    name: 'power',
    value: value,
    descriptionText: "Power is ${value} W"
)
}

// A state event will probably look like:
//   8|1536896687|5998|0|249789|1209600|118|190|164773|483265057
// Fields are:
//   8            on/off 
//   1536896687   last changed at (UNIX timestamp)
//   5998         last on for (seconds?)
//   0            on today
//   249789       on total
//   1209600      window (seconds) over which onTotal is aggregated
//   110          average power (Watts)
//   190          current power (Watts?)
//   164773       energy today (mW mins)
//   483265057    energy total (mW mins)
private createStateEvents(stateString) {
def params = stateString.split('\\|')
log.trace "Event params: ${params}"
def events = []
events << createBinaryStateEvent(params[0])
events << createPowerEvent(params[7])
events << createEnergyEvent(params[8])
return events
}
1 Like

Thanks! I'll update my source.

Thank You Very Much @jason0x43! I have a basic WeMo switch and your driver and app found and loaded it straight out of the gate! Too Cool Man!!

@jason0x43

Suggestion for code change on all DHs for correct polling.

def poll() {
    log.debug 'poll()'
    if (device.currentValue('switch') != 'offline') {
        runIn(3, setOffline)
    }
    parent.childGetBinaryState(device)
}

Original was 30 which turns out to poll every 30 minutes. At least with the Insight you need more polling for energy state so I placed this as 3 so I wouldn’t have to make a RM to refresh every 3 min.

Just a suggestion.

2 Likes

How to connect a Wemo Switch, Insight or motion in Hubitat with the @jason0x43 App and Drivers

1- Add your Wemo object in the official Wemo App and give it a name. This part MUST work before going to step 2.

1b- Not required but highly recommended : In your router, reserve the ip address of your Wemo device.

2- Select the driver for your device in this page and copy is code : https://github.com/jason0x43/hubitat/tree/master/drivers
Use the Raw button to copy only the code.

3- In Hubitat, Drivers Code, Click New Driver

4- Paste the Drivers code

5- Copy the App code at this link : https://github.com/jason0x43/hubitat/blob/master/apps/jason0x43-wemo_connect.groovy
Use the Raw button to copy only the code.

6- Paste it in Hubitat in, Apps Code, New App, Save

7- In Hubitat, Apps, click Add User App

8- Select the WeMo Connect App

9- WAIT WAIT It can take 50 secondes or more for all your device to show. The 0 found will change…

10- Now you need to be fast to select the device you want to add and hit Done. Auto-Refresh every 30 secondes

11- Your Wemo device must now be visible in the Devices menu

You can now use Hubitat or the Wemo App to control your device. Working since Hubitat V2.0.0.104 and always working in 2.3.0.124
I wanted to share with you what i just did.

5 Likes

FYI that I made sure all my WEMO devices had permanent IP addresses in my router table so that the need to rediscover will be limited. I did this when I first found many issues on ST that were related to ghost WEMOs and incorrect IPs.

I have WEMO smartplugs (from Costco) and although the app finds the plugs, it never creates a device for them. Any help?

I got it. You really do have to be fast enough to keep the device populated in the app discovery.

1 Like

Yeahhhh...that was an amusing process for me as well. DO NOT BLINK!! Haaaaaa :sunglasses:

Do you mean that devices are disappearing from the dropdown (or the dropdown is being closed) before you have a chance to select anything?

For me it's this scenario. Just gotta be quicky nimble finger to check the box in time.

Ah, I see what happened there. I just pushed an update to the app that increases the refresh time for the device selector to 30 seconds (it was initially set to refresh every 5 seconds).

thanks @ jason0x43 it was too fast 5 seconds!! Great work man

@lehighkid, you mentioned that you had some success with getting your Wemo Maker to work. Which app and drivers were you using? I'm trying to get my Maker working using the code posted by @jason0x43 but it doesn't seem to be discovering my Maker.

I just installed @jason0x43's Wemo and it works great except it doesn't recognize my Wemo dimmer. I noticed a post from @lehighkid last year about a version of the app that did.
Should I install @lehighkid's app too?
Is there a chance the ability to work the Wemo dimmer will be added to @jason0x43's?
On a side note, how do I know when Discovery is done?
TIA,
Jim

I can add dimmer and Maker drivers, but I can't test them since I don't have a dimmer or a Maker.

Discovery is a never-ending process. When the connect app is open, it broadcasts a request every 25 seconds and waits for devices to respond.

I would be happy to test the dimmer driver. Just give me the driver and tell me what logs you want and I'm on it.
Thanks.

I have 2 WEMO switches:


and

I have installed the drivers, and the app, but neither one can be found. I actually let the discovery run for over an hour.
I have rebooted the hub, and tried again, but no joy.
Any idea what else I can try?

Try opening the Hubitat logs, then open the WeMo Connect app in a new tab. What does the log show from the app? Also, are the Hubitat and the WeMo devices on the same subnet?