How a Select Template could change restore_mode behavior?

Hi, all!

I have this code

select:
  - platform: template
    id: power_state
    name: "Power-On State"
    options:
      - "On"
      - "Off"
      - "Last State"
    initial_option: "Off"
    restore_value: true
    optimistic: true

switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO12
    id: "relay"
    restore_mode: RESTORE_DEFAULT_OFF

But instead ‘RESTORE_DEFAULT_OFF’, I want to get the value from the selector template. Like… if I select On, I should have ALWAYS_ON to my restore_mode of the switch, for selecting Off, restore_mode should be ALWAYS_OFF and if I select “Last State”, the restore_mode should be “RESTORE_DEFAULT_OFF”

Any help, please?

Thanks!

Which board you have?

a sonoff device with esp8266

You don’t show your whole yaml, but for 8266 flash restore defaults off.

Yes, but I want something else, I want to have the possibility to set this how I want, if I want always off, on, or restoring the last state… :slight_smile:

I have solved the issue.
In a normal way, restore_mode does not accept any template, which made me get rid of this option and use some scripts, some global vars, and now I can change the “restore_mode” behavior…

Thanks anyway :wink:

Not sure if that was meant to be sarcastic or not.

One way that you could help someone else who might have a similar question in the future is to share what you learned.

I know I have learned a lot from things many others have shared, even if what they shared didn’t work. I have also seen many posts where someone asks years later, how did you solve that and there is no response. That is incredibly frustrating.

1 Like

Maybe the emoticon " :wink: " made you think this, but it was by mistake, I wanted to put " :slight_smile: ", and then I didn’t care to change it… I don’t have time to be sarcastic, I just ignore details… I was helped enough to make me try more and more, alone, till I got it, don’t get me wrong " :wink: " ok?

I wanted to share it, but I wanted to test it more and change it to be better for my solution.

In the end, I stopped using globals and I tried a “backstage” selector

So, here is my solution to achieve what I wanted:

...............

esphome:
  name: ${name}
  name_add_mac_suffix: false
  friendly_name: ${friendly_name}
  on_boot:
    priority: 800
    then:
      - delay: 1s
      - lambda: |-
          while (id(power_state).state == "") {
            delay(500);
          }
          if (id(power_state).state == "On") {
            id(relay).turn_on();
          } else if (id(power_state).state == "Off") {
            id(relay).turn_off();
          } else {
              if (id(variable).state == "On") {
                  id(relay).turn_on();
              } else {
                  id(relay).turn_off();
              }
          }

...............

select:
  - platform: template
    id: power_state
    name: "Power-On State"
    options:
      - "On"
      - "Off"
      - "Last State"
    initial_option: "Off"
    restore_value: true
    optimistic: true
  - platform: template
    id: variable
    name: "Variable"
    options:
      - "On"
      - "Off"
    initial_option: "Off"
    restore_value: true
    optimistic: true
    internal: true

switch:
  - platform: gpio
    name: "Power Switch"
    pin: GPIO12
    id: relay
#   restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on:
      then:
        - script.execute: delay_var_on
        - switch.turn_on: relay
        - if:
            condition:
              lambda: 'return id(off_power).state > 0;'
            then:
              - delay: !lambda 'return id(off_power).state * 1000;'
              - select.set:
                  id: variable
                  option: "Off"
              - switch.turn_off: relay
    on_turn_off:
      then:
        - script.execute: delay_var_off
        - switch.turn_off: relay
        - if:
            condition:
              lambda: 'return id(on_power).state > 0;'
            then:
              - delay: !lambda 'return id(on_power).state * 1000;'
              - select.set:
                  id: variable
                  option: "On"
              - switch.turn_on: relay
  - platform: restart
    name: "Restart Device"

script:
  - id: delay_var_on
    mode: restart
    then:
      - delay: 1s
      - select.set:
          id: variable
          option: "On"
  - id: delay_var_off
    mode: restart
    then:
      - delay: 1s
      - select.set:
          id: variable
          option: "Off"