summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2022-05-26 08:43:49 +0200
committerDaniel Friesel <derf@finalrewind.org>2022-05-26 08:43:49 +0200
commit0367bc298f7f5f060bb6c7b9fcf5ce261cdaddb6 (patch)
treeb71a00f48b760ef5bb99a21d782657b5ddd91ea2
parent102dd0072d34fa081f5861148be61c0c3739827c (diff)
Expose device temperature via MQTT and InfluxDB
-rw-r--r--init.lua13
1 files changed, 8 insertions, 5 deletions
diff --git a/init.lua b/init.lua
index b6d7175..05e4c67 100644
--- a/init.lua
+++ b/init.lua
@@ -63,8 +63,8 @@ function uart_callback(data)
return
end
- local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":"%d"}', wifi.sta.getrssi(), mh_z19.co2)
- local influx_str = string.format("co2_ppm=%d", mh_z19.co2)
+ local json_str = string.format('{"rssi_dbm":%d,"co2_ppm":"%d","temperature_celsius":%d}', wifi.sta.getrssi(), mh_z19.co2, mh_z19.temp)
+ local influx_str = string.format("co2_ppm=%d,temperature_celsius=%d", mh_z19.co2, mh_z19.temp)
if not publishing_mqtt then
watchdog:start(true)
@@ -101,12 +101,15 @@ function hass_register()
local hass_device = string.format('{"connections":[["mac","%s"]],"identifiers":["%s"],"model":"ESP8266 + MH-Z19","name":"MH-Z19 %s","manufacturer":"derf"}', wifi.sta.getmac(), device_id, chip_id)
local hass_entity_base = string.format('"device":%s,"state_topic":"%s/data","expire_after":120', hass_device, mqtt_prefix)
local hass_co2 = string.format('{%s,"name":"CO₂","object_id":"%s_co2","unique_id":"%s_co2","device_class":"carbon_dioxide","unit_of_measurement":"ppm","value_template":"{{value_json.co2_ppm}}"}', hass_entity_base, device_id, device_id)
+ local hass_temp = string.format('{%s,"name":"Temperature","object_id":"%s_temp","unique_id":"%s_temp","device_class":"temperature","unit_of_measurement":"°c","value_template":"{{value_json.temperature_celsius}}","entity_category":"diagnostic"}', hass_entity_base, device_id, device_id)
local hass_rssi = string.format('{%s,"name":"RSSI","object_id":"%s_rssi","unique_id":"%s_rssi","device_class":"signal_strength","unit_of_measurement":"dBm","value_template":"{{value_json.rssi_dbm}}","entity_category":"diagnostic"}', hass_entity_base, device_id, device_id)
mqttclient:publish("homeassistant/sensor/" .. device_id .. "/co2/config", hass_co2, 0, 1, function(client)
- mqttclient:publish("homeassistant/sensor/" .. device_id .. "/rssi/config", hass_rssi, 0, 1, function(client)
- collectgarbage()
- setup_client()
+ mqttclient:publish("homeassistant/sensor/" .. device_id .. "/temperature/config", hass_temp, 0, 1, function(client)
+ mqttclient:publish("homeassistant/sensor/" .. device_id .. "/rssi/config", hass_rssi, 0, 1, function(client)
+ collectgarbage()
+ setup_client()
+ end)
end)
end)
end