I have a Tasmota Smart Meter Reader, at first I had the problem that the Matter integration with Smartthings didn’t work, so I thought I’d just write my own driver and use HTTP requests. That’s what I did. The only problem is that I somehow fail because of the following problems:
- automatic updates → I can only update the values of the electricity meter manually, but actually I want the value to be updated automatically in order to execute routines and have a diagram.
- the diagram only has a division into hours and days, but actually I would also like to have a division into minutes, as is the case with the Aeotec Motion Sensor, for example.
How can I solve these problems?
I’m not yet very familiar with Smartthings drivers, which you can probably tell from the code.
Many thanks in advance!
init.lua
-- require st provided libraries
local capabilities = require "st.capabilities"
local Driver = require "st.driver"
local log = require "log"
-- require custom handlers from driver package
local command_handlers = require "command_handlers"
local discovery = require "discovery"
-----------------------------------------------------------------
-- local functions
-----------------------------------------------------------------
-- this is called once a device is added by the cloud and synchronized down to the hub
local function device_added(driver, device)
log.info("[" .. device.id .. "] Adding new Smart Meter device")
end
-- this is called both when a device is added (but after `added`) and after a hub reboots.
local function device_init(driver, device)
log.info("[" .. device.id .. "] Initializing Smart Meter device")
-- mark device as online so it can be controlled from the app
device:online()
end
-- this is called when a device is removed by the cloud and synchronized down to the hub
local function device_removed(driver, device)
log.info("[" .. device.id .. "] Removing Smart Meter device")
end
-- create the driver object
local smi_driver = Driver("smart_meter", {
discovery = discovery.handle_discovery,
lifecycle_handlers = {
added = device_added,
init = device_init,
removed = device_removed
},
capability_handlers = {
[capabilities.refresh.ID] = {
[capabilities.refresh.commands.refresh.NAME] = command_handlers.refresh,
}
}
})
-- run the driver
smi_driver:run()
command_handlers.lua
local log = require "log"
local capabilities = require "st.capabilities"
local cosock = require "cosock"
local http = cosock.asyncify "socket.http"
local json = require("dkjson")
local command_handlers = {}
function get_sml_current_consumption(value)
-- Ein Muster, um die SML Current consumption zu finden
local pattern = "{s}SML Current consumption {m}(.-) W{e}"
-- Eine Variable, um das Ergebnis zu speichern
local result = nil
-- Eine Schleife, um das Muster im Wert zu suchen
for match in string.gmatch(value, pattern) do
-- Das Ergebnis auf den gefundenen Wert setzen
result = match
-- Die Schleife beenden
break
end
-- Das Ergebnis zurückgeben
return result
end
function command_handlers.refresh(driver, device, command)
log.info("status wird aktuallisiert")
local response = http.request("http://192.168.178.105/?m=t")
local power = get_sml_current_consumption(response)
log.info("Aktueller Verbrauch: "..power.."W")
device:emit_event(capabilities.powerMeter.power({value = tonumber(power), unit = "W"}))
end
return command_handlers
profil.yaml
name: smi.v1
components:
- id: main
capabilities:
- id: powerMeter
version: 1
- id: refresh
version: 1
categories:
- name: CurbPowerMeter