Virtual Switch, Alexa and Ifttt

If I can do it with google it’s the same for me.
What I’m trying to do is:

I have a shelly relay that turn on the main light of the room if I click a button. In the shelly app I can add a call to a url when a button is turned on or off. As I have other lights in that room I would love to turn them on when I click on the button to turn on the main light.

pretty simple

the code is this

I’m in the US so I’m not real familiar with your devices so I’m going to ask a couple questions here. BTW, Google won’t do what you want.

Do you actually have the Alexa assistant device or are you just using the app because it has routines?

You have the Shelly relay and Smartthings sees that, what are you using to control the other lights and is Smartthings seeing the other lights?

What’s the aqara sensor used for?

What are you using the simulated switches for? FWIW, I would create virtual switches instead of simulated switches. There have been reports of issues with virtual switches.

Do you have the new Smartthings app or the classic app loaded on your phone?

Answer these questions and we will go from there.

Thanks a lot for your patience, I’m going to try to answer all tour questions:

I do use 5 echo in my home so it’s not just the app

No, smartthings doesn’t see anything of my devices, if It would I should have solved all my Issues. Unfortunately shelly As Well As xiaomi for example or meros are not compatible with smartthing

Just to measure temp and humidity but it’s not strictly related to my issue

The flow is this one: i turn on the light of the room clicking on the button, shelly calls a Url configured As webhook in ifttt. When that Url is called ifttt open the virtual switch. When that switch is open alexa should trigger a routine that turn on other lights (that was my main problem I was trying to address, turn other lights on when i click on a button)

The one included in my Samsung phone

Try using the Classic App! Be sure to authorize the devices in the Classic App IFTTT section of Apps. Be sure to press save. Also make sure you are in the correct location.

Locations:

So your just using smart things for the virtual switches? Correct?

I am assuming that IFTTT can’t see whatever device is controlling your other lights.

At the Moment yes, i’m trying to use It just for that and maybe some Door sensor i would like to install It the work

At the Moment my problem is with alexa not seeing my simulated switch or seeing them but not usable to fire routines, why changing the app should help me on that? I’m going to try It anyay

I know in the forums I’ve seen references to some cloud interaction with Shelly and smart things. People are also using xiomi in smartthings too. Is your other device meros or meross?

Can you list model #'s of devices

Alexa is not seeing switches as triggers for routines because Alexa cannot use a switch as a trigger for a routine to do what you want, you need a specific type of virtual switch that shows up as BOTH a switch and a contact sensor because Alexa CAN trigger on a contact sensor.

Now that you have your location issues sorted (good job BTW) Search the forums - there’s a post that details exactly how to setup the kind of virtual device you’re going to need.

1 Like

Thanks for jumping in. I was hoping that someone with some Alexa insight would.

1 Like

Yep you guys got him to fix the location issues which honestly was the harder issue to work through. I’m mobile or I’d link straight to the post with the DTH you need. If we’re lucky someone will jump on with that link.

1 Like

Change the device type in IDE from a virtual switch to a simulated garage door opener. Open for on, closed for off. Discover devices. Create your routines. One for on, one for off. This is how I use my roborock with Alexa routines.

1 Like

Thanks a lot for your support,

first of all I finally was able to do it. How? Really don’t know. I was out from home for some hours and the only thing I did was to install the SmartThings classic app. When I got back home I tried again to make a routine and it worked. So what’s the problem now? It’s that when I turn on the main light with the phisical button, the event that turn on the other lights fire in about 10 seconds. A little bit too much. I don’t know if it’s alexa or iftt or the shelly url call that is taking so much time but it’s a huge time.

Excellent! I found them! In particoular I found this post

and I was able to configure the shelly in smartthings. Unfortunately I’m not able to use those as alexa routine trigger so I’m still stuck. I was trying to change the handler code to set those switch as sensor as well but the problem is that it works if I turn it on/off via the app but not using the phisical button, in this case it doesn’t change the status of the simulated sensor. This is the modified code but I think I’m missing something

    /**
 *  Shelly as Switch Device Handler
 *
 *  Copyright 2018 DUCCIO GASPARRI
 *
 *  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.
 *
 *
 *
 * Shelly http POST at http://IP/relay/0 ( can be 0/1 for shelly 2) with body form-urlencoded:
 *   turn=on
 *   turn=off
 *   ison=boolean
 *
 */
 
 

metadata {
	definition (name: "Shelly as Switch or Sensor", namespace: "decarlim", author: "Mario De carli") {
		capability "Actuator"
		capability "Sensor"
        capability "Refresh" // refresh command
        capability "Health Check"
        capability "Switch"
        capability "Contact Sensor"	 
	}

    simulator {
		status "open": "contact:open"
		status "closed": "contact:closed"
	}

    tiles(scale: 2) {
        multiAttributeTile(name:"switch", type: "lighting", width: 6, height: 4, canChangeIcon: true){
            tileAttribute ("device.switch", key: "PRIMARY_CONTROL") {
                attributeState "on", label:'${name}', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#00A0DC", nextState:"turningOff"
                attributeState "off", label:'${name}', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#FFFFFF", nextState:"turningOn", defaultState: true
                attributeState "turningOn", label:'Turning On', action:"switch.off", icon:"st.Home.home30", backgroundColor:"#00A0DC", nextState:"turningOn"
                attributeState "turningOff", label:'Turning Off', action:"switch.on", icon:"st.Home.home30", backgroundColor:"#FFFFFF", nextState:"turningOff"
            }
        }

        standardTile("explicitOn", "device.switch", width: 2, height: 2, decoration: "flat") {
            state "default", label: "On", action: "switch.on", icon: "st.Home.home30", backgroundColor: "#ffffff"
        }
        standardTile("explicitOff", "device.switch", width: 2, height: 2, decoration: "flat") {
            state "default", label: "Off", action: "switch.off", icon: "st.Home.home30", backgroundColor: "#ffffff"
        }

        standardTile("refresh", "device.refresh", width: 2, height: 2, inactiveLabel: false, decoration: "flat") {
            state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh", nextState: "disabled"
            state "disabled", label:'', action:"", icon:"st.secondary.refresh"
        }


        main(["switch"])
        details(["switch", "explicitOn", "explicitOff","refresh"])

    }


    preferences {
        input("ip", "string", title:"IP", description:"Shelly IP Address", defaultValue:"" , required: false, displayDuringSetup: true)
        input("channel", "string", title:"Channel", description:"Channel (only for Shelly2):", defaultValue:"0" , required: false, displayDuringSetup: true)
    }

}


def getCheckInterval() {
    // These are battery-powered devices, and it's not very critical
    // to know whether they're online or not – 12 hrs
    log.debug "getCheckInterval"
    return 4 * 60 * 60
}

def installed() {
    log.debug "Installed"
    sendEvent(name: "checkInterval", value: checkInterval, displayed: false)
    refresh()
}

def updated() {
    log.debug "Updated"
    if (device.latestValue("checkInterval") != checkInterval) {
        sendEvent(name: "checkInterval", value: checkInterval, displayed: false)
    }
    refresh()
}


def parse(description) {
    log.debug "Parsing result $description"
    
    def msg = parseLanMessage(description)

    // log.debug "Lan message $msg"
    // headers:[content-length:172, http/1.1 200 ok:null, connection:close, content-type:application/json, server:Mongoose/6.11], 
    // body:{"state":"close","power":0.00,"is_valid":true,"safety_switch":false,"stop_reason":"normal",
    //    "last_direction":"close","current_pos":46,"calibrating":false,"positioning":true}, 
    // header:HTTP/1.1 200 OK 
    
 
    def headersAsString = msg.header // => headers as a string
    def headerMap = msg.headers      // => headers as a Map
    def body = msg.body              // => request body as a string
    def status = msg.status          // => http status code of the response
    def data = msg.data              // => either JSON or XML in response body (whichever is specified by content-type header in response)
    
    log.debug "Data"
    log.debug data
    
    def evt1 = null
    if ( data.ison == true ) {
        log.debug "CreateEvent ison=true"
        evt1 = createEvent(name: "switch", value: "on", displayed: false)
        sendEvent(name: "contact", value: "open")
    } else  {
        log.debug "CreateEvent ison=false"
        evt1 = createEvent(name: "switch", value: "off", displayed: false)
        sendEvent(name: "contact", value: "closed")
    }

    return evt1
}


//switch.on
def on() {
    log.debug "Executing switch.on"
    sendSwitchCommand "turn=on"
    sendEvent(name: "contact", value: "open")
}

//switch.off
def off() {
    log.debug "Executing switch.off"
    sendSwitchCommand "turn=off"
    sendEvent(name: "contact", value: "closed")
}



def ping() {
    log.debug "Ping"
    refresh()
}

def refresh() {
    log.debug "Refresh - Getting Status"
    sendHubCommand(new physicalgraph.device.HubAction(
      method: "GET",
      path: "/relay/" + channel,
      headers: [
        HOST: getShellyAddress(),
        "Content-Type": "application/x-www-form-urlencoded"
      ]
    ))
}

def sendSwitchCommand(action) {
    log.debug "Calling /relay/ with $action"
    sendHubCommand(new physicalgraph.device.HubAction(
      method: "POST",
      path: "/relay/" + channel,
      body: action,
      headers: [
        HOST: getShellyAddress(),
        "Content-Type": "application/x-www-form-urlencoded"
      ]
    ))
    runIn(25, refresh)
}


private getShellyAddress() {
    def port = 80
    def iphex = ip.tokenize( '.' ).collect { String.format( '%02x', it.toInteger() ) }.join().toUpperCase()
    def porthex = String.format('%04x', port.toInteger())
    def shellyAddress = iphex + ":" + porthex
    device.deviceNetworkId = shellyAddress.toUpperCase()
    log.debug "Using IP " + ip + ", PORT 80 and HEX ADDRESS " + shellyAddress + " for device: ${device.id}"
    return device.deviceNetworkId
}

What I found is this one:

That also need to install something on my synology NAS. I will try to see if I can do something with that

The problem is that I have different devices by different manufacturers (meross, tplink, xiaomi, yeelight, some smart life devices, etc…) and the only software that can handle all of these is Alexa

I know that and that is in fact the handler I’m using right now, before I tried with this one but it wasn’t working. Don’t know why

The delay is occurring because you are having to use so many cloud to cloud interactions. I am actually surprised you only saw a 10 sec delay.

If you want this to work your best bet is going to be to replace some, or all, of your devices so that they can talk to each other on the same platform. Whether that is all xiaomi, melross or devices that will work directly with Smartthings. As long as you have to use so many different services to make your lights turn on your going to have a long delay.

Everything I have talks directly to my V1 Smartthings hub but, being that it’s cloud based, I will, on occasion, still see a 1-2 second delay from the time a motion sensor or door opens until the light comes on.

On a positive note, you learned a lot getting this project to work!

1 Like

each part of the flow is an http call of less than a kilobyte, how it would take so much time it’s nonsense, there is maybe some lag somewhere.

It’s not as easy and it’s expensive. I will still try to find a better solution. For this kind of feature a couple of seconds of delay it’s ok for me.
Now that I have a device handler in smartthings that handle shelly whenever I switch on/off in smartthing the alexa routine is fired immediately. The problem is that it doesn’t change the status in smartthing if I don’t do that by clicking on the phisical button and I have to figure out why

For this kind of device 1-2 seconds for me is unacceptable, I will give it a shot when I receive the aqara window sensor today. The other issue is how to make alexa to see the aqara temperature/humidity sensor

Yep, all of this. And here’s the link to the FAQ discussing how to create a virtual device that can be used in Alexa to trigger routines:
https://community.smartthings.com/t/faq-can-i-trigger-an-echo-action-without-speaking-to-it/117918

As I posted in the other thread, you simply need to add one of the following to the metadata in the device handler:
capability “Thermostat”
capability “Contact Sensor”
capability “Motion Sensor”

Which one you use is up to you, but be aware that this will enable commands that won’t do anything, and potentially result in confusing answers when you query Alexa about them, so choose wisely.
Once you do that, use the SmartThings app to give permission to Alexa for those devices (classic app would be Automation > SmartApps > Amazon Alexa > Device Authorization, don’t remember offhand where to do this in the new app). Once that is done have Alexa discover devices again.

2 Likes

At the moment the best solution I found, that reduce the lag, is this one:

Simulated switch
Webcore piston that turns that switch on or off
I got the external url of that piston and I made a webpage in a server I own that does proxy (when my page is called I call the webcore url, the reason is that the webcore url is too long for the shelly app)
I configure the shelly to call my url when the relay goes on/off
I configure an alexa routine that is triggered when the virtual switch is turned on/off

Now when I click on the button after 1/2 seconds the other lights are turned on/off
Not bad at all

I also installed the aqara door/window sensor and it works flawlessly at the moment turning on the light in less than a second.
Great!

I also found out that now Shelly has an integration in the Samsung Smartthings app. I tried it (after deleting all my custom shelly devices) but it doesn’t work for me