Skip to content

Instantly share code, notes, and snippets.

@FunDeckHermit
Last active June 25, 2025 17:15
Show Gist options
  • Save FunDeckHermit/959692bcbaccce5d22c774d136613d2d to your computer and use it in GitHub Desktop.
Save FunDeckHermit/959692bcbaccce5d22c774d136613d2d to your computer and use it in GitHub Desktop.
# You should load this driver with load("sunblind") in an autoexec.be file on the root
# Save this file as sunblind.be on the root filesystem
import math
class Sunblind : Driver
static max_time_to_open_ms = 30000
static switch_open_mask = 0x01
static switch_closed_mask = 0x02
var timestamp_millis_activated
var opening_active
var closing_active
var percentage_open
def init()
self.percentage_open = 0.0
end
def calc_diff()
var diff = tasmota.millis() - self.timestamp_millis_activated
var percentage_promile = tasmota.scale_uint(diff, 0, self.max_time_to_open_ms, 0, 10000)
return percentage_promile / 100.0
end
def pub_result()
tasmota.cmd("TelePeriod")
end
def set_power_handler(cmd, idx)
if idx & self.switch_open_mask
self.timestamp_millis_activated = tasmota.millis()
print(f"Sunblind opening from {self.percentage_open}%")
self.opening_active = true
end
if self.opening_active && ((idx & self.switch_open_mask) == 0)
self.percentage_open = math.min(100.0, self.percentage_open + self.calc_diff())
print(f"Sunblind stopped opening at {self.percentage_open}%")
self.opening_active = false
self.pub_result()
end
if idx & self.switch_closed_mask
self.timestamp_millis_activated = tasmota.millis()
print(f"Sunblind closing from {self.percentage_open}%")
self.closing_active = true
end
if self.closing_active && ((idx & self.switch_closed_mask) == 0)
self.percentage_open = math.max(0.0, self.percentage_open - self.calc_diff())
print(f"Sunblind stopped closing at {self.percentage_open}%")
self.closing_active = false
self.pub_result()
end
end
def web_sensor()
import string
var msg = string.format(
"{s}Sunblind percent open{m}%.f %%{e}",
self.percentage_open)
tasmota.web_send_decimal(msg)
end
def json_append()
import string
var msg = string.format(",\"Sunblind\":{\"Percent_Open\":%.f}", self.percentage_open)
tasmota.response_append(msg)
end
end
d1 = Sunblind()
tasmota.add_driver(d1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment