Need advice with final Groovy code - raspberry pi temperature sensor

Hi all,

I have a raspberry pi setup with a digital temperature sensor which is hosting that value to a webpage (public with DNS). I have been trying to write a device handler for a virtual tile to get the temperature every time I refresh it but I am stuck as to how to get the value onto the tile.

This is the code so far:

metadata {
	definition (name: "Pi Simulated Temperature Sensor °C", namespace: "xxx", author: "xxx") {
	capability "Temperature Measurement"
	capability "Sensor"
	capability "Refresh"

    command "setTemperature", ["number"]
	}

	// UI tile definitions
    tiles {
        valueTile("temperature", "device.tempreading", height: 2, width: 3, canChangeIcon: true) {
            state("temperature", label:'${currentValue}°', unit:"C",
                backgroundColors:[
                    [value: 0, color: "#153591"],
                    [value: 7, color: "#1e9cbb"],
                    [value: 15, color: "#90d2a7"],
                    [value: 23, color: "#44b621"],
                    [value: 29, color: "#f1d801"],
                    [value: 35, color: "#d04e00"],
                    [value: 36, color: "#bc2323"]
                ]
            )
        }
        standardTile("refresh", "device.switch", inactiveLabel: false, height: 1, width: 1, decoration: "flat") {
                state "default", label:'', action:"device.refresh", icon:"st.secondary.refresh"
        }        

        main "temperature"
        details(["temperature","refresh"])
    }
}


def refresh() {
	def tempresult = 0
    def params = [
        uri : "http://urltopi.com",
        path: "/temp"
    ]

    try {
        httpGet(params) { resp ->
        tempresult = resp.data
        log.debug tempresult
        
		sendEvent(name:"temperature", value:tempresult)
        }
    } catch (e) {
    log.error "something went wrong: $e"
}

}

The line I am having issues with is:
sendEvent(name:"temperature", value:tempresult)

what should I put as the name?

Since I am interested in the same thing, did you ever solve your problem?

It’s likely because your tile is associated with the "tempreading" attribute, but no event of that name is being sent.

Since you are implementing the Temperature Measurement capability, the attribute should be "temperature":

valueTile("temperature", "device.temperature", height: 2, width: 3, canChangeIcon: true) {
            state("temperature", label:'${currentValue}°', unit:"C",
                backgroundColors:[
                    [value: 0, color: "#153591"],
                    [value: 7, color: "#1e9cbb"],
                    [value: 15, color: "#90d2a7"],
                    [value: 23, color: "#44b621"],
                    [value: 29, color: "#f1d801"],
                    [value: 35, color: "#d04e00"],
                    [value: 36, color: "#bc2323"]
                ]
            )
        }

This is documented more thoroughly in the Tiles and attribute state section of the Tiles docs. Hope that helps!

1 Like

Did you get this working??