Skip to content

Instantly share code, notes, and snippets.

@gacha
Last active January 6, 2025 19:16
Show Gist options
  • Save gacha/0e0aa95d5b69623f16f426c84c3ed5eb to your computer and use it in GitHub Desktop.
Save gacha/0e0aa95d5b69623f16f426c84c3ed5eb to your computer and use it in GitHub Desktop.
Fan control with shelly MiniPMG3
var speed, current_speed 1
var fan_state, current_fan_state = 0
var log = true
var fan_air_in = 2
var fan_air_out = 0
var fan_air_in_and_out = 1
var fan_on = 1
var fan_off = 0
var fan_state_url = "http://example.local/api/entities/livingroom_fan.state/state"
var fan_speed_url = "http://example.local/api/entities/livingroom_fan.speed/state"
var fan_airflow_url = "http://example.local/api/entities/livingroom_fan.airflow/state"
function check_power(msg) {
if (is_undefined(msg.delta.apower)) { return }
let apower = msg.delta.apower
speed, fan_state = 1
if(apower >= 100 && apower < 140) {
speed = 1
} else if (apower >= 140) {
speed = 2
} else if (apower < 100) {
fan_state = 0
speed = 1
}
if(fan_state != current_fan_state){
if(fan_state == 1) {
Shelly.call("HTTP.POST", {url: fan_state_url, body: fan_on})
Shelly.call("HTTP.POST", {url: fan_airflow_url, body: fan_air_in})
} else {
Shelly.call("HTTP.POST", {url: fan_state_url, body: fan_off})
Shelly.call("HTTP.POST", {url: fan_airflow_url, body: fan_air_in_and_out})
}
if(log) print("Fan state", fan_state)
current_fan_state = fan_state
}
if(current_speed != speed) {
Shelly.call("HTTP.POST", {url: fan_speed_url, body: speed})
if(log) print("Change speed to", speed)
current_speed = speed
}
if(log) print(apower);
}
function is_undefined(o) {
return typeof o == "undefined";
}
Shelly.addStatusHandler(check_power);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment