[Solved] Logic problem for a relay?

Hi

I’m currently flashing some Shelly 1 devices (wall mount relay ESP based: https://shelly.cloud/shelly1-open-source/
I’m going to use one to replace a traditional relay that was controled by multiple “impulsive” buttons wired in parallel. As I’m replacing it with an “intelligent” device I’d like to add an auto-off feature. If light keeps more than 3 minutes on, switch it off automatically.
Here is the code I wrote but it doesn’t want my delay part of code whatever if I put in switch part or in output part. What am I doing wrong ?
Side question: is the delay cancelled and actions in it cancelled if state goes back off ? to avoid false triggers if there is multiple ON in a short time !

Thanks for help,

Vincèn

esphome:
  name: escaliers
  platform: espressif8266
  board: esp_wroom_02
  board_flash_mode: dout

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_passwd
  fast_connect: True
  manual_ip:
    static_ip: !secret ip_escaliers
    gateway: 192.168.1.1
    subnet: 255.255.255.0

api:

logger:

ota:

binary_sensor:
  - platform: gpio
    pin: GPIO5
    id: 'bouton_escaliers'
    name: "Bouton Escaliers"
    on_press:
      then:
        - switch.toggle: relais

output:
  - platform: gpio
    pin: GPIO4
    id: 'generic_out'
    on_turn_on:
      - delay: 4min
      - switch.off: relais

switch:
  - platform: output
    name: "Escaliers"
    output: 'generic_out'
    id: 'relais'

Solved nearly the problem (out of the timer that I would need to cancel if switch goes off to avoid multiple delayed timer in case of multiple on/off in a short time !, so still looking for a way to do that correctly !).

Here is updated sketch:

esphome:
  name: escaliers
  platform: espressif8266
  board: esp_wroom_02
  board_flash_mode: dout

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_passwd
  fast_connect: True
  manual_ip:
    static_ip: !secret ip_escaliers
    gateway: 192.168.1.1
    subnet: 255.255.255.0

api:

logger:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
    name: "Bouton Escaliers"
    on_press:
      - switch.toggle: relay

switch:
  - platform: gpio
    name: "Escaliers"
    pin: GPIO4
    id: relay
    restore_mode: always off
    on_turn_on:
      - delay: 4min
      - switch.turn_off: relay

While trying to figure out a solution, I think I’m nearly done with it but have a syntax problem here and I have checked in doc but it looks fine so if anyone has an idea what’s the problem ? It complains on the script_stop line and for the if :frowning:

esphome:
  name: escaliers
  platform: espressif8266
  board: esp_wroom_02
  board_flash_mode: dout

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_passwd
  fast_connect: True
  reboot_timeout: 60min
  manual_ip:
    static_ip: !secret ip_escaliers
    gateway: 192.168.1.1
    subnet: 255.255.255.0

api:
  reboot_timeout: 60min

logger:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
    name: "Bouton Escaliers"
    on_press:
      then:
        - script.stop: script_minuterie
        - switch.toggle: relay
        if:
          condition:
            switch.is_on: relay
          then:
            - script.execute: script_minuterie

switch:
  - platform: gpio
    name: "Escaliers"
    pin: GPIO4
    id: relay
    restore_mode: always off

script:
  - id: script_minuterie
    then:
      - delay: 5min
      - switch.turn_off: relay

What errors are you getting?

ERROR Error while reading config: while parsing a block collection in "/config/esphome/Escaliers.yaml", line 31, column 9 expected <block end>, but found '?' in "/config/esphome/Escaliers.yaml", line 33, column 9

33, by my count, is the if: in your binary_sensor. Try indenting it and the four lines below.

In fact as indicated in my original message the two errors triggered are on the script.stop line and on the if ! If I do the change you suggest by tabbing the if and subsequent lines I get then that new error so not sure it’s really the way to correct the errors no ?

INFO Reading configuration...
Failed config

binary_sensor.gpio: [source /config/esphome/Escaliers.yaml:24]
  platform: gpio
  pin: 
    number: GPIO5
  name: Bouton Escaliers
  on_press:  [source /config/esphome/Escaliers.yaml:29]
    then:  [source /config/esphome/Escaliers.yaml:30]
      - script.stop: 
          id: script_minuterie
      
      Cannot have two actions in one item. Key 'switch.toggle' overrides 'if'! Did you forget to indent the block inside the action?.
      - [source /config/esphome/Escaliers.yaml:31]
        switch.toggle: relay
        if: 
          condition: 
            switch.is_on: relay
          then: 
            - script.execute: script_minuterie

As I didn’t succeed to find out if problem was again with f*cking yaml syntax or if I was trying to do something not possible in esphome logic, I rewrote it in a different way and now it works perfect (no risk of retrigger in case of fast multiple on/off :wink:
If someone wants to include it in cookbook of ESPHome, feel free to go :wink: Checked how to do that but gosh so complicated so gave up on it :frowning:

esphome:
  name: escaliers
  platform: espressif8266
  board: esp_wroom_02
  board_flash_mode: dout

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_passwd
  fast_connect: True
  reboot_timeout: 60min
  manual_ip:
    static_ip: !secret ip_escaliers
    gateway: 192.168.1.1
    subnet: 255.255.255.0

api:
  reboot_timeout: 60min

logger:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO5
    name: "Bouton Escaliers"
    on_press:
      then:
        - switch.toggle: relay

switch:
  - platform: gpio
    name: "Escaliers"
    pin: GPIO4
    id: relay
    restore_mode: always off
    on_turn_on:
    - script.execute: script_minuterie
    on_turn_off:
    - script.stop: script_minuterie

script:
  - id: script_minuterie
    then:
      - delay: 5min
      - switch.turn_off: relay
2 Likes