Access device inputs from app

Hi,

Give this bit of code in a device...

section("Controls") {
input(name:"SomeName", type:"bool", title:"Some Input", displayDuringSetup:true, defaultValue:false)
}

... is there a way to set/get the state of the input via code in the App using the device?

Thanks much.

No, apps, in general, can only look at attributes and commands from a device. (This is part of how Hubitat's "capability" model extracts lower-level device and driver details into these higher-level concepts so that apps can work with any device without knowing details of how it or the driver works.)

One exception is if this is a child device of a parent app; in that case, I believe setting the value "directly" would work (using updateSetting() on the device, the same way you'd do it in a driver), but you could for sure always write a method in the child driver and call it from the parent app, or vice versa, and that method can do whatever you need.

If that's not your situation, you'll need a command to do it.

PS -

Not sure where you got this from (actually, I have some ideas :slight_smile: ) but it's not a parameter for input in Hubitat.

Thanks for the info.

Regards that displayDuringSetup - I saw it somewhere and cannot find it now but it does not apply here.

So I cannot change/view the settings of a Device input from within the App. Got it.

I have a number of settings in the Device that need to be turned on/off and to date I have coded a pair of commands for on and off of each setting.

Is there a better way to do this sort of thing? Perhaps a command that can take a bool parameter?

Thanks.

Do you just want to change the preferences, or are you using them as part of an automation? If the latter, they are likely better done as a command anyway. If the former, the built-in Preference Manager app can help with changing a lot of preferences at once (I know, I said apps can't do this--system apps have rare exceptions).

2 Likes

Changing the various on/off settings are part of controlling the Device and the settings are changed from code in the App.

Using a pair of commands for on/off settings (which most of them are) works but it seems a bit cumbersome.

Is there a way to do it with a single command that takes a bool parameter?

Thanks.

I'm not sure I understand what you're asking. You don't need two different inputs for the same device to send two different commands (like on or off) to it if that's the implication. Sharing more details about what you're doing may be helpful, possibly including code snippets or screenshots of the app.

Let's ignore Inputs for now and focus on Commands.

Imagine a Hubitat Device implementing a Telnet connection to a remote piece of hardware. And a Hubitat App (which uses that Device) processing the data sent from the hardware and sending data to that hardware.

Now for purposes of debugging I want to be able to log the data sent by the Device and/or the data received by the Device.

So I implement four Commands in the Device: TraceSendOn, TraceSendOff, TraceRecvOn, TraceRecvOff.

def TraceSendOn() {
state.traceSend = true
}

def TraceSendOff() {
state.traceSend = false
}

def TraceRecvOn() {
state.traceRecv = true
}

def TraceRecvOff() {
state.traceRecv = false
}

In the Device code I can test the state of the traceSend and traceRecv to control if the data sent is logged and/or the data received is logged.

In the App code I can call the four commands as needed to set the desired state of traceSend and traceRecv as desired.

But having four commands just to set two "flags" to true or false seems rather inelegant.

I was thinking perhaps there is a way to have just one Command accept two parameters something like:
TraceControl("Send", "On")
TraceControl("Send", "Off")
TraceControl("Recv", "On")
TraceControl("Recv", "Off")

Or perhaps I am just overthinking it.

Thanks.

Commands can accept parameters, so what you propose is indeed a possibility.

But are these just random apps and drivers? It sounds like you wrote them both. Would it make sense to have them as parent/child? In that case, you can just call a method from one directly on the other (without the need to "elevate" it into a formal command). If not, the above should still work.

I am coding them both. They are working but I am finding small matters that need fixing.

What does a Command that accepts parameters look like? I tried several things but kept getting errors when saving the file.

I have not researched the parent/child capability. I will add that to my list.

Thanks.

See the docs (or various examples, but this is where it's spelled out): Device Definition | Hubitat Documentation

That was the documentation I was looking at.

I think I have it sorted.

While I can specify this: command "trace", ["string", "string"]

The command code needs this: def trace(String value1, String value2) - string with upper case S.

Thanks - moving ahead once again.

1 Like

Yeah, the method signature needs to match the parameters of the command in terms of number (and type if you specify them in your method signature, though Groovy can use dynamic types if not). I don't think lowercase "string" is anything in Groovy, but String is indeed the class for a plain string data type.

In other words, as you've probably discovered by now, that looks right!

Thanks.

My normal language, Delphi, is case-insensitive.

Working with case-sensitive language can be a bit frustrating especially when sometimes it doesn't seem to be case-sensitive.

In any I have Device commands with parameters working.

Next is understand the feature of parent/child app/device relationships.

Thanks again.

1 Like

You can look for something similar implemented in the Tuya Zigbee Multi-Sensor 4 In 1 driver - the SetPar() command. It accepts 2 parameters- the first one is the preference name and the second is the preference new value. The implementation is not the clearest one, but may give you some ideas.

Thanks for the info.