Adding a virtual device that will send an HTTP command to my Yamaha Receiver so I can link it to a scene and use it with Alexa

I have owned my Z-Box for some time but have not used it much. Z-box works with a couple z-wave light switches I added, and it works well with Alexa too to turn those on and off. I am beginning to migrate over from my old Vera3 and VeraPlus system, which had other features too. For now, all I want to do is create a device that will trigger a Yamaha Receiver’s HTTP IP command. On the Z-Box, I have tried setting up a LUA Device and inserting some code in the action box I obtained from ChatGPT and other forums but have been unsuccessful. The HTTP commands currently work from any browser so I don’t understand why I can’t get them to work with Z-Box sending them out. I think I just need to figure out the manner in which to insert the HTTP commands into the Lua device action box. Are there any tutorials on just getting it to send simple HTTP IP commands using a device / scene setup? Or, if one of you have such a script/code, can you just post it so I can insert my own IP command.

For instance, ChatGPT told me to insert this into the Action box of the Device I created but it doesnt work when I hit ‘run action’…

local http = require(“socket.http”)
local ltn12 = require(“ltn12”)

local response_body = {}

local res, code, headers, status = http.request{
url = “http://192.168.1.127/YamahaExtendedControl/v1/main/setInput?input=hdmi5”,
method = “GET”,
sink = ltn12.sink.table(response_body)
}

print(“HTTP status:”, code)

Any ideas?

Thanks.

Test this quickapp code:
Change the Webhook_url to yours


-- Build webhook URL from variables
local WEBHOOK_URL = "https://someurltosendto"



function QuickApp:onInit()
    self:debug("Webhook QuickApp Initialized")
end

-- Function: webhook
function QuickApp:webhook(data)
    local http = net.HTTPClient({ timeout = 5000 })

    local payload = json.encode(data or {})  -- optional JSON payload

    self:debug("Sending webhook ")

    http:request(WEBHOOK_URL, {
        method = "POST",
        headers = { ["Content-Type"] = "application/json" },
        data = payload,
        success = function(response)
            if response.status == 200 or response.status == 204 then
                self:debug("successfully!")
            else
                self:error("Error: " .. tostring(response.status) .. " - " .. tostring(response.data))
            end
        end,
        error = function(err)
            self:error("HTTP error: " .. tostring(err))
        end
    })
end

-- Button in QuickApp UI
function QuickApp:button1()
    local data = {}
    self:webhook(data)  -- optional: pass table with parameters
end

Remember to create a button named button1 on the “onRelease” tag:

3 Likes

First question: do you have any experience writing/debugging Lua code?

Edit: I see @Brors94 has given you a much better example/starting point! We were typing at the same time :grinning_face_with_smiling_eyes:

1 Like

Was spot on the same time :laughing: :saluting_face:

1 Like

Thank you both for your responses. I inserted that code you posted and substituted my HTTP link on the second line after: local WEBHOOK_URL = “[here]”. Then I created the Button and viola, it worked! Thank you very much. I’ve come to rely on home automation based receiver-HDM-Iinput-switching and this will help me greatly !!! Thank you very much !! [ I do not have any experience with Lua code and will start learning online] This Z-Box is more advanced than my prior Veras (for sure) so I will get online and figure things out. Thanks again!

2 Likes

Glad I could help :smiley:
Checkout the fibaro forum for alot more info on qucikapps and lua:
https://forum.fibaro.com/

1 Like