im trying to create a app/driver to check for on switches and the display it in a tile
ive kind of got it working but the attribute display in the driver i would like a device on each line ive tried '\n'. how do i arrange/split the data onto individual lines?
app
void handler(evt) {
String whichOn = ""
switches?.each{
if(it.currentSwitch == "on") {
whichOn += "$it, "
}
}
log.debug whichOn
dataDev.parse(whichOn)
whichOn = null
}
Driver
metadata {
definition (name: "Whats On Tile", namespace: "Mark-C-UK", author: "Mark C") {
capability "Actuator"
attribute "SwitchData", "String"
command "parse"
}
preferences() {
input "logEnable", "bool", title: "Enable logging", required: true, defaultValue: true
}
}
void parse(data){
sendEvent(name: "SwitchData", value: data, displayed: true)
}
Ive tried passing it as a list as well as string
A bit of a hack, but I think you could format it as an html list to get it to render the way you want, e.g.:
void handler(evt) {
String whichOn = "<ul>"
switches?.each{
if(it.currentSwitch == "on") {
whichOn += "<li>${it}</li>"
}
}
whichOn += "</ul>"
log.debug whichOn
dataDev.parse(whichOn)
whichOn = null
}
Or use a paragraph <p>
, with line break <br>
at the end of each one
1 Like
You could check out something like the Hub Info driver. I expect @thebearmay is using html table cells. I'm pretty sure I've seen them used in other drivers like Tile Master.
2 Likes
that seemed to get it
void handler(evt) {
String whichOn = "<ul>"
//List whichOn = []
switches?.each{
if(it.currentSwitch == "on") {
whichOn += "<p>${it}<br>"
//whichOn << it
}
}
whichOn += "</ul>"
log.debug whichOn
dataDev.parse(whichOn)
whichOn = null
1 Like
Several options. For a single colum, If you want to use the \n enclose the text in a <pre></pre> tag (probably the smallest amount of characters used) , the <p></p> and <ul></ul> default to adding the newline normally but you need to enclose each item in the list.
Preformatted Line 1 Line 2
paragraph 1
paragraph 2
1 Like
thanks \n works now.
ive just noticed i need to sort the list alphabetically now!
error java.lang.IllegalArgumentException: Command 'sorted' is not supported by device 258. on line 49 (method handler)
So it wants to be difficult, eh? Well maybe try something like:
List switchList = (List) switches
switchList.sorted().each {
compVal = it
switches?.each{
if(compVal == it.name && it.currentSwitch == "on") {
whichOn += "<p>${it}<br>"
//whichOn << it
}
…
this bit is going wrong, No such property: currentSwitch for class: java.lang.String on line
Should it be a
switches.displyname.SortedSet().each{
if(it.currentSwitch == "on") {
whichOn += "<p>
Or use .toSorted()
Very confusing all these sorting methods
OK got this to kinda work
Map switchesMap = [:]
switches?.each{
switchesMap << ["${it.device.displayName}":"${it.currentSwitch}"]
}
switchesMap.sort().each{
if (it.value == "on"){
whichOn += "${it}\n"
}
}
1 Like
bobbles
December 19, 2021, 7:44pm
13
This looks interesting.
Are you going to formally publish?
try it, just trying to keep it as small and light weight as possible
App
Summary
definition(
name: "Whats on",
namespace: "Mark-c-uk",
author: "Mark C",
description: "check what is on",
category: "Convenience",
iconUrl: "",
iconX2Url: ""
)
preferences {
page(name: "mainPage")
}
Map mainPage() {
dynamicPage(name: "mainPage", title: "Setup", uninstall: true, install: true) {
section {
input "appName", "text", title: "Name this instance whats on", submitOnChange: true
if(appName) app.updateLabel(appName)
input "switches", "capability.switch", title: "Switches to check", multiple: true
input "therms", "capability.thermostat", title: "Therm to check", multiple: true
paragraph "For the trigger use a Virtual Switch with auto-off enabled, turning it on checks switches above"
input "trigger", "capability.switch", title: "Trigger switch"
input "dataName", "text", title: "Enter a name for the Data Device", required:true, submitOnChange:true
paragraph "A device will automaticaly be created for you as soon as you click outside of this field"
if(dataName) createDataChildDevice()
input "dataDev", "capability.actuator", title: "Data Device", multiple: false
}
}
}
void updated() {
unsubscribe()
initialize()
}
void installed() {
initialize()
}
void initialize() {
subscribe(trigger, "switch.on", handler)
runIn (5, handler)
}
void handler(evt) {
String whichOn = '<pre align="left">'
/*
switches?.each{
if(it.currentSwitch == "on") {
whichOn += "${it}\n"
//whichOn << it
}
}
*/
whichOn += '<p>Therm</p>'
therms?.each{
if(it.thermostatMode != "off") {
whichOn += "${it}\n"
//whichOn << it
}
}
/*
List switchList = (List) switches
switchList?.displayName.sort().each {
compVal = it
log.debug it
switches?.each{
/*
}
*/
Map switchesMap = [:]
switches?.each{
switchesMap << ["${it.device.displayName}":"${it.currentSwitch}"]
}
whichOn += '<p>Switches</p>'
switchesMap.sort().each{
if (it.value == "on"){
whichOn += "${it}\n"
}
}
whichOn += "</pre>"
//log.debug whichOn
dataDev.parse(whichOn)
whichOn = null
switchesMap = null
//log.debug switches switches.currentSwitch
//switches?.device.displayName.toSorted().each{
// log.debug "${it} " //yes
// log.debug "${it} ${it.currentSwitch}" //errorgroovy.lang.MissingPropertyException: No such property: currentSwitch for class: java.lang.String on line 82 (method handler)
//}
}
def createDataChildDevice() {
log.debug "In createDataChildDevice"
String statusMessageD = ""
if(!getChildDevice(dataName)) {
log.debug "In createDataChildDevice - Child device not found - Creating device: ${dataName}"
try {
addChildDevice("Mark-C-UK", "Whats On Tile", dataName, 1234, ["name": "${dataName}", isComponent: false])
log.debug "In createDataChildDevice - Child tile device has been created! (${dataName})"
statusMessageD = "<b>Device has been been created. (${dataName})</b>"
} catch (e) { if(logEnable) log.debug "unable to create data device - ${e}" }
} else {
statusMessageD = "<b>Device Name (${dataName}) already exists.</b>"
}
return statusMessageD
}
Driver
Summary
/*
*/
metadata {
definition (name: "Whats On Tile", namespace: "Mark-C-UK", author: "Mark C") {
capability "Actuator"
attribute "SwitchData", "String"
command "parse"
}
preferences() {
input "logEnable", "bool", title: "Enable logging", required: true, defaultValue: true
}
}
void parse(data){
//log.debug data
//def (aMap, splitit) = data.split(',')
sendEvent(name: "SwitchData", value: data, displayed: true)
}
bobbles
December 19, 2021, 8:22pm
15
I like it.
Nice and straightforward with a quick look up to see what is on.
Well done.
All this malarkey is way behind me and I'm too long in the tooth now to try!!