libretiny-eu / libretiny

PlatformIO development platform for IoT modules
http://docs.libretiny.eu/
MIT License
382 stars 55 forks source link

respond to physical switch even if WiFi isn't connected #207

Closed ntzb closed 7 months ago

ntzb commented 7 months ago

hi, after flashing libretiny-esphome, when there's no WiFi connection (e.g. for some reason the switch fails to connect after a power blackout), the relay is not switchable. how can I set it up, so it listens to the physical switch, even if no WiFi is connected? I'd expect it to act as a dumb switch, if no WiFi is available.

Cossid commented 7 months ago

This isn't really a LibreTiny issue, and without a config, there is no advice that can really be given. All button/binary_sensor/switch/relay components are local by default unless you tie them into a service. Wifi state should not matter for them, though if you lose wifi, the device will likely reboot every 15 minutes if HA doesn't connect to it by default.

ntzb commented 7 months ago

I know it could be esphome related, but I could only find https://www.reddit.com/r/homeassistant/comments/12dmyrh/help_needed_for_esphome_to_work_without_home/ which points to esphome's Binary Sensor Automation, but since it's working when WiFi is connected, I don't think I can should override on_press..

here's my yaml, it's the extracted/converted yaml produced after using tuya-cloudcutter, and after I calibrated the current/power etc:

esphome:
  name: "local-smart-plug"

bk72xx:
  board: generic-bk7231n-qfn32-tuya

logger:

web_server:

captive_portal:

mdns:

api:
  password: ""

ota:
  password: ""

wifi:
  ssid: SSID
  password: PASSWORD
  ap:

button:
  - platform: restart
    name: Restart

debug:
  update_interval: 30s

text_sensor:
  - platform: debug
    reset_reason:
      name: Reset Reason
  - platform: libretiny
    version:
      name: LibreTiny Version

sensor:
  - platform: uptime
    name: Uptime
  - platform: hlw8012
    model: BL0937
    cf_pin:
      number: P7
      inverted: true
    cf1_pin:
      number: P6
      inverted: true
    sel_pin:
      number: P24
      inverted: true
    current:
      name: BL0937 Current
      filters:
        - multiply: 0.45
        - lambda: if (x < 0.01) {return 0;} else {return x;}
    voltage:
      name: BL0937 Voltage
    power:
      name: BL0937 Power
    energy:
      name: BL0937 Energy
    voltage_divider: 820
    current_resistor: 0.00103 ohm
    update_interval: 3s

binary_sensor:
  - platform: gpio
    id: binary_switch_1
    pin:
      number: P10
      inverted: true
      mode: INPUT_PULLUP
    on_press:
      then:
        - switch.toggle: switch_1

switch:
  - platform: gpio
    id: switch_1
    name: Relay 1
    pin: P26
    on_turn_on:
      - light.turn_on: light_status
    on_turn_off:
      - light.turn_off: light_status

light:
  - platform: status_led
    id: light_status
    pin:
      number: P8
      inverted: true
Cossid commented 7 months ago

That should always work locally, regardless of wifi state. Are you sure you weren't in a reboot (which happens every 15 minutes when not conntected with api: present)? Also, every 10 reboots (if not marked successful), if it still can't connect, it will fallback to safemode for a period of time (and safemode, the configuration of buttons and relays is ignored). Again, that is all stock ESPHome functionality.

You might want to consider changing reboot_timeout to 0.

ntzb commented 7 months ago

I'll try setting the reboot_timeout to 0s. btw did you mean the one in wifi or the one in api?

I always catch it after more than 15 minutes and probably more than 10 reboots, so it's possible it's in safe mode ('ll definitely watch out for that next time), but then I unplug and replug it (even multiple times), and still it will not respond to the button press unless the WiFi is connected. does unplugging it resets the 10-reboots-to-safe-mode-counter? if not, how should I exit the safe mode? is there a way to boot normally, maybe holding the switch button while plugging in the device? can it be configured?

Cossid commented 7 months ago

For the case you're describing, you might need it in both places. (or you could change it to a much higher value)

does unplugging it resets the 10-reboots-to-safe-mode-counter? if not, how should I exit the safe mode?

It depends, it's based off of when it last set the successful boot flat, not sure when it does that in the case of no wifi/rebooting for timeouts.

is there a way to boot normally, maybe holding the switch button while plugging in the device? can it be configured?

Sure, you can configure reboot to a button press. Not quite sure what you mean here, as if you're plugging the device in, it's already rebooting. I'm unsure if you can override the safemode count. You'd need to ask ESPHome support that question.

ntzb commented 7 months ago

by normally I meant forcing a reboot to normal operation (vs safe mode) even within the timeframe of the safe mode.

thanks for your help! I'll close this for now, and continue with esphome support.