How to script an Authorization header for OpenUrl

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
smartypantsuk
Posts: 12
Joined: Friday 21 January 2022 16:17
Target OS: Windows
Domoticz version: 2021.1
Location: North Yorkshire, UK
Contact:

How to script an Authorization header for OpenUrl

Post by smartypantsuk »

Hi,

Excuse my noobness, I'm very new to Domoticz and very new to Lua/Dzvents.
I have a dummy switch in Domoticz that I use as a boost heating button.
When it is 'On', a script updates a setpoint for 15 minutes on my Evohome.
When the button is 'Off', it effectively cancels the override by returning the Evohome zone to Auto.

So, I have a very basic script at the moment and what I am trying to do is use OpenURL to call a json command which switches the dummy boost button on/off in Homeseer. Simply put I want to keep both buttons in sync with on/off states. So when the Domoticz script runs it calls the respective json command to update the button in Homeseer.

The command works fine without basic authentication but I need it turned on for various reasons and I can make it work fine from ha-bridge and curl using the usual Authorization: Basic k9034je3edwd3e= format. However, with Dzvents I cannot work it out. I have read and searched every post out there and there is virtually nothing about this. I don't get any errors from the openurl command so I have no where to turn now.

If someone can point me in the right direction as I just don't know how to write the authentication properly.
Here's the primitive script.

Code: Select all

   return {
        on = {
            devices = {
            'BoostBathroom'
            }
        },
        execute = function(domoticz, switch)
           if (switch.state == 'On') then
                local untilDateTime = os.date("!%Y-%m-%dT%H:%M:%SZ",os.time()+900) 
                domoticz.devices('BathroomTRV').updateSetPoint(24.0, "TemporaryOverride", untilDateTime)
                domoticz.openURL (
                    {
                url = 'http://xxxx:xx/JSON?request=setdevicestatus&ref=1971&value=1',
                method = 'GET',
                headers = {
                  Authorization = "Basic scoobydoobydoo"
                  }
                }
        )
            elseif (switch.state == 'Off') then
                domoticz.devices('BathroomTRV').updateSetPoint(nil, "Auto", nil)   
        end
    end
}
Software:
Windows 10 Pro
Domoticz & Homeseer Home Assistant
HA-Bridge & Traccar
Jellfin Media Server

Hardware:
ASUS Mini Core i5 / 8GB RAM
Heating: Honeywell Evohome + HGS80
Fibaro Z-Wave Dimmers/Switches
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: How to script an Authorization header for OpenUrl

Post by jmleglise »

Solution 1 :
You have a login/password for your external URL ? You can write them like this :
domoticz.openURL('http://username:[email protected]/pat ... rameters=1')

Solution 2 :
write your CURL command in a shell script and execute it with dzventz " search "Asynchronous shell command execution" in https://www.domoticz.com/wiki/DzVents:_ ... _scripting

And an easy way to get your CURL command :
https://techtldr.com/copy-any-api-call- ... per-tools/
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
User avatar
smartypantsuk
Posts: 12
Joined: Friday 21 January 2022 16:17
Target OS: Windows
Domoticz version: 2021.1
Location: North Yorkshire, UK
Contact:

Re: How to script an Authorization header for OpenUrl

Post by smartypantsuk »

jmleglise wrote: Friday 21 January 2022 18:34 Solution 1 :
You have a login/password for your external URL ? You can write them like this :
domoticz.openURL('http://username:[email protected]/pat ... rameters=1')

Solution 2 :
write your CURL command in a shell script and execute it with dzventz " search "Asynchronous shell command execution" in https://www.domoticz.com/wiki/DzVents:_ ... _scripting

And an easy way to get your CURL command :
https://techtldr.com/copy-any-api-call- ... per-tools/
Thanks for that. So are you saying that the normal method of header authentication isn't possible in Dzvents? I say DzVents because I'm sure lua would be doable. I most likely to use curl and keep encoding but would have preferred it all in script rather than relying on more external files.
Software:
Windows 10 Pro
Domoticz & Homeseer Home Assistant
HA-Bridge & Traccar
Jellfin Media Server

Hardware:
ASUS Mini Core i5 / 8GB RAM
Heating: Honeywell Evohome + HGS80
Fibaro Z-Wave Dimmers/Switches
User avatar
smartypantsuk
Posts: 12
Joined: Friday 21 January 2022 16:17
Target OS: Windows
Domoticz version: 2021.1
Location: North Yorkshire, UK
Contact:

Re: How to script an Authorization header for OpenUrl

Post by smartypantsuk »

For the record, when I went back to this script later today it was working properly.
I have no reason why it decided to now work, but it has, so for reference, this works for me when base64 encoding is required for user:pass in the header.

Code: Select all

                domoticz.openURL (
                    {
                url = 'http://x.x.x.x:xx/JSON?request=setdevicestatus&ref=1971&value=1',
                method = 'GET',
                headers = {
                  Authorization = "Basic <yourbase64encodeduser:passhere>" 
                  }
                }
        )
Software:
Windows 10 Pro
Domoticz & Homeseer Home Assistant
HA-Bridge & Traccar
Jellfin Media Server

Hardware:
ASUS Mini Core i5 / 8GB RAM
Heating: Honeywell Evohome + HGS80
Fibaro Z-Wave Dimmers/Switches
boggiz
Posts: 46
Joined: Thursday 07 March 2019 11:40
Target OS: NAS (Synology & others)
Domoticz version: 2024.4
Location: France
Contact:

Re: How to script an Authorization header for OpenUrl

Post by boggiz »

Many thx to you @smartypantsuk !
I give us a link to encode/decode in Base64
:https://codebeautify.org/base64-encode

Is it secure ???
NAS Syno DS718+ with 12Go ram
RFXtrx433XL v1044 with 3dBi antenna
Domoticz 2024.4 on Docker
TFA rain gauge, 4 ways automatic watering program, LINKY consumption, waterflow counter
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest