esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
285 stars 34 forks source link

Template Switch State Problem #5736

Closed Salvora closed 2 weeks ago

Salvora commented 3 weeks ago

The problem

I have two issues with template switches after updating to 2024.4.1. I don't remember which version I was before since I did not check it before but it was at least 9 months old. After updating

Case 1:

Every time I reboot ESP device, it engages the template switch There is a GPIO binary sensor to get the TV state and an IR transmitter to control the TV. There is a template switch where all of that comes together to create a switch for TV power.

# Monitor TV State
binary_sensor:
  - platform: gpio
    name: "$roomname TV Durumu"
    internal: True
    entity_category: "diagnostic"
    device_class: "power"
    id: emre_tv_power
    pin:
      number: 34 # Input only pin without internal pull-up or pull-down resistors
  # TV Switches
  - platform: template
    name: "$roomname TV Güç"
    id: emre_tv_pswitch
    icon: mdi:power
    device_class: "switch"
    lambda: |-
      if (id(emre_tv_power).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - remote_transmitter.transmit_lg:
          data: 0x20DF10EF
          nbits: 32
    turn_off_action:
      - remote_transmitter.transmit_lg:
          data: 0x20DF10EF
          nbits: 32

For some reason, every time the device reboots, the action is engaged. I did not have this issue pre-update.

Case 2:

Global variable states are not preserved and do not survive the reboots. I highly suspect that this is related to Case 1. I have another template switch that controls the screen of a tm1637 which gets the screen state from a global variable. This global variable is set to restore_value but it is not working.

switch:
  # On/Off Switch for Display
  - platform: template
    name: "$devicename Ekran"
    icon: mdi:monitor
    device_class: "switch"
    entity_category: "config"
    lambda: !lambda |-
      if (id(display_status) == true) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - globals.set:
          id: display_status
          value: "true"
      - component.update: tm1637_display
    turn_off_action:
      - globals.set:
          id: display_status
          value: "false"
      - component.update: tm1637_display
globals:
  - id: display_status
    type: bool
    restore_value: true
    initial_value: "true"

Which version of ESPHome has the issue?

2024.4.1

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2024.4.4

What platform are you using?

ESP32

Board

nodemcu

Component causing the issue

Compopnents/Switch Component/Template Switch

Example YAML snippet

No response

Anything in the logs that might be useful for us?

INFO ESPHome 2024.4.1
INFO Reading configuration /config/esphome/emre-oda-esp32s-devkitv1.yaml...
INFO Generating C++ source...
INFO Compiling app...
Processing emre-oda-esp32s-devkitv1 (board: esp32doit-devkit-v1; framework: espidf; platform: platformio/espressif32@5.4.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
 - framework-espidf @ 3.40406.240122 (4.4.6) 
 - tool-cmake @ 3.16.4 
 - tool-ninja @ 1.7.1 
 - toolchain-esp32ulp @ 2.35.0-20220830 
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
Reading CMake configuration...
Dependency Graph
|-- noise-c @ 0.1.4
Compiling .pioenvs/emre-oda-esp32s-devkitv1/src/main.o
Linking .pioenvs/emre-oda-esp32s-devkitv1/firmware.elf
RAM:   [=         ]   9.9% (used 32368 bytes from 327680 bytes)
Flash: [=====     ]  50.1% (used 919133 bytes from 1835008 bytes)
Building .pioenvs/emre-oda-esp32s-devkitv1/firmware.bin
Creating esp32 image...
Successfully created esp32 image.
esp32_create_combined_bin([".pioenvs/emre-oda-esp32s-devkitv1/firmware.bin"], [".pioenvs/emre-oda-esp32s-devkitv1/firmware.elf"])
Wrote 0xf1ef0 bytes to file /data/build/emre-oda-esp32s-devkitv1/.pioenvs/emre-oda-esp32s-devkitv1/firmware-factory.bin, ready to flash to offset 0x0
========================= [SUCCESS] Took 7.47 seconds =========================
INFO Successfully compiled program.
INFO Connecting to 192.168.25.137
INFO Uploading /data/build/emre-oda-esp32s-devkitv1/.pioenvs/emre-oda-esp32s-devkitv1/firmware.bin (925424 bytes)
Uploading: [============================================================] 100% Done...

INFO Upload took 11.61 seconds, waiting for result...
INFO OTA successful
INFO Successfully uploaded program.
INFO Starting log output from 192.168.25.137 using esphome API
INFO Successfully connected to emre-oda-esp32s-devkitv1 @ 192.168.25.137 in 7.189s
INFO Successful handshake with emre-oda-esp32s-devkitv1 @ 192.168.25.137 in 0.117s
[06:34:37][I][app:100]: ESPHome version 2024.4.1 compiled on Apr 26 2024, 00:00:17
[06:34:37][C][wifi:580]: WiFi:
[06:34:37][C][wifi:408]:   Local MAC: 9C:9C:1F:C7:00:D4
[06:34:37][C][wifi:413]:   SSID: 'EVIOT'[redacted]
[06:34:37][C][wifi:416]:   IP Address: 192.168.25.137
[06:34:37][C][wifi:420]:   BSSID: F6:38:83:25:7C:60[redacted]
[06:34:37][C][wifi:421]:   Hostname: 'emre-oda-esp32s-devkitv1'
[06:34:37][C][wifi:423]:   Signal strength: -61 dB ▂▄▆█
[06:34:37][C][wifi:427]:   Channel: 1
[06:34:37][C][wifi:428]:   Subnet: 255.255.255.0
[06:34:37][C][wifi:429]:   Gateway: 192.168.25.1
[06:34:37][C][wifi:430]:   DNS1: 192.168.25.1
[06:34:37][C][wifi:431]:   DNS2: 0.0.0.0
[06:34:37][C][logger:166]: Logger:
[06:34:37][C][logger:167]:   Level: DEBUG
[06:34:37][C][logger:169]:   Log Baud Rate: 115200
[06:34:37][C][logger:170]:   Hardware UART: UART0
[06:34:37][C][template.number:050]: Template Number 'Emre Oda ESP32S Ekran Parlaklığı (0-7)'
[06:34:37][C][template.number:050]:   Icon: 'mdi:brightness-5'
[06:34:37][C][template.number:051]:   Optimistic: YES
[06:34:37][C][template.number:052]:   Update Interval: 4.0s
[06:34:37][C][template.select:065]: Template Select 'Emre Oda ESP32S Ekran Modu'
[06:34:37][C][template.select:066]:   Update Interval: 60.0s
[06:34:37][C][template.select:069]:   Optimistic: YES
[06:34:37][C][template.select:070]:   Initial Option: Saat & Sıcaklık
[06:34:37][C][template.select:071]:   Restore Value: YES
[06:34:37][C][gpio.binary_sensor:015]: GPIO Binary Sensor 'Emre Oda TV Durumu'
[06:34:37][C][gpio.binary_sensor:015]:   Device Class: 'power'
[06:34:37][C][gpio.binary_sensor:016]:   Pin: GPIO34
[06:34:37][C][template.sensor:022]: Template Sensor 'Free HEAP'
[06:34:37][C][template.sensor:022]:   State Class: 'measurement'
[06:34:37][C][template.sensor:022]:   Unit of Measurement: 'b'
[06:34:37][C][template.sensor:022]:   Accuracy Decimals: 1
[06:34:37][C][template.sensor:022]:   Icon: 'mdi:memory'
[06:34:37][C][template.sensor:023]:   Update Interval: 60.0s
[06:34:37][C][uptime.sensor:031]: Uptime Sensor 'Emre Oda ESP32S Açık Süre'
[06:34:37][C][uptime.sensor:031]:   Device Class: 'duration'
[06:34:37][C][uptime.sensor:031]:   State Class: 'total_increasing'
[06:34:37][C][uptime.sensor:031]:   Unit of Measurement: 's'
[06:34:37][C][uptime.sensor:031]:   Accuracy Decimals: 0
[06:34:37][C][uptime.sensor:031]:   Icon: 'mdi:timer-outline'
[06:34:37][C][template.switch:068]: Template Switch 'Emre Oda ESP32S Ekran'
[06:34:37][C][template.switch:070]:   Icon: 'mdi:monitor'
[06:34:37][C][template.switch:079]:   Device Class: 'switch'
[06:34:37][C][template.switch:091]:   Restore Mode: always OFF
[06:34:37][C][template.switch:057]:   Optimistic: NO
[06:34:37][C][template.switch:068]: Template Switch 'Emre Oda TV Güç'
[06:34:37][C][template.switch:070]:   Icon: 'mdi:power'
[06:34:37][C][template.switch:079]:   Device Class: 'switch'
[06:34:37][C][template.switch:091]:   Restore Mode: always OFF
[06:34:37][C][template.switch:057]:   Optimistic: NO
[06:34:37][C][remote_transmitter:015]: Remote Transmitter...
[06:34:37][C][remote_transmitter:016]:   Channel: 0
[06:34:37][C][remote_transmitter:017]:   RMT memory blocks: 1
[06:34:37][C][remote_transmitter:018]:   Clock divider: 80
[06:34:37][C][remote_transmitter:019]:   Pin: GPIO33
[06:34:37][C][remote_transmitter:022]:     Carrier Duty: 50%
[06:34:37][C][dht:017]: DHT:
[06:34:37][C][dht:018]:   Pin: GPIO18
[06:34:37][C][dht:024]:   Model: DHT22 (or equivalent)
[06:34:37][C][dht:027]:   Update Interval: 10.0s
[06:34:37][C][dht:029]:   Temperature 'Emre Oda DHT22 Sıcaklık'
[06:34:37][C][dht:029]:     Device Class: 'temperature'
[06:34:37][C][dht:029]:     State Class: 'measurement'
[06:34:37][C][dht:029]:     Unit of Measurement: '°C'
[06:34:37][C][dht:029]:     Accuracy Decimals: 2
[06:34:37][C][dht:030]:   Humidity 'Emre Oda DHT22 Nem'
[06:34:37][C][dht:030]:     Device Class: 'humidity'
[06:34:37][C][dht:030]:     State Class: 'measurement'
[06:34:37][C][dht:030]:     Unit of Measurement: '%'
[06:34:37][C][dht:030]:     Accuracy Decimals: 2
[06:34:37][C][restart.button:017]: Restart Button 'Emre Oda ESP32S Restart'
[06:34:37][C][wifi_info:013]: WifiInfo Mac Address 'Emre Oda ESP32S Wi-Fi MAC Address'
[06:34:37][C][wifi_info:013]:   Icon: 'mdi:wifi-marker'
[06:34:37][C][version.text_sensor:021]: Version Text Sensor 'Emre Oda ESP32S Version'
[06:34:37][C][version.text_sensor:021]:   Icon: 'mdi:package-up'
[06:34:37][C][display.tm1637:138]: TM1637:
[06:34:37][C][display.tm1637:139]:   Intensity: 0
[06:34:37][C][display.tm1637:140]:   Inverted: 0
[06:34:37][C][display.tm1637:141]:   Length: 6
[06:34:37][C][display.tm1637:142]:   CLK Pin: GPIO25
[06:34:37][C][display.tm1637:143]:   DIO Pin: GPIO26
[06:34:37][C][display.tm1637:144]:   Update Interval: 0.5s
[06:34:37][C][captive_portal:088]: Captive Portal:
[06:34:37][C][sntp:055]: SNTP Time:
[06:34:37][C][sntp:056]:   Server 1: '192.168.1.1'
[06:34:37][C][sntp:057]:   Server 2: ''
[06:34:37][C][sntp:058]:   Server 3: ''
[06:34:37][C][sntp:059]:   Timezone: 'TRT-3'
[06:34:37][C][mdns:115]: mDNS:
[06:34:37][C][mdns:116]:   Hostname: emre-oda-esp32s-devkitv1
[06:34:37][C][ota:096]: Over-The-Air Updates:
[06:34:37][C][ota:097]:   Address: emre-oda-esp32s-devkitv1.local:3232
[06:34:37][C][ota:100]:   Using Password.
[06:34:37][C][ota:103]:   OTA version: 2.
[06:34:37][C][api:139]: API Server:
[06:34:37][C][api:140]:   Address: emre-oda-esp32s-devkitv1.local:6053
[06:34:37][C][api:142]:   Using noise encryption: YES
[06:34:37][C][wifi_signal.sensor:009]: WiFi Signal 'Emre Oda ESP32S Wi-Fi Sinyal Gücü'
[06:34:37][C][wifi_signal.sensor:009]:   Device Class: 'signal_strength'
[06:34:37][C][wifi_signal.sensor:009]:   State Class: 'measurement'
[06:34:37][C][wifi_signal.sensor:009]:   Unit of Measurement: 'dBm'
[06:34:37][C][wifi_signal.sensor:009]:   Accuracy Decimals: 0
[06:34:37][C][wifi_info:011]: WifiInfo SSID 'Emre Oda ESP32S Connected SSID'
[06:34:37][C][wifi_info:011]:   Icon: 'mdi:router-wireless'
[06:34:38][C][wifi_info:012]: WifiInfo BSSID 'Emre Oda ESP32S Connected BSSID'
[06:34:38][C][wifi_info:012]:   Icon: 'mdi:router-wireless'
[06:34:38][C][wifi_info:009]: WifiInfo IPAddress 'Emre Oda ESP32S IP Address'
[06:34:38][C][wifi_info:009]:   Icon: 'mdi:ip'
[06:34:38][C][debug:067]: Debug component:
[06:34:38][C][debug:072]:   Free space on heap 'Free MEM'
[06:34:38][C][debug:072]:     State Class: ''
[06:34:38][C][debug:072]:     Unit of Measurement: 'B'
[06:34:38][C][debug:072]:     Accuracy Decimals: 0
[06:34:38][C][debug:072]:     Icon: 'mdi:counter'
[06:34:38][D][debug:079]: ESPHome version 2024.4.1
[06:34:38][D][debug:083]: Free Heap Size: 197264 bytes
[06:34:38][D][debug:113]: Flash Chip: Size=4096kB Speed=40MHz Mode=DIO
[06:34:38][D][debug:162]: Chip: Model=ESP32, Features=WIFI_BGN,BLE,BT, Cores=2, Revision=1
[06:34:38][D][debug:170]: ESP-IDF Version: v4.4.2
[06:34:38][D][debug:175]: EFuse MAC: 9C:9C:1F:C7:00:D4
[06:34:38][D][debug:275]: Reset Reason: Software Reset CPU
[06:34:38][D][debug:320]: Wakeup Reason: Unknown
[06:34:38][D][switch:012]: 'Emre Oda TV Güç' Turning ON.
[06:34:38][W][component:237]: Component api took a long time for an operation (72 ms).
[06:34:38][W][component:238]: Components should block for at most 30 ms.
[06:34:39][D][dht:048]: Got Temperature=28.5°C Humidity=32.6%
[06:34:39][W][component:170]: Component dht.sensor cleared Warning flag
[06:34:39][D][binary_sensor:036]: 'Emre Oda TV Durumu': Sending state ON

Additional information

Looking at the logs, for both of the template switches I am seeing an "always OFF" mode. I tried to modify this by checking these docs https://esphome.io/components/switch/#base-switch-configuration

and tried multiple combinations but, it is not changing. I tried "RESTORE_DEFAULT_OFF", "RESTORE_DEFAULT_ON", "ALWAYS_ON", and "DISABLED". For some reason, the logs show this as OFF all the time no matter what config you use.

I suspect that during shutdown or boot up or in both at the same time, the board turns off the switches and the lambda is ignored.

Masterz69 commented 3 weeks ago

Just guess, try to handle case when emre_tv_power has no state:

    lambda: |-
      if (id(emre_tv_power).has_state()) {
        if (id(emre_tv_power).state) {
          return true;
        } else {
          return false;
        }
      } else {
        return {};
      }
Salvora commented 3 weeks ago

Just guess, try to handle case when emre_tv_power has no state:

    lambda: |-
      if (id(emre_tv_power).has_state()) {
        if (id(emre_tv_power).state) {
          return true;
        } else {
          return false;
        }
      } else {
        return {};
      }

Nope, does not work. Still the same behaviour. The binary sensor should always have some state in any case.

Salvora commented 2 weeks ago

Tried a USB flash instead of OTA in case some configuration file is not getting a proper update. Still the same behaviour.

Salvora commented 2 weeks ago

issue closed. RESTORE_DEFAULT_OFF", "RESTORE_DEFAULT_ON", "ALWAYS_ON", and "DISABLED, works now as it was my mistake and I was editing wrong config files.