Code fragments
This works good:
tasmota.add_rule("hasp#p1b13#event=up", / -> print("Button p1b13 pressed"))
This returns “up”:
tasmota.add_rule("hasp#p1b13#event=up", / event-> print("p1b13 event =", event))
This returns “up”:
tasmota.add_rule("hasp#p1b13#event=up", / val-> print("p1b13 event val =", val))
This crashes, no display at all:
tasmota.add_rule("hasp#p1b13#event=up", / -> print("Button p1b13 value ", p1b13.val))
This crashes, no display at all:
tasmota.add_rule("hasp#p1b13#event=up", / -> print("Arc touched, val = ", global.p1b13.val); global.p1b12.val = global.p1b13.val)
This works good:
tasmota.add_rule("hasp#p1b13#event=up", / -> print("Button p1b13 value ", global.p1b13.val))
Some tests in the Berry console - This works good:
global.p1b13.val = 15
p1b13.val = 15
p1b12.val = 15
p1b13.text = "15 °C"
print("Button p1b13 value ", p1b13.val)) --> results in: "Button p1b13 value 18"
print("Button p1b13 value ", global.p1b13.val)
tasmota.cmd("mem1") --> results in: {'Mem1': '18'}
tasmota.cmd('status 5')
tasmota.cmd('status 7')
Trigger sensors every 2 seconds
The Teleperiod command can not be set lower than 10 seconds. If you need faster updates, this works:
def readsensorsquick()
var s = tasmota.read_sensors()
if (s)
tasmota.publish_rule(s)
end
end
tasmota.add_cron('*/2 * * * * *', readsensorsquick, "read_sensors_quickly")
Inspiration
https://community.home-assistant.io/t/sending-data-from-home-assistant-to-esp32-tasmota-for-local-display/655343/2
Alternative code for thermostat
#----------------------------------------------
Implement the thermostat algorithm.
Setpoint = Mem1.
Actual temp = SHT4X#Temperature.
Trigger SHT4X#Temperature works, but toggles as fast as the temperature measurement = every 5s
To trigger only at TelePeriod time, prefix the sensor with the word Tele-.
Status: Tested - works, triggers every 30s = teleperiod.
No hysteresis needed since the period is 30s.
-----------------------------------------------#
def run_thermostat(value, trigger, msg)
var m1 = tasmota.cmd('Mem1')['Mem1'])
if ((m1 > value) && (global.p1b10.state == 2))
tasmota.cmd('Var1' .. 1)
tasmota.cmd('Power2' .. 1)
global.p1b18.image_recolor_opa = 0
else
tasmota.cmd('Var1' .. 0)
tasmota.cmd('Power2' .. 0)
global.p1b18.image_recolor_opa = 255
end
end
tasmota.add_rule("Tele#SHT4X#Temperature", run_thermostat)
Show an icon on the display.
Touching the icon toggles the icon, and switches between auto and manual:
This goes in pages.json:
{"id":10,"obj":"switch","x":18,"y":70,"w":80,"h":80,"toggle":true,"enabled":true,"bg_opa":0,"bg_color11":"#FFFFFF"}
{"id":14,"obj":"img","x":33,"y":82,"scale":300,"src":"A:/auto.png"}
This goes inautoexec.be:
#----------------------------------------------
toggle the thermostat manual/auto setting
after the img p1b14 has been touched
Status: Tested - works good - but do we need it?
-----------------------------------------------#
def toggleManAuto()
var m
print("Checkmark state -------- : ", global.p1b10.state)
m = global.p1b10.state
print("New checkmark value -------- : ", m)
if (m == 2) # 2 = checked = auto
tasmota.cmd('Rule1 0')
tasmota.cmd("mem2 " .. false)
tasmota.cmd("var1 " .. 0)
tasmota.cmd("power2 " .. 0)
global.p1b14.src = "A:/manualmode.png"
else # false = manual
tasmota.cmd("mem2 " .. true)
global.p1b14.src = "A:/auto.png"
tasmota.cmd('Rule1 1')
end
print("New Mem2 value -------- : ", tasmota.cmd('Mem2')['Mem2'])
end
tasmota.add_rule("hasp#p1b10#event=up", toggleManAuto)
Define new Tasmota commands
#----------------------------------------------
Define a new Tasmote command: setSetpoint
Type in the console: SetSetPoint {"SetPoint":21}
Status: Tested - works OK
-----------------------------------------------#
def setSetpoint(cmd, idx, payload, payload_json)
var value
# does the payload contain a 'SetPoint' field?
if ((payload_json != nil) && (payload_json.find("SetPoint") != nil))
value = int(payload_json.find("SetPoint"))
if ((value <= 26) && (value >= 10))
tasmota.cmd('Mem1 '.. value)
tasmota.resp_cmnd_done()
else
tasmota.resp_cmnd_failed()
end
else
tasmota.resp_cmnd_failed()
end
end
tasmota.add_cmd('SetSetpoint', setSetpoint)
#----------------------------------------------
Define a new Tasmote command: getThermostat
Type in the console: getThermostat
Still missing: the temperature!!!
Status: TOBETESTED
-----------------------------------------------#
def getThermostat()
var setpoint
var stat
setpoint = tasmota.cmd('Mem1 ')['Mem1']
stat = tasmota.cmd('Var1 ')['Var1']
#var s
#s = string.format('{"Thermostat":{"SetPoint":%i, "Status":%i }}', setpoint, stat)
var d
d = string.format('{ "idx" : 501, "svalue1" : %i }', setpoint)
var e
e = string.format('{ "idx" : 309, "nvalue" : %i }', stat)
# tasmota.cmd('Publish ' + getStatTopic("THERMOSTAT") + " " + s)
# mqtt.publish(getStatTopic("THERMOSTAT"), s)
mqtt.publish('domoticz/in', d)
mqtt.publish('domoticz/in', e)
#tasmota.resp_cmnd(s)
tasmota.resp_cmnd_done()
end
tasmota.add_cmd('GetThermostat', getThermostat)
To update a Domoticz thermostat setpoint
This works:
http://corvus:8080/json.htm?type=command¶m=udevice&idx=501&svalue=12
Good example HaspMota -> MQTT
tasmota.add_rule("hasp#p2b15#val", / value -> mqtt.publish('tele/lanbon_1/INFO4', str(value)))
Taken from: https://github.com/arendst/Tasmota/discussions/20222#discussioncomment-7843186
Example of a closure with multiple parameters
tasmota.add_rule("Switch1#state", /value,trigger,data->print(value,trigger,data))
2 Switch1#state {'Switch1': {'State': 2}}
2 Switch1#state {'Switch1': {'State': 2}}
JSONL content that works
{"page":4,"comment":"---------- Page 4 System info ----------"}
{"id":0,"text_font":"montserrat-20"}
{"id": 60,"x": 8,"y": 60, "w%": 100,"obj": "label","text": "Hostname", "text_rule":"StatusNET#Hostname","text_rule_formula":"val","text_rule_format":"Hostname: %s","comment":"Status 5"}
{"id": 61,"x": 8,"y": 80, "w%": 100,"obj": "label","text": "IP", "text_rule":"StatusNET#IPAddress","text_rule_formula":"val","text_rule_format":"IP: %s"}
{"id": 62,"x": 8,"y": 100,"w%": 100,"obj": "label","text": "MQTT","text_rule":"Mqtt#Connected","text_rule_formula":"val","text_rule_format":"MQTT is connected: %s"}
{"id": 63,"x": 8,"y": 120,"w%": 100,"obj": "label","text": "Time","text_rule":"StatusTIM#Local","text_rule_formula":"val","text_rule_format":"Local time: %s","comment":"Status 7"}
{"id": 64,"x": 8,"y": 140,"w%": 100,"obj": "label","text": "Mem1-Setpoint","text_rule":"Mem1#State","text_rule_formula":"val","text_rule_format":"Mem1 = Setpoint: %s"}
{"id": 65,"x": 8,"y": 160,"w%": 100,"obj": "label","text": "Time","text_rule":"StatusSNS#Time","text_rule_formula":"val","text_rule_format":"Sensor time: %s","comment":"Status 10"}
{"id": 66,"x": 8,"y": 180,"w%": 100,"obj": "label","text": "Temp","text_rule":"ESP32#Temperature", "text_rule_formula":"val","text_rule_format":"ESP32 internal temperature: %2.1f °C"}
{"id": 67,"x": 8,"y": 200,"w%": 100,"obj": "label","text": "Temp","text_rule":"SHT4X#Temperature", "text_rule_formula":"val","text_rule_format":"SHT40 temperature: %2.1f °C"}
{"id": 68,"x": 8,"y": 220,"w%": 100,"obj": "label","text": "Temp","text_rule":"SHT4X#Humidity", "text_rule_formula":"val","text_rule_format":"SHT40 humidity: %2.1f %%"}
Comments: