Beginners help with groovy driver

Hi all,
I'm trying to create a device driver that will extract values from node-red message that contains an array of object data and eventually put the data on my dashboard. I have everything worked out to get the data in HE and I can parse out any data point I want. The challenge I have, with my very basic groovy understanding, is that I do not want to just hard code all the attributes and pick out the data, and digging around I cannot find an example on how to setup an array of attributes so I can loop through the message and extract all the data. The array can vary in size based on how many parameters are requested so I need the attribute list to be dynamic, too. If anyone could point me to a few groovy examples I would appreciate it. Thanks.

See the GitHub code. May do what you are looking for:

Thanks, I'll take a look!

Edit: Actually, it's one level higher than what I need, It will report all the attributes that are already available for a device but I'm at the point where I still need to define all the attributes. Node Red is returning many attributes which I need to parse and pass into a attributes so I can act upon and display them in HE.

I have a feeling you may need to call them out specifically in the driver.... I could be wrong, but I have mistakenly tried to store values in an attribute name that is not in the list at the top of the driver and it appears temporarily on the device page and disappears straight away.

We may need a little more info about what you are wanting to do. If you are just wanting to display a simple table or even just single values you may need to try and produce an attribute with html inside and display that on your dashboard. Another option may be to store the json and have a local html file either render it in an iframe, or have the driver produce a static html file when parsing the data and display that file in an iframe.

Thanks for the feedback. I just had a generator installed and there is no integration for it yet. There was enough info around the web where I was able to track down how to interface to the unofficial API. There are hundreds of parameters available. The API allows access to all of them and you need to request whatever parameters you want. I wanted the driver to be able to take whatever params are passed up from NR (which may change if I need to monitor more or less of them), parse them to attributes so I can display on a dashboard and also create notification events (i.e. the oil too hot). Not a big deal, I can call them all out individually and just adjust the driver if need be, I'm trying to learn groovy and it just seemed that there should be more dynamic way to do it.

I hope I am wrong, but I suspect not. My suggestion would be to get a few coming through as attributes of there own setup in the driver. In the meantime someone else may have a bright idea :slightly_smiling_face:

I would also say that being specific about the attributes you include can have some benefits in that you can write the driver and any associated code or rules with the knowledge that they will exist.

1 Like

Yea, it's looking that way; no biggie. Thanks!

No worries. This does go against my last comment slightly, but you can define attributes in the list for the driver, but you don't have to populate them.

1 Like

@wecoyote5 Haven’t found a good way of dynamically creating attributes that stick around long enough to populate a dashboard. Some options off the top of my head:

  • bring back the NR values and stick them in a generic attribute for later parsing by a rule or an app.
  • store them to state and bring them back using a command to a generic attribute https://raw.githubusercontent.com/thebearmay/hubitat/main/dynAttribute.groovy
  • use the driver to parse them and store them as hub variables (probably the best idea). I have some code in an app that I could easily port down to a driver if you like (actual variable creation would have to use the hubitat UI but the driver could control everything the update after that).
1 Like

@thebearmay, thanks... sure, I'm for trying anything to better learn the platform. If you have a link to that app code then please forward it on, thanks again!

Well they made it a little harder. Early on you could set and get a variable from a driver now can only do it from an app, but no fear I just split the code into a controller app and a device driver. Import the app into the Apps Code section and the Driver into the Driver Code section.

App: https://raw.githubusercontent.com/thebearmay/hubitat/main/apps/varController.groovy
Driver: https://raw.githubusercontent.com/thebearmay/hubitat/main/varCntrl.groovy

Create a virtual device using the driver (Devices|Add Device|Virtual).

Create a new user app (Apps|Add User App), click done, reenter the app and select the device you created:

Create your hub variables at Settings|Hub Variabes

After all of that, add the device to MakerAPI and NodeRed and you should be able to use the Hubitat Controller Node to push or retrieve data.

image

To set variable the arguments are: VariableName, set, Value
To get a variable value the arguments are: VariableName, get
Value for the get will be returned in the varReturn attribute

1 Like

Thanks! This'll give me something to play with over the holidays.

1 Like