This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.

Homey and Fibaro HC2: Use a HC2 Global Variable change as trigger for Homey ... anyone?

mvdkallenmvdkallen Member
edited July 2016 in Questions & Help
Hi guys,

I can't manage to get this working and am stuck.

I'd like Homey to respond (.. execute a flow) based on a Global Variable change which is done by- and on my Home Center 2 controller. So Homey needs to read out the Global Variable on HC2 and once this Global Variable changes then Homey must respond instantly as well.

For some reason I can't manage to get this working... Can anyone check and support, please?

Instead of ditching one controller, I'd like both controllers to work together in my environment because I really see added value for a controller that can speak- and act on speech input.

Thanks and best regards,
Mark

«1

Comments

  • I got iI got both to work like a charm... you have HTTP request flow cards installed? ;)

  • djeskodjesko Member
    I got iI got both to work like a charm... you have HTTP request flow cards installed? ;)

    Any info to share on how ? 

    Thanks


  • Sure, just tell me what you need, i have request sending both ways, even with IFTTT trigger (Netatmo Welcome) going to Homey and set variables in HC. 
  • steenjensensteenjensen Member
    edited July 2016
    I mainly use 2 kinds: set gobal in HC and starts scenes. And use Homey as TTS, speech commands.

    And of course have HC trigger flows
  • mvdkallenmvdkallen Member
    edited July 2016
    Hi steenjensen,

    Ok, one example is that I'd like Homey to say something when I set the alarm for certain room through the HC2.

    For this example Homey must check the Global Variable called "alarmstatus" on HC2 and when it is set to "ingeschakeld" then it needs to say something.

    (By the way, yes I installed the HTTP request flow cards app... I think it is just me who need to understand the trick here :smiley: )

    Thanks





  • steenjensensteenjensen Member
    edited July 2016
    Ahhh okay

    damn lost my guide in this, hang on ;)
  • On the HC i use
    local homeyhttp = net.HTTPClient()
    homeyhttp:request('http://yourhomeyip/api/app/com.internet/homeysay/welcome-home----steen-jensen')

    You can't use spaces (dont know why) so i use "-" insted, and "----" x4 make a pause
  • steenjensensteenjensen Member
    edited July 2016
    As you can see in the screenshoot i made a flow with Incoming GET request and make it say the value, have yet to get it to work with https but for now i deselect Homey settings - HTTP request flow cards the: Require authorization on API (restart the app)

    -----
    I use this in some of code: to remove spaces
    spaceout = string.gsub(variable," ","-")

    And btw dont see why you would let Homey tjek an Global variable on the HC, when HC do that just fine ,) but you can have Homey set a Global variable on the HC, i use that ;P

  • @steenjensen: Maybe you can try using '%20' instead of a space, or does that not work either?
    http://www.w3schools.com/tags/ref_urlencode.asp
  • steenjensensteenjensen Member
    edited July 2016
    Let me try, thank you ;)
  • steenjensensteenjensen Member
    edited July 2016
    @steenjensen: Maybe you can try using '%20' instead of a space, or does that not work either?
    http://www.w3schools.com/tags/ref_urlencode.asp
    It works ,) 

    Well kinda, not here: 
    weather2 = string.gsub((weather1)," ","%20")
  • mvdkallenmvdkallen Member
    edited July 2016
    Hi Steenjensen,

    Thanks for your advice.

    In my situation I don't want Homey to change a Global Variable on HC2 (.. Homey always just have to respond on what happens by the HC2), or, (.. other example) Homey just have to say what Global Value is set on HC2 when I ask Homey to.

    I ask:  Hey Homey, what is the status of the alarm for room xyz?
    Homey should respond:    The alarm for room xyz is "set" (.. and "set" is actually the value of the Global Variable inside HC2).

    So besides on what I listed above about Homey responding when the Global Variable on HC2 changes, I also would like to be able to ask Homey what the status is of the alarm. For this flow I need to let Homey read out the HC2 Global Variable value of "alarmstatus". How do I do that?

    Sorry for the many questions, I'm going through a learning curve here wink 

  • steenjensensteenjensen Member
    edited July 2016
    Iam also at a learning progress its fun

    I see what you like to do, i have some guys looking into how to spilt a json array, like you do in HC, because that i think is what you can get from HC when you ask for the Global variables ...

    But for now i made a "work a round" by calling a scene in HC for the alarm status

    http://admin:yourpasswordhomecenterip:80/api/sceneControl?id=7&action=start
    7 = id of your scene

    But i do hope to figure out how to make Homey use a json array, just managed to do so in HC ;)
  • Hi Steenjensen,

    Although this will work, I'm not fond of putting the login details into any URL.

    I'd like to thank you for the above given support, I gave it a try and it is working. Though it is not fully what I am looking for; this way of working opens up some more ideas :smile: 

    Kind regards,
    Mark

  • @steenjensen ;

    Hi,

    You mentioned in an earlier post that you managed to let Homey change a global variable on Home Center 2.

    Can you show me how you manage to do this, because no matter what I try with a flow on Homey, the HC2 keeps the global variable set on the same value as it was).

    I must be overlooking something little here...

    Thanks and best regards,
    Mark

  • VonkenboerVonkenboer Member
    edited July 2016
    I'm just received my Homey, so sorry for the noob question...

    The goal is to send a http url to let homey say something. This I want to use when a status is changed on my Fibaro HC2.
    I installed, in my new Homey, the HTTP request app. Made a flow from the screenshot example steenjensen posted.

    When I post the url /api/app/com.internet/homeysay/welcome-home in my Firefox browser, I get an error:
    {
    • "status": 401,
    • "result": "no_auth_method_found"
    }

    I figured out how to get my token but where do I have to put this?
    Help me out guys, I'm just don't get it... (as usual)   :(


  • mvdkallen said:
    @steenjensen ;

    Hi,

    You mentioned in an earlier post that you managed to let Homey change a global variable on Home Center 2.

    Can you show me how you manage to do this, because no matter what I try with a flow on Homey, the HC2 keeps the global variable set on the same value as it was).

    I must be overlooking something little here...

    Thanks and best regards,
    Mark

    Hi Mark

    i use HTTP Put JSON

    http://admin:xxxxxxx@xx.xx.xx.xx/api/globalVariables/Hometime

    {"value":"MovieResumed"}

    Hometime are the global variable in HC
    and value are of course the value


  • I'm just received my Homey, so sorry for the noob question...

    The goal is to send a http url to let homey say something. This I want to use when a status is changed on my Fibaro HC2.
    I installed, in my new Homey, the HTTP request app. Made a flow from the screenshot example steenjensen posted.

    When I post the url /api/app/com.internet/homeysay/welcome-home in my Firefox browser, I get an error:
    {
    • "status": 401,
    • "result": "no_auth_method_found"
    }

    I figured out how to get my token but where do I have to put this?
    Help me out guys, I'm just don't get it... (as usual)   :(


    I dont use token yet, not sure if i have to, when I dont have any ports open in my router, but for now i uncheck the "Require authorization on API"


  • mvdkallenmvdkallen Member
    edited July 2016
    Hi @steenjensen ;;

    Ah, that's the trick... I didn't include the {"value" part but added just the value name instead in the input window  :p 
    Thanks a lot!

    Br. Mark

  • steenjensensteenjensen Member
    edited July 2016
    mvdkallen said:
    Hi @steenjensen ;;;

    Ah, that's the trick... I didn't include the {"value" part but added just the value name instead in the input window  p 
    Thanks a lot!

    Br. Mark

    You velcome :D

    Also by using "HTTP Get" you can start a scene

    http://admin:xxxxxx@xx.xx.xx.xx:80/api/sceneControl?id=7&action=start
  • I dont use token yet, not sure if i have to, when I dont have any ports open in my router, but for now i uncheck the "Require authorization on API"

    Clear! That works, thanks! Forgot to disable/enable the app to get it working.... For security reasons, can the token be used in a 'stand-alone' URL some way?
  • Se that is what i dont know... yet ;)
  • VonkenboerVonkenboer Member
    edited July 2016
    I want to do something back for the Homey community so I share some code I made.

    This code is for a scene you have to include in the Fibaro HC2.
    1 - Make two global variables on the HC Variables panel named SC_Homey_Command and SC_Homey_Value
    2 - Add a new scene with the lua code:
    --[[
    %% properties
    %% events
    %% globals
    SC_Homey_Command
    --]]
    --
    -- ℹ Initialisation of user settings (change if necessary)
    --
        -- Global variables (has to be made in HC2 variables panel)
    local globVarCommand = 'SC_Homey_Command'; -- Name of global variable to read command
    local globVarValue = 'SC_Homey_Value'; -- Name of global variable to read command value
    local homeyToken = ''; -- Not used yet
    local homeyIp = '192.168.0.61'; -- IP-address of your Athom Homey
        -- Debug options (true = Enable, false = Disable)
    local debug = true;
    --
    -- Funtions (do not change)
    --
        -- Debug and Log
    local function log(str) if debug then fibaro:debug('<font color="yellow">'..str..'</font>'); end; end
    local function errorlog(str) fibaro:debug('<font color="red">'..str..'</font>'); end  
    --
    -- Code (do not change)
    --
        -- Initialisation of communication to Homey
    local selfHttp = net.HTTPClient({timeout=5000});
        -- Prevents the scene from running when command is 0
    if (tonumber(fibaro:getGlobalValue(globVarCommand)) == 0) then
      fibaro:abort();
    end
        -- Get command and value from global variable and convert get space chrac   
    local command = fibaro:getGlobalValue(globVarCommand);
    local command = string.gsub(command,'%s','%%20'); -- Replace <space> with %20
    local value = fibaro:getGlobalValue(globVarValue);
    local value = string.gsub((value), '%s', '%%20'); -- Replace <space> with %20
    local value = string.gsub((value),'?','%%3F'); -- Replace ? with %3F
        -- Construct API URL
    local apiURL = 'http://'..homeyIp..'/api/app/com.internet/'..tostring(command)..'/'..value;
    if debug then log(apiURL) end;
        --Sending HTTP apiURL
    selfHttp:request(apiURL, {
      options={
        headers = selfHttp.controlHeaders,
        data = requestBody,
        method = 'GET',
        timeout = 5000
      },
      success = function(status)
        local result = json.decode(status.data);
        if result.status == 200 then
        if debug then log('<font color="green">OK</font>') end;
        else
          errorlog('failed');
          if debug then log(status.data) end;
        end
      end,
      error = function(error)
        errorlog("ERROR");
        if debug then log(error) end;
      end
    })
    -- Reset the global command variable to 0 and global value variable to empty
    fibaro:setGlobal(globVarCommand, '0');
    fibaro:setGlobal(globVarValue, '');

    To test it you can make a VD with a button with the code below. Press the button to make your Homey speak.You need the setup example from steenjensen in his posting (seventh from the top).

    local globVarCommand = 'SC_Homey_Command';
    local globVarValue = 'SC_Homey_Value';
    local commandTest = 'homeysay';
    local valueTest = 'blaaaah-blah----a a blaaaaah---- a a blaaaaaaaa---bla';

    fibaro:setGlobal(globVarValue, valueTest);
    fibaro:setGlobal(globVarCommand, commandTest);


    I tried it and it is working. To activate homey to speak, just store a text in the SC_Homey_Value and the Homey variable in SC_Homey_Command. The scene shows debug messages for clarity.

    BTW, you can use every HTTP Request Flow Cards GET Request trigger and store every value you want from your HC2 to your Homey! Just store the value in the HC2 global variable SC_Homey_Value and the GET Request trigger variable name in SC_Homey_Command.

    Have fun!
  • Wow! This is really a very nice piece of code. Exactly what I was looking for. You saved me a lot of time. Thank you so much!
  • You're welcome!  :)
  • Hi Vonkenboer.
    After looking on the internet, I did found your post about 'homey talk' via Fibaro.
    I did insert the code above, but couldn't get it work.

    When I use the debug functionality, I receive following error:
    [DEBUG] 13:00:24: line 25: invalid capture index %2

    I don't understand what I am doing wrong (I am not a program expert)

    This is what I included:

    --[[
    %% properties
    %% events
    %% globals
    --]]

    local globVarCommand = 'SC_Homey_Command'; -- Name of global variable to read command
    local globVarValue = 'SC_Homey_Value'; -- Name of global variable to read command value
    local homeyToken = ''; -- Not used yet
    local homeyIp = '192.168.100.130'; -- IP-address of your Athom Homey
    local debug = true;
    local function log(str) if debug then fibaro:debug('<font color="yellow">'..str..'</font>'); end; end
    local function errorlog(str) fibaro:debug('<font color="red">'..str..'</font>');
    end
    local selfHttp = net.HTTPClient({timeout=5000});
    if (tonumber(fibaro:getGlobalValue(globVarCommand)) == 0) then fibaro:abort();
    end
    local command = fibaro:getGlobalValue(globVarCommand);
    local command = string.gsub(command,'%20','%%20');
    local value = fibaro:getGlobalValue(globVarValue);
    local value = string.gsub((value), '%20', '%%20');
    local value = string.gsub((value),'3F','%%3F');
    local apiURL = 'http://'..homeyIp..'/api/app/com.internet/'..tostring(command)..'/'..value;
    if debug then log(apiURL) end;
    selfHttp:request(apiURL, {options={headers = selfHttp.controlHeaders,data = requestBody, method = 'GET', timeout = 5000 },
    success = function(status)local result = json.decode(status.data);
    if result.status == 200 then if debug then log('<font color="green">OK</font>') end ;
    else
    errorlog('failed');
    if debug then log(status.data) end;
    end
    end,
    error = function(error)
    errorlog("ERROR");
    if debug then log(error) end;
    end
    })
    fibaro:setGlobal(globVarCommand, '0');
    fibaro:setGlobal(globVarValue, '');

    Hope you or anyone on this forum could help.



  • VonkenboerVonkenboer Member
    edited February 2017
    Did you made a global variable in your HC2 called SC_Homey_Command and SC_Homey_Value?
    You have to add this in the Variables Panel.
  • Yes, I have created the 2 variables.


    and is changed the %s to '%20'
  • VonkenboerVonkenboer Member
    edited February 2017
    Ypolit said:
    Yes, I have created the 2 variables.


    and is changed the %s to '%20'
    Why? %s means 'any whitespace character'. 

    Also, can you add the global variable trigger:
    --[[
    %% properties
    %% events
    %% globals
    SC_Homey_Command
    --]]
    BTW, this is code only for using in a scene, not a Virtual Device....
  • Thanks Vonkenboer,

    Seems like I could fix the scene. When I now start the scene I get following error message:

    [DEBUG] 11:36:33: http://192.168.100.130/api/app/com.internet/homeysay/blaaaah-blah----a a blaaaaah---- a a blaaaaaaaa---bla
    [DEBUG] 11:36:34: failed
    [DEBUG] 11:36:34: {"status":401,"result":"no_auth_method_found"}

    Based on mu understanding, it looks like I need to insert somewhere my Homey Credentials. Any idea how I could resolve ?

Sign In or Register to comment.