Jump to content

REd brick openHAB MQTT usage


rwblinn
 Share

Recommended Posts

Hi,

 

as a test tried to setup openHAB MQTT displaying AmbientLight bricklet Illuminance BUT nothing is displayed. Any idea what could be wrong?

 

Items

Number Illuminance "Illuminance [%.0f Lux]" <none> { }
String IlluminanceRaw "IlluminanceRaw" [%s] <none> { mqtt="<[mosquitto:tinkerforge/bricklet/ambient_light/mdh/illuminance:state:default]" }

 

Rule

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

rule "MqttAmbientLightParse"
  when 
    Item IlluminanceRaw changed
  then
var String json = (IlluminanceRaw.state as StringType).toString
logInfo("FILE", json)
//{"illuminance":1354,"_timestamp":1456750088.109141}
var String il = transform("JSONPATH", "$.illuminance", json)
logInfo("FILE", "IL="+il)
var String ts = transform("JSONPATH", "$._timestamp", json)
logInfo("FILE", "TS="+ts)
    	sendCommand(Illuminance,  il)
  end 

 

sitemap

sitemap ambientlightmqtt label="MQTT Illuminance"
{
Frame
{
	Text item=Illuminance
	Text item=IlluminanceRaw
}
} 

 

Link to comment
Share on other sites

By...

Items

Number Illuminance "Illuminance [%.0f Lux]" <none>
String IlluminanceTimeStamp "Illuminance Timestamp [%1$tm/%1$td %1$tH:%1$tM]" <none>
String IlluminanceRaw "IlluminanceRaw [%s]" <none> { mqtt="<[mosquitto:tinkerforge/bricklet/ambient_light/mdh/illuminance:state:default]" }

Sitemap

sitemap ambientlightmqtt label="MQTT Illuminance"
{
Frame
{
	Text item=Illuminance
	Text item=IlluminanceTimeStamp
	Text item=IlluminanceRaw
}
} 

Rules = UNIX timestamp conversion needs to be improved.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

rule "MqttAmbientLightParse"
when 
Item IlluminanceRaw changed
then
//Convert the raw data to a string
var String data = (IlluminanceRaw.state as StringType).toString

//Parse the JSON string
// { "illuminance":1354,"_timestamp":1456750088.109141 }

//Illuminance
var String il = transform("JSONPATH", "$.illuminance", data)
var iln = new Double(il)
iln = iln * 0.1
    	Illuminance.postUpdate( iln)

//Timestamp
var String ts = transform("JSONPATH", "$._timestamp", data)
        //THIS NEEDS TOBE IMPROVED
ts = ts.replaceAll("E9", "")
ts = ts.replaceAll("[.]", "")
//IlluminanceTimeStamp.postUpdate(ts)
var DateTime dt = new DateTime(Long::parseLong(ts) * 1000L)
IlluminanceTimeStamp.postUpdate(new DateTimeType(dt.toString))
end

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...