Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

http request in a HC3 scene


AlexPeret

Question

Hello,

I'm trying to "convert" this LUA script in a scene, but my trouble cames from the fact that I don't know how to send an http request in a scene.

Here is the script I use in a device (and it works find):

Please login or register to see this code.

The goal, is to send a time stamped notification in a remote web server when a scene is launch, but maybe I'm on a wrong way ?

 

Thanks for your help.

Alex.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Thanks for you answer @SmartHomeEddy

    In the solution you gave, you use a QuickApp that trigger the request and in the scene you call the Quickapp. That's can do the job, but is there a shorten solution to send the request directely in the scene ?

    Link to comment
    Share on other sites

    • 0
    1 hour ago, AlexPeret said:

    but is there a shorten solution to send the request directely in the scene ?

     

    sure, e.g.

     

    Please login or register to see this code.

    if you don't need any authorization, you might remove the base64 and headers = {.....} part

    • Like 2
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Whaou ! thanks very much @tinman that's do the job !

     

    By the way is there a solution to include my own librarie ? I will used a lot of times the url_encode function and I don't want to copy-paste it everywhere, and I think it can more powerfull just to include a cutom librarie with this code (and some other functions for latter).

    Edited by AlexPeret
    Link to comment
    Share on other sites

    • 0

    Obs: Sorry for possible errors ... I used the google translator.

     

    Hello.

     

    I migrated from HC2 to HC3. At HC2 I had a generic scene that was called by several others to send me a message on the telegram.

     

    Example: Someone touched the campaign, I received a message on Telegram: "The campaign has been activated". The door was opened, I received a message "the door was opened".

     

    It used a global variable in which the main scene determined the variable telegram and, after that, the generic scene that sent the message to the telegram was called.

     

    However, with HC3, this scene stopped working.

     

    I made some changes to the LUA code, but I can't get the message on the telegram.

     

    The error message I get is:

     

    [DEBUG] [SCENE54]: 400
    [DEBUG] [SCENE54]: {"ok": false, "error_code": 400, "description": "Bad Request: message text is empty"}

     

    I put one more message in this code to see the value of the variable Telegram and url, and both are not reset.

     

    fibaro.debug("URL é", url)
    fibaro.debug("MSG é", msg)

     

    Please, could someone guide me where I was missing the LUA code below? Thank you.

    ------------------------------------------------------------------------------

    --[[
    %% properties
    %% globals
    Telegram
    --]]

     

    local token = "nr. TOKEN of TELEGRAM"
    local chat_id = "nr. chat id"
    local url = "https://api.telegram.org/bot"..token.."/sendMessage?chat_id="..chat_id.."&text="

     

    if (tonumber(fibaro.getGlobalVariable("Telegram")) == 0) then
      fibaro:abort();
    end

     

    local selfhttp = net.HTTPClient({timeout=2000})

     

    local msg = fibaro.getGlobalVariable("Telegram")

     

    url = url .. msg

     

    selfhttp:request(url, {
      options={
        method = 'GET',
        headers = selfhttp.controlHeaders,
        data = requestBody,
        timeout = 5000
      },
     success = function(response) 
                print(response.status)
                print(response.data)
        end,
        error = function(message)
                print("error:", message)
        end
    })

     

    fibaro.debug("URL é", url)
    fibaro.debug("MSG é", msg)

     

    --Reset the global Telegram variable to 0


    fibaro.setGlobalVariable("Telegram", "0")

    Link to comment
    Share on other sites

    • 0

    I installed HC3 for my home, and when I sent it to the telegram I received the same error.
    In the mode of sending by the GET method, it was not possible to successfully send the message (more precisely, I did not find such a method). However, by switching to the POST method, the message was sent successfully.

    I took a sample for the program from this forum thread: 


    Below is an example. From what I added to the program text - a variable for specifying the chat ID, and the text of the message. Plus one more variable for request headers.

    Please login or register to see this code.

     

    Link to comment
    Share on other sites

    • 0
    On 4/25/2021 at 1:17 PM, DobrijZmej said:

    I installed HC3 for my home, and when I sent it to the telegram I received the same error.
    In the mode of sending by the GET method, it was not possible to successfully send the message (more precisely, I did not find such a method). However, by switching to the POST method, the message was sent successfully.

    I took a sample for the program from this forum thread: 


    Below is an example. From what I added to the program text - a variable for specifying the chat ID, and the text of the message. Plus one more variable for request headers.

    Please login or register to see this code.

     

    Try this @Brors94  to integrate your zaptec charger sendCommand

    Please login or register to see this link.

     

    • Thanks 1
    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...