class MySensorPublisher
var topic
var data
var debug
def init()
import string
import gpio
# from Berry Cookbook
self.topic = string.replace(string.replace(tasmota.cmd('FullTopic',true)['FullTopic'],'%topic%', tasmota.cmd('Topic',true)['Topic']),'%prefix%', tasmota.cmd('Prefix',true)['Prefix3'])+ 'SENSOR'
# create list of pins to be monitored
var gpios = [10,12]
self.data = map()
for pin:gpios
self.data.insert(pin, gpio.digital_read(pin))
end
self.debug = true
end
# change this if you want a slower update
def every_second()
self.checkGpios()
end
def checkGpios()
import gpio
var state = false
var needToPublish = false
for pin:self.data.keys()
state = gpio.digital_read(pin)
if self.data.item(pin) != state || self.debug
needToPublish = true
self.data.setitem(pin, state);
end
end
if needToPublish
needToPublish = false
self.publishSensorData()
end
end
def publishSensorData()
import mqtt
import json
if !self.debug
mqtt.publish(self.topic, json.dump(self.data))
else
print(json.dump(self.data))
end
end
def web_sensor()
import string
var text = ""
for pin:self.data.keys()
text = text + string.format("{s}GPIO%s{m}%s{e}", pin, self.data[pin] == 1 ? "HIGH" : "LOW")
end
tasmota.web_send(text)
end
def json_append()
import json
var msg = "\"GPIOSensor\":" + json.dump(self.data)
tasmota.response_append(msg)
end
end
var mySensorPublisher = MySensorPublisher()
tasmota.add_driver(mySensorPublisher)
Hi Andrew
That’s odd - I’ve just done some testing on one of my esp32 boards. Using 13.1.0.
With the true argument I get no output to the log or mqtt. Without the arg I get the log. i.e. expected behaviour. I tried again with 13.4.0 and got the same, expected, behaviour.
However there is somewhat of a workaround by storing the topic in a variable at boot time and encapsulating the publishing as a command (or in a function anyway). This way, even if it does spit out a log entry it is only once in the lifecycle. Here’s the code I’ve been testing with (trying to emulate your gpio monitoring aim)
class MySensorPublisher
var topic
var data
var debug
def init()
import string
import gpio
self.topic = string.format("tele/%s/SENSOR", tasmota.cmd("topic", true)["Topic"])
# create list of pins to be monitored
var gpios = [10,12]
self.data = map()
for pin:gpios
self.data.insert(pin, gpio.digital_read(pin))
end
self.debug = true
end
# change this if you want a slower update
def every_second()
self.checkGpios()
end
def checkGpios()
import gpio
var state = false
var needToPublish = false
for pin:self.data.keys()
state = gpio.digital_read(pin)
if self.data.item(pin) != state || self.debug
needToPublish = true
self.data.setitem(pin, state);
end
end
if needToPublish
needToPublish = false
self.publishSensorData()
end
end
def publishSensorData()
import mqtt
import json
if !self.debug
mqtt.publish(self.topic, json.dump(self.data))
else
print(json.dump(self.data))
end
end
end
var mySensorPublisher = MySensorPublisher()
tasmota.add_driver(mySensorPublisher)