Use string from Harmony API in LUA Topic is solved

Moderator: leecollings

Post Reply
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Use string from Harmony API in LUA

Post by Ingmar »

Hi guys,

I'd like to use a string from an API in my local network. If i go to 192.168.1.10:8080/api/devices/harmony/show it will show the following data:

Code: Select all

[
{
hub: "HarmonyHub",
activity: {
label: "PowerOff",
suggestedDisplay: "Default",
id: -1,
activityTypeDisplayName: "Default",
controlGroup: [ ],
tuningDefault: false,
fixit: {
47822007: {
id: "47822007",
power: "OFF",
alwaysOn: false,
relativePower: false
},
type: "PowerOff",
icon: "Default"
}
}
]
How do I get the string of "label" in a variable? So in the above example code, I'd like to put "PowerOff" in a variable.

I've searched on the internet for this, but I can't figure it out. Maybe i'm searching for the wrong thing, because I'm not a coder. This shouldn't be too difficult, right?!
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Re: Use string from Harmony API in LUA

Post by Ingmar »

Nobody has an idea?
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Use string from Harmony API in LUA

Post by dannybloe »

It's not difficult if you are a coder ;)

Somehow you have to make a script of some sorts that does the http request and put the results in Domoticz. You can do this with Lua (preferably dzVents). I'm sure there are a lot of example scripts to be found here in the forum that show you how you can do this is such a way that it doesn't block the event system. But I don't know of a way for a non-coder.

Just learn to code, the basics are not that hard and it is a lot of fun once you get the hang of it :D
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Re: Use string from Harmony API in LUA

Post by Ingmar »

It's not difficult if you are a coder ;)
Heheh, that's what I thought!

Thanks for your reply. I already spent several hours searching for an example piece of code that I can edit but found nothing that really fitted the bill. Maybe i'm not searching for the correct terms of what I want?! I'll search for "http request" next ;)
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Use string from Harmony API in LUA

Post by Nautilus »

Try searching for something like "lua read json", assuming the ouput is json? If so, something like this should work:

Code: Select all

json = (loadfile '/home/pi/domoticz/scripts/lua/JSON.lua')()
    file = assert(io.popen('curl "http://192.168.1.10:8080/api/devices/harmony/show"'))  
        
    raw = file:read('*all')
    file:close()   
    deviceinfo = json:decode(raw)
    
    label = deviceinfo.activity.label
This of course is not tested in any way but might give you a hint where to look into. Note that you need the json.lua file to be in the directory you specify in the script, Domoticz should be shipped with it though: https://github.com/domoticz/domoticz/bl ... a/JSON.lua
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Re: Use string from Harmony API in LUA

Post by Ingmar »

Thank you Nautilus, that was of great help! :D

I basically got done what I wanted to do. The only problem I encountered, is that my JSON data starts with a "[" and then the script doesn't read the value . The log gives the error "EventSystem: in Fetch Harmony status: [string "..."]:11: attempt to index field 'activity' (a nil value)". And line 11 is:

Code: Select all

label = deviceinfo.activity.label
I've made a copy of the output data into a separate file to test with and I removed the "[" and the "]" at the beginning and the end of the file and then it works perfectly. So somehow the system struggles to read the data when the whole thing is in an array....

I found a URL that has JSON data in a similar way (see https://api.github.com/users/mralexgray/repos). How would you extract the data from the field "login"? Because deviceinfo.owner.login doesn't work...

Any thoughts on how to solve this?
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Use string from Harmony API in LUA

Post by Nautilus »

Ingmar wrote: Tuesday 31 October 2017 14:11 Thank you Nautilus, that was of great help! :D

I basically got done what I wanted to do. The only problem I encountered, is that my JSON data starts with a "[" and then the script doesn't read the value . The log gives the error "EventSystem: in Fetch Harmony status: [string "..."]:11: attempt to index field 'activity' (a nil value)". And line 11 is:

Code: Select all

label = deviceinfo.activity.label
I've made a copy of the output data into a separate file to test with and I removed the "[" and the "]" at the beginning and the end of the file and then it works perfectly. So somehow the system struggles to read the data when the whole thing is in an array....

I found a URL that has JSON data in a similar way (see https://api.github.com/users/mralexgray/repos). How would you extract the data from the field "login"? Because deviceinfo.owner.login doesn't work...

Any thoughts on how to solve this?
Glad there was some help from this (or that is how I interpreted it :)). When you have several records you need to refer from which specific record you want to extract the data from. If you want to retrieve the first it would be something like: deviceinfo.owner[0].login

Or loop through each record to test for certain key value. Sorry, not an expert in this in any way so not a lot more I can help with in this area...
Ingmar
Posts: 51
Joined: Sunday 04 May 2014 1:34
Target OS: NAS (Synology & others)
Domoticz version:
Location: The Netherlands
Contact:

Re: Use string from Harmony API in LUA

Post by Ingmar »

It took me several hours of searching and trying, but I managed to solve it! The trick was: deviceinfo[1].activity.label

To complete this topic for future reference:
- I'm running Domoticz and HA bridge on my Synology NAS
- I have a Harmony hub connected to Domoticz via HA bridge (as native connection in Domoticz settings>hardware seemed to create an unstable system for some people, and I need HA bridge anyway so that I can manage activities via Alexa). This means i can switch my activities on and off in Domoticz via HA bridge.
- The problem with this is that when I use the harmony handheld remote control to start or stop an activity, my switches don't get updated. So when i used Domoticz to switch the activity "watch TV" on but used the handheld remote control to end the activity, the activity is still shown in Domoticz as "on".
- To solve this I used dummy switches and read out the harmony hub status via the LUA script i started this topic for. The reason for using the dummy switches is because you can then change the device status without it sending the switch command. In order to set this up, do as follows:
  • Starting point is that you have Domoticz running, HA bridge running, and your harmony activities are in Domoticz as on/off switches.
  • Create a dummy switch for every activity (as an example I will use the "watch TV" activity), in this case a dummy switch called "Watch TV slave".
  • Then edit the original "Watch TV" switch, and define a sub/slave device, in this case you define "Watch TV slave" as a sub/slave of the "Watch TV" switch. Repeat this for your other activity switches
  • Go to settings > more options > events and create a new LUA time event. Remove all the standard example code and paste the following code in there:

    Code: Select all

    commandArray = {}
    
    json = (loadfile '/usr/local/domoticz/samplescripts/lua/JSON.lua')() -- check where your JSON.lua file is located on your sytem
        file = assert(io.popen('curl "http://192.168.1.10:8080/api/devices/harmony/show"'))  -- Change this to your IP:port of HA bridge
        raw = file:read('*all')
        file:close()   
        deviceinfo = json:decode(raw)
        
        label = deviceinfo[1].activity.label
    
    
    print("The current Harmony Hub activity is: " .. label)
    
    if label == "Watch TV" then
       commandArray['Watch TV slave']='On'
       commandArray['Watch DVD slave']='Off'
     elseif label == "Watch DVD" then
       commandArray['Watch TV slave']='Off'
       commandArray['Watch DVD slave']='On'
     elseif label == "PowerOff" then
       commandArray['Watch TV slave']='Off'
       commandArray['Watch DVD slave']='Off'
      else
         print("Hub status UNKNOWN OR NOT IN EVENT SCRIPT")
    end
    
    return commandArray
  • Make sure you changed the JSON.lua location and HA bridge IP and port to your setup. Tick "event active" and press save.
The slave switches should now update according to the current status of the Harmony Hub. You can check the log (in Domoticz > settings > log) to see if there are any errors or what the label of the current activity is.

The script will update every minute, so there is a delay of max 60 seconds before the status of the switches will be updated. Maybe this can be done in a more clever way (that the script runs when the label changed for example) but i don't have the skills for that. Suggestions and comments are always welcome :mrgreen:


Many thanks to Nautilus for helping me in the right direction!

Gr. Ingmar
Synology NAS with stable release, AEON Z-wave, RFXCOM, Toon, Echo Dot, HAbridge, HarmonyHUB, Dashticz.
Post Reply

Who is online

Users browsing this forum: Michel13 and 1 guest