I am trying to make my own device and got to a problem where i try to find a solution, but cant find anything that helps.
I want to have a select input, where 2 options for the mode exist, “Automatic” and “Manual”. If in Automatic mode and its inactive, the change to Manual should be possible. If in Manual mode and its inactive, the change to Automatic should be possible. But, when a manual activity is active, the change should be prevented,s ame for Automatic, change should be prevented when Automatic is active.
What i tried:
script which is activated on_value. In that script the value is checked, if its Manual and any automatic is active, switch back to Automatic, same the other way around.
Other aproach was a second mode selector, one i call “set_mode” and the other one “running_mode”. The running_mode counts for activating manual activity or also automatic activity. On value change the script checks if they are different, if so, then either switching the running mode to the value of set mode or switching the set mode back.
A Part of the second aproach works, i can change the running_mode from the script, but i can not change the select value from the set_mode. This is wired, as these are the identical values.
The part of the code for the mode:
select:
- platform: template
name: "Set Mode"
id: set_mode
options:
- "Automatic"
- "Manual"
initial_option: "Automatic"
optimistic: True
on_value:
then:
- script.execute:
id: check_mode
- platform: template
name: "Running Mode"
id: running_mode
options:
- "Automatic"
- "Manual"
initial_option: "Automatic"
optimistic: True
script:
- id: check_mode
then:
lambda: !lambda |-
auto set_mode_state = id(set_mode).state.c_str();
auto running_mode_state = id(running_mode).state.c_str();
ESP_LOGD("CHECK-MODE", "New Mode: %s", set_mode_state);
ESP_LOGD("CHECK-MODE", "Running Mode: %s", running_mode_state);
if (strcmp(set_mode_state, running_mode_state) != 0) {
// ---> running_mode != set_mode
if (strcmp(set_mode_state, "Manual") == 0) {
// ---> running_mode = Automatic
// ---> set_mode = Manual
ESP_LOGD("CHECK-MODE", "New Mode: %s", set_mode_state);
if ((id(irrigation_area_1).is_running() == 1) or (id(irrigation_area_2).is_running() == 1)) {
ESP_LOGD("CHECK-MODE", "Automatic irrigation running, cant switch mode");
// ---> need to set "set_mode" back to Automatic
auto call = id(set_mode).make_call();
call.set_option("Automatic");
call.perform();
} else{
ESP_LOGD("CHECK-MODE", "No automatic irrigation running");
// ---> need to set "running_mode" to Manual
//id(running_mode).publish_state("Manual");
auto call = id(running_mode).make_call();
call.set_option("Manual");
call.perform();
}
} else {
// ---> running_mode = Manual
// ---> set_mode = Automatic
ESP_LOGD("CHECK-MODE", "New Mode: %s", set_mode_state);
if ((id(relay_zone_1).state == 0) and (id(relay_zone_2).state == 0) and (id(relay_zone_3).state == 0) and (id(relay_zone_4).state == 0) and (id(relay_zone_5).state == 0) and (id(relay_zone_6).state == 0) and (id(relay_zone_7).state == 0) and (id(relay_zone_8).state == 0)) {
ESP_LOGD("CHECK-MODE", "No manual irrigation running, switching mode");
// --> neet to set running_mode to Automatic
//id(running_mode).publish_state("Automatic");
auto call = id(running_mode).make_call();
call.set_option("Automatic");
call.perform();
} else {
ESP_LOGD("CHECK-MODE", "Manual irrigation running, cant switch mode");
// ---> need to set "set_mode" back to Manual
//id(set_mode).publish_state("Manual");
auto call = id(set_mode).make_call();
call.set_option("Manual");
call.perform();
}
}
} else {
ESP_LOGD("CHECK-MODE", "Mode did not change");
}
There are some debug messages where i tried to see whats happening. I see in the log, that the set_mode should be set back to Manual or Automatic, but it doenst change.
Could the reason be, that the trigger is from set_mode and that way it can not be changed?
Another aproach would be to disable changes of “set_mode”, but i couldnt find anything.
Maybe someone can give me thi right direction to find the solution. I am working with ESPHome very short time and it could be the solution what i need.