Issues Writing Edge Driver for WIFI Sensors

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:

  1. 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.
  2. 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

Tagging @nayelyz

1 Like

have you looked at her profile recently? :slight_smile:

tagging @AlejandroPadilla

3 Likes

Hi @Hellerqr

for the first question, the easy way to do it automatically is using call_with_delay on the driver something like

device.thread:call_on_schedule(5.0, function ()
  	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)

and with this, the hub will update the values depending on the seconds you define, another option is that your devices work also as server something like this https://github.com/SmartThingsDevelopers/SampleDrivers/blob/main/lightbulb-lan-esp8266/driver/src/server.lua but I’m not very familiar with it sorry :sweat_smile:

for the second question, I think it is more related more with the presentation and how the app generates it.

postdata: thanks for the help @milandjurovic71 @jkp

1 Like

I have now solved the problem by loading a script onto the sensor that sends the current value to the Smartthings Hub once every 5 seconds. I found it easier.
Thanks for the answer anyway.

1 Like