Using an expression for a device attribute help?


#1

I am using a custom device handler, that stores information in an attribute called “forecastFriday” for example.

I can get this info into webcore,straight away as [device:forecastFriday]. However does any one know if you can do something like this: [device:forecast{$dayofweekname}]

If I do that it returns device:Sunday. Unfortunately not the info stored in the attribute forecastSunday.

I’m just missing on the syntax I think. My other option is to use a case statement for each day of the week. But that is not as straight forward as I would like.

Any suggestions? Thanks


#2

You have the capitalisation of $dayOfWeekName incorrect… but that’s not the problem here… you can’t build expressions inside the device attribute references.

It’s hard for me to test without having your specific DTH installed on my IDE, but try building the attribute name in a variable, and then referencing the variable in the expression.

define
string attributeName = forecast{$dayOfWeekName}

Expression:

[device name:{attributeName}]


#3

Unfortunately, that doesn’t work either. It reruns [deviceName:Sunday]. It’s basically the same as your advanced alarm device. Trying to get the value for the attribute Sunday. From an expression.

Similar trial different device handler


#4

For my alarm device, I use the following expression in a variable to return ‘on’ if todays button is on.

($dayOfWeekName=='Monday'&&([Alarm:Monday])=='on'?'on':($dayOfWeekName=='Tuesday'&&([Alarm:Tuesday])=='on'?'on':($dayOfWeekName=='Wednesday'&&([Alarm:Wednesday])=='on'?'on':($dayOfWeekName=='Thursday'&&([Alarm:Thursday])=='on'?'on':($dayOfWeekName=='Friday'&&([Alarm:Friday])=='on'?'on':($dayOfWeekName=='Saturday'&&([Alarm:Saturday])=='on'?'on':($dayOfWeekName=='Sunday'&&([Alarm:Sunday])=='on'?'on':'off')))))))

Does this help?


#5

This is more suited for your use case… Just rename ‘Device’ to the name of your device:

($dayOfWeekName=='Monday'?[Device:weatherMonday]:($dayOfWeekName=='Tuesday'?[Device:weatherTuesday]:($dayOfWeekName=='Wednesday'?[Device:weatherWednesday]:($dayOfWeekName=='Thursday'?[Device:weatherThursday]:($dayOfWeekName=='Friday'?[Device:weatherFriday]:($dayOfWeekName=='Saturday'?[Device:weatherSaturday]:($dayOfWeekName=='Sunday'?[Device:weatherSunday]:'error')))))))


#6

Thnks. I’m not very good at terinay statements. Lol