import string

var current = 0

tasmota.add_rule('ADS1115#A0',
  def (value,trigger,data)
    if value < 0
      #print("A0:",value*-1)
      current = value*-1
    elif value > 0
      #print("A0:",value)
      current = value
    end
  end)

import mqtt
import string

# Function to publish the sensor data
def publish_my_sensor_data()

    var current_timestamp = tasmota.rtc()["local"] # Get current epoch time
    var formatted_time = tasmota.strftime("%Y-%m-%dT%H:%M:%S",current_timestamp)
    var topic = string.format("tele/%s/SENSOR", tasmota.cmd("Topic", true)["Topic"])
    var payload = string.format("{\"Time\":\"%s\",\"SCT013\":{\"CURRENT\":%d}}", formatted_time,current) 
    mqtt.publish(topic, payload) # Publish the message
    print("Published to " + topic + ": " + payload)
end

# You can call this function at intervals or based on an event
# Example: call every 10 seconds
tasmota.add_cron("*/5 * * * * *", publish_my_sensor_data)


