TV Status or generic devices with ip address as gateway



  • Hi @jkandasa ,

    I'd like to be able to know when my TV is on. Thank to my friend, give me the idea to ping the chromecast. It's little hard to do with MySensor using gateway, because of space and old library. So I'd like to do it on MyController side.

    So, can we create a new Network type "Devices" with only IP address as property. And I'll be able to say turn off TV on no motion, and if TV is already off do nothing ?

    Or is their other way to handle than?


  • ADMIN

    @Fraid We can meet this requirement by using the script with the following steps,

    • Create a dummy status sensor variable for your TV(for ping status).
    • Create a UID tag for your dummy sensor variable.
    • Create a script to ping for your tv IP and update status. This script will update your dummy sensor as ON or OFF based on ping status.
    • Execute the script every x seconds/minutes once. based on your requirement. You can do other operations based on this dummy sensor variable.

    Dummy sensor

    0_1492105636790_upload-5e4a7202-bc4f-4451-90b5-641e81111ea7

    UID Tag

    0_1492105574757_upload-2ec84cee-5662-4c9a-9a32-553f5dd697bf

    Script

    var myImports = new JavaImporter(java.io, java.lang, java.util, java.text, java.net.InetAddress);
    
    with(myImports) {
      //Get sensor variable
      var statusVar =  mcApi.uidTag().getByUid(senVarUid).getResource();
      //Create pong object and update default values
      var pong = {};
      pong.reachable = false;
      pong.message = "";
      try{
        //Check reachable status
        var addresses = InetAddress.getAllByName(ip);
        for (var index = 0; index < addresses.length; index++) {
            if (addresses[index].isReachable(20000)) {
                pong.reachable = true;
                pong.message += "Reachable:["+ addresses[index]+"], ";
            } else {
                pong.message += "Failed:["+ addresses[index]+"], ";
            }
        }
        
      }catch(ex){
        //If there is an exception, mark it not reachable and record exception message
        pong.reachable = false;
        pong.error = ex.message;
      }
      var pingable = pong.reachable == true ? "1" : "0";
      //Check previous value. Update if there is a change
      if(statusVar.value !== pingable){
        statusVar.value = pingable;
        //Send value to sensor
        mcApi.sensor().sendPayload(statusVar);
      }
    }
    

    When execute this script you have to pass following bindings,

    • pi and senVarUid
    • Example: {"ip":"localhost", "senVarUid":"tv-ping"}

    Hope this will address your requirement.



  • @jkandasa Thank you it's perfectly working :D :D :D !
    I even dimmed the light when TV is turned on, awesome!

    Just to make sure I added a timer like following:
    *** Simple, Repeat[Interval:20 Seconds, Count:999999999], Executed count:75***
    If I set 0 he doesn't execute, so how can set infinite count ?


  • ADMIN

    @Fraid

    Just to make sure I added a timer like following:
    *** Simple, Repeat[Interval:20 Seconds, Count:999999999], Executed count:75***
    If I set 0 he doesn't execute, so how can set infinite count ?

    I request you to set timer interval at least 30 seconds. We have ping time-out as 20 seconds(20000) in the script. It is better to go higher this value.
    Set -1 as count to run infinitely.



  • @jkandasa thank you so much, it's working.


Log in to reply