Hue formula with openURL [Solved]

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

Moderator: leecollings

Post Reply
johnnyboy1984
Posts: 13
Joined: Thursday 14 March 2019 17:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Hue formula with openURL [Solved]

Post by johnnyboy1984 »

Hi there,
I am trying to switch a formula (tv mimicking) on/off with a dummy switch.
I want when the dummy switch is switched on/off to start or stop the hue tv mimicking formula. Unfortunatly it doesn't work. I retrieved the api and code with the HUE clip api debugger and that works, but i don't know how to convert the code into a script, please help:

Code: Select all

 return {
        on = {
          devices = {
             'test1'
          }
        },
        execute = function(domoticz, switch)
          if (switch.state == 'On') then
             domoticz.log('TV Mimick on')
                        domoticz.openURL({
                        url = 'http://<my hue ipadress>/api/<my api code>/sensors/55/state',
                        method = 'PUT',
                        put = { "status": 1 }
                    })
          else
             domoticz.log('TV Mimick off!')
                        domoticz.openURL({
                        url = 'http://<my hue ipadress>/api/<my api code>/sensors/55/state',
                        method = 'PUT',
                        put = { "status": 0 }
                    })
          end
        end
}
Last edited by johnnyboy1984 on Friday 05 June 2020 10:59, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Hue formula with openURL

Post by waaren »

johnnyboy1984 wrote: Thursday 04 June 2020 11:26 I retrieved the api and code with the HUE clip api debugger and that works, but i don't know how to convert the code into a script, please help:
From the wiki:
postData: Optional. When doing a POST, PUT 3.0.2 or DELETE 3.0.2 this data will be the payload of the request (body).
so try with:

Code: Select all

 return {
        on = {
          devices = {
             'test1'
          }
        },
        execute = function(domoticz, switch)
          if (switch.state == 'On') then
             domoticz.log('TV Mimick on')
                        domoticz.openURL({
                        url = 'http://<my hue ipadress>/api/<my api code>/sensors/55/state',
                        method = 'PUT',
                        postData = { "status" = 1 }
                    })
          else
             domoticz.log('TV Mimick off!')
                        domoticz.openURL({
                        url = 'http://<my hue ipadress>/api/<my api code>/sensors/55/state',
                        method = 'PUT',
                        postData = { "status" = 0 }
                    })
          end
        end
}
If it still fails, please share the relevant loglines and the version you are on.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johnnyboy1984
Posts: 13
Joined: Thursday 14 March 2019 17:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Hue formula with openURL

Post by johnnyboy1984 »

still no succes, error log sais:

Code: Select all

2020-06-04 11:55:26.006 Error: dzVents: Error: (3.0.1) error loading module 'Script Hue TV mimicking' from file '/usr/local/domoticz/var/scripts/dzVents/generated_scripts/Script Hue TV mimicking.lua': 
 2020-06-04 11:55:26.006 ...ts/dzVents/generated_scripts/Script Hue TV mimicking.lua:14: '}' expected near ':' 
i have dzVents Version: 3.0.1
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Hue formula with openURL

Post by waaren »

johnnyboy1984 wrote: Thursday 04 June 2020 11:58 Still no succes
i have dzVents Version: 3.0.1
The methods PUT and DELETE are not available in dzVents version 3.0.1
These methods were introduced in Version 3.0.2

Also change "status" : 1 to "status" = 1
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johnnyboy1984
Posts: 13
Joined: Thursday 14 March 2019 17:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Hue formula with openURL

Post by johnnyboy1984 »

ok, and dzVents 3.0.2 is in Domoticz 2020.2 right?
But that vesrsion is not available for Synology, is there another way with Lua maybe to achivef my goals?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Hue formula with openURL

Post by waaren »

johnnyboy1984 wrote: Thursday 04 June 2020 12:25 ok, and dzVents 3.0.2 is in Domoticz 2020.2 right?
But that version is not available for Synology, is there another way with Lua maybe to achieve my goals?
Not natively with dzVents or Lua but os.execute(CurlCommand) in a dzVents script could be a work around.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johnnyboy1984
Posts: 13
Joined: Thursday 14 March 2019 17:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Hue formula with openURL

Post by johnnyboy1984 »

ok, so i found this other https://www.domoticz.com/forum/viewtopi ... 36&t=31481, and used a part of the script, but still an error in my log:

Code: Select all

return {
        on = {
          devices = {
             'test1'
          }
        },
        execute = function(domoticz, switch)
          if (switch.state == 'On') then
             domoticz.log('TV Mimick on')
             os.execute('curl --header "Content-Type: application/json"   --request PUT  --data '{"status":1}'   http://<my hue ipadress>/api/<my api code>/sensors/55/state')         
          else
             domoticz.log('TV Mimick off!')
             os.execute('curl --header "Content-Type: application/json"   --request PUT  --data '{"status":0}'   http://<my hue ipadress>/api/<my api code>/sensors/55/state')         
           
          end
        end
}
log:

Code: Select all

2020-06-04 13:52:58.977 Error: dzVents: Error: (3.0.1) error loading module 'Script Hue TV mimicking 2' from file '/usr/local/domoticz/var/scripts/dzVents/generated_scripts/Script Hue TV mimicking 2.lua': 
2020-06-04 13:52:58.977 .../dzVents/generated_scripts/Script Hue TV mimicking 2.lua:11: ')' expected near '{' 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Hue formula with openURL

Post by waaren »

johnnyboy1984 wrote: Thursday 04 June 2020 13:54 ok, so i found this other https://www.domoticz.com/forum/viewtopi ... 36&t=31481, and used a part of the script, but still an error in my log:

Code: Select all

2020-06-04 13:52:58.977 Error: dzVents: Error: (3.0.1) error loading module 'Script Hue TV mimicking 2' from file '/usr/local/domoticz/var/scripts/dzVents/generated_scripts/Script Hue TV mimicking 2.lua': 
2020-06-04 13:52:58.977 .../dzVents/generated_scripts/Script Hue TV mimicking 2.lua:11: ')' expected near '{' 
some quote problems..
Can you try:

Code: Select all

return
{
    on =
    {
        devices =
        {
            'test1',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Curl to hue' ,
    },

    execute = function(dz, switch)
        dz.log('TV Mimick ' .. switch.state, dz.LOG_DEBUG)

        local hueCommand = ( switch.state == 'On' and 1 ) or 0
        local curlCommand = 'curl --header "Content-Type: application/json"   --request PUT  --data \'{"status": '  .. hueCommand .. ' }\'   http://<Hue IP>/api/<API CODE>/sensors/55/state'

        os.execute(curlCommand)
        dz.log('curl Commnand: ' .. curlCommand, dz.LOG_DEBUG)

    end
}

If it does not work yet just copy the command you see in the log and try it from the CLI.
curl will show you what's wrong.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johnnyboy1984
Posts: 13
Joined: Thursday 14 March 2019 17:46
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Hue formula with openURL

Post by johnnyboy1984 »

This works!!! Thanks a lot.
I removed the extra loggin now and can use it.

Code: Select all

return 
{
    on =
    {
        devices =
        {
            'test1',
        }
    },

    

    execute = function(dz, switch)
        local hueCommand = ( switch.state == 'On' and 1 ) or 0
        local curlCommand = 'curl --header "Content-Type: application/json"   --request PUT  --data \'{"status": '  .. hueCommand .. ' }\'   http://<my hue ipadress>/api/<my api code>/sensors/55/state'

        os.execute(curlCommand)
        
    end
}
Post Reply

Who is online

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