ESPHome help evaluate condition

Hi,

I’m experimenting few things in ESPHome and running into few glitches. Here is the summary:

  1. Trying to use “interval” feature in ESPHome, in the test script below it should trigger every 2 minutes
  2. Seems like the first execution time interval is random so trying to identify first run using a global variable (counter) & avoid it.
  3. But seems like the “If” condition always evaluate to “false” which seems like the source of my problem.
esphome:
  name: ${name_of_board}
  platform: ESP8266
  board: d1_mini
globals:
  - id: counter
    type: int
    restore_value: no
    initial_value: "0"
interval:
  - interval: 2min
    then:
      - if:
          condition:
            lambda: 'return id(counter) == 0;'
          then:
            - logger.log: "Interval component is working in IF part"
            - globals.set:
                id: counter
                value: '1'
          else:
            - globals.set:
                id: counter
                value: '1'
            - logger.log: "Interval component is working in ELSE part"

Here are the logs from the device which suggests that “IF” statement not geting “true” may be the solution I need.

[02:46:38][D][main:225]: Test Board booted fine 
[02:47:58][D][main:096]: Interval component is working in else part
[02:49:58][D][main:096]: Interval component is working in else part
[02:51:58][D][main:096]: Interval component is working in else part

Let me know if you see any errors in my code, please let me know.

[14:03:09][D][wifi:386]: Starting scan…
[14:03:09][D][main:031]: Interval component is working in IF part
[14:03:14][D][main:039]: Interval component is working in ELSE part
wifi initialisation
I2C bus init
and more
[14:03:24][D][main:039]: Interval component is working in ELSE part
[14:03:34][D][main:039]: Interval component is working in ELSE part
[14:03:44][D][main:039]: Interval component is working in ELSE part
[14:03:54][D][main:039]: Interval component is working in ELSE part

Means it does reac IF aswell as ELSE some seconds later then the devices fires up, connects to wifi and all the rest and then finally is starts running through the defined interval and since having set your global to 1 earlier you only see the ELSE part.

Even if you want to catch it by enacling the ESPhome webserver you won’t notive since that also starts way after the initial 2 runs of your condition check.

btw: using 10s noch 2min since time is ithe most essential good nowadays
Code works as designed if you ask me.
if does check for counter == 0 which it is. then it directly (4s) sets this to 1 in the IF part
and from now on (with respect of your defined 2mins it only ends up in the ELSE part

1 Like

Great insight and actually explains a lot about few odd behaviors I’ve seen in the past.

For this one, I managed to make it work by modifying the following line:

lambda: 'return id(counter) < 3;'