ham/light_buttons: add state machine
This commit is contained in:
parent
ebde803cac
commit
023d911e31
@ -1,22 +1,77 @@
|
|||||||
# light.wohnzimmerbeleuchtung
|
|
||||||
# light.wohnzimmer_deko
|
|
||||||
# light.arbeitszimmerbeleuchtung
|
|
||||||
# light.arbeitszimmer_deko
|
|
||||||
# light.schlafzimmerbeleuchtung
|
|
||||||
|
|
||||||
let
|
let
|
||||||
toggle = light: btn:
|
btn_state = light: btn: halfbright:
|
||||||
|
let
|
||||||
|
maxbright = 255;
|
||||||
|
transition = 0.2; # seconds
|
||||||
|
in
|
||||||
|
# this function implements a simple state machine based on the state and brightness of the light (light must support brightness
|
||||||
{
|
{
|
||||||
alias = "Toggle Light ${light} via ${btn}";
|
alias = "Cycle through states of ${light} via button ${btn}";
|
||||||
trigger = {
|
trigger = {
|
||||||
platform = "state";
|
platform = "state";
|
||||||
entity_id = "sensor.${btn}_click";
|
entity_id = "sensor.${btn}_click";
|
||||||
to = "single";
|
to = "single";
|
||||||
};
|
};
|
||||||
action = {
|
action = {
|
||||||
service = "light.toggle";
|
choose = [
|
||||||
data.entity_id = light;
|
{
|
||||||
data.transition = 0;
|
# state 0: off to half
|
||||||
|
conditions = {
|
||||||
|
condition = "template";
|
||||||
|
value_template = ''{{ states("${light}") == "off" }}'';
|
||||||
|
};
|
||||||
|
sequence = [
|
||||||
|
{
|
||||||
|
service = "light.turn_on";
|
||||||
|
data = {
|
||||||
|
entity_id = light;
|
||||||
|
brightness = halfbright;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# state 1: half to full
|
||||||
|
conditions = {
|
||||||
|
condition = "template";
|
||||||
|
value_template = ''{{ states('${light}') == 'on' and ( ${toString (halfbright - 1)} <= state_attr("${light}","brightness") <= ${toString (halfbright + 1)})}}'';
|
||||||
|
};
|
||||||
|
sequence = [
|
||||||
|
{
|
||||||
|
service = "light.turn_on";
|
||||||
|
data = {
|
||||||
|
entity_id = light;
|
||||||
|
brightness = maxbright;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# state 2: full to off
|
||||||
|
conditions = {
|
||||||
|
condition = "template";
|
||||||
|
# TODO: it seems like the devices respond with brightness-1 , maybe off-by-one somewhere?
|
||||||
|
value_template = ''{{ states("${light}") == "on" and state_attr("${light}","brightness") >= ${toString (maxbright - 1)}}}'';
|
||||||
|
};
|
||||||
|
sequence = [
|
||||||
|
{
|
||||||
|
service = "light.turn_off";
|
||||||
|
data = {
|
||||||
|
entity_id = light;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
];
|
||||||
|
# default: on to off
|
||||||
|
# this works because state 0 checks for "state == off"
|
||||||
|
default = [{
|
||||||
|
service = "light.turn_off";
|
||||||
|
data = {
|
||||||
|
entity_id = light;
|
||||||
|
};
|
||||||
|
}];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
turn_off_all = btn:
|
turn_off_all = btn:
|
||||||
@ -34,11 +89,9 @@ let
|
|||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
services.home-assistant.config.automation = [
|
services.home-assistant.config.automation = [
|
||||||
(toggle "light.arbeitszimmerbeleuchtung" "arbeitszimmer_btn1")
|
# (btn_state "light.arbeitszimmerbeleuchtung" "arbeitszimmer_btn1")
|
||||||
(toggle "light.schlafzimmerbeleuchtung" "schlafzimmer_btn2")
|
(btn_state "light.schlafzimmer_komode_osram" "schlafzimmer_btn2" 128)
|
||||||
(toggle "light.wohnzimmerbeleuchtung" "wohnzimmer_btn3")
|
# (btn_state "light.wohnzimmerbeleuchtung" "wohnzimmer_btn3")
|
||||||
(turn_off_all "arbeitszimmer_btn1")
|
|
||||||
(turn_off_all "schlafzimmer_btn2")
|
(turn_off_all "schlafzimmer_btn2")
|
||||||
(turn_off_all "wohnzimmer_btn3")
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user