Toggle switch with shelly 2.5

Hi, I have next configuration and I can’t enable a toggle switch (not momentary).

substitutions:
  devicename: "bedroom-light"
  friendly_name: "Bedroom Light"
  channel_1: First Light
  channel_2: Second Light

  max_power: "2000.0"
  max_temp: "70.0"

esphome:
  name: ${devicename}
  platform: ESP8266
  board: esp01_1m
  comment: "Shelly 2.5 Bedroom"

# WiFi connection
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on
  power_save_mode: HIGH # for ESP8266 LOW/HIGH are mixed up, esphome/issues/issues/1532

#Old device name
#  use_address: shelly25-bedroom.local  

  manual_ip:
    static_ip: 192.168.5.174
    gateway: 192.168.5.1
    subnet: 255.255.255.0
  
  ap:
    ssid: "${friendly_name} Hotspot"
    password: !secret hotspot_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

# Enable over-the-air updates
ota:

# Enable Web server
web_server:
  port: 80
  auth:
    username: !secret web_server_username
    password: !secret web_server_password

time:
  - platform: homeassistant
    id: homeassistant_time

i2c:
  sda: GPIO12
  scl: GPIO14

sensor:
  - platform: ade7953
    irq_pin: GPIO16 # Prevent overheating by setting this
    voltage:
      name: ${friendly_name} Voltage
    # On the Shelly 2.5 channels are mixed ch1=B ch2=A
    current_a:
      name: ${channel_2} current
      internal: true
    current_b:
      name: ${channel_1} current
      internal: true
    active_power_a:
      name: ${channel_2} Power
      id: power_channel_2
      filters:
        - calibrate_linear:
          - 0.0 -> 0.0
          - 105 -> 12.3
        - lambda: if (x < 2) return 0; else return (x);
      on_value_range:
        - above: ${max_power}
          then:
            - switch.turn_off: shelly_relay_2
            - homeassistant.service:
                service: persistent_notification.create
                data:
                  title: Message from ${friendly_name}
                data_template:
                  message: Switch turned off because power exceeded ${max_power}W
    active_power_b:
      name: ${channel_1} Power
      id: power_channel_1
      filters:
        - calibrate_linear:
            - 0.0 -> 0.0
            - -42 -> 5.1
        - lambda: if (x < 2) return 0; else return (x);      
      on_value_range:
        - above: ${max_power}
          then:
            - switch.turn_off: shelly_relay_1
            - homeassistant.service:
                service: persistent_notification.create
                data:
                  title: Message from ${friendly_name}
                data_template:
                  message: Switch turned off because power exceeded ${max_power}W
    update_interval: 30s

  - platform: total_daily_energy
    name: ${channel_1} Energy
    power_id: power_channel_1
    filters:
      - multiply: 0.001
    unit_of_measurement: kWh

  - platform: total_daily_energy
    name: ${channel_2} Energy
    power_id: power_channel_2
    filters:
      # Multiplication factor from W to kWh is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh

  # NTC Temperature
  - platform: ntc
    sensor: temp_resistance_reading
    name: ${friendly_name} Temperature
    unit_of_measurement: "°C"
    accuracy_decimals: 1
    icon: "mdi:thermometer"
    calibration:
      b_constant: 3350
      reference_resistance: 10kOhm
      reference_temperature: 298.15K
    on_value_range:
      - above: ${max_temp}
        then:
          - switch.turn_off: shelly_relay_1
          - switch.turn_off: shelly_relay_2
          - homeassistant.service:
              service: persistent_notification.create
              data:
                title: Message from ${friendly_name}
              data_template:
                message: Switch turned off because temperature exceeded ${max_temp}°C
  - platform: resistance
    id: temp_resistance_reading
    sensor: temp_analog_reading
    configuration: DOWNSTREAM
    resistor: 32kOhm
  - platform: adc
    id: temp_analog_reading
    pin: A0

status_led:
  pin:
    number: GPIO0
    inverted: yes

switch:
  - platform: gpio
    id: shelly_relay_1
    name: ${channel_1}
    pin: GPIO4
    icon: "mdi:electric-switch"
    restore_mode: RESTORE_DEFAULT_OFF

  - platform: gpio
    id: shelly_relay_2
    name: ${channel_2}
    pin: GPIO15
    icon: "mdi:electric-switch"
    restore_mode: RESTORE_DEFAULT_OFF

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO13
    name: "${channel_1} Input"
    on_state:
      then:
        - switch.toggle: shelly_relay_1
    filters:
      - delayed_on_off: 5ms    
  - platform: gpio
    pin:
      number: GPIO5
    name: "${channel_2} Input"
    on_state:
      then:
        - switch.toggle: shelly_relay_2

Like in this screenshot from original shelly settings:


I tried this but if I press on phisical switch nothing happen

  - platform: gpio
    pin:
      number: GPIO5
      mode: INPUT_PULLUP
      inverted: True
    name: "${channel_2} Input"
    on_state:
      then:
        - switch.toggle: shelly_relay_2

According to:

  - platform: gpio
    pin:
      number: GPIO5
    name: ${devicename} Switch2
    on_press:
      then:
        - switch.toggle: shelly_relay_2

you should not invert and pull up seems not to be necessary :bulb:

1 Like

Thank you, I solved by creating separate actions on_press and on_release:

  - platform: gpio
    pin:
      number: GPIO5
    name: "${room} ${channel_2} Input"
    on_press:
      then:
        - switch.turn_on: shelly_relay_2
    on_release:
      then:
        - switch.turn_off: shelly_relay_2

Actually not. You solved your binary_sensor not changing states by “stopping” pulling it ‘up’ and inverting it (essentially “fixing” it to one state).

In detail your not working config:

VS the working one:

On the other hand you now have a mostly anticipated configuration

because (depending on your [wall] switch type) you now have states fixed to the switch position. This is normally not wanted because the moment you switch the shelly via HA once the next action on the [wall] switch will simply do nothing when it is a typical toggle switch :bulb:

So best practice is really to just use:

Actually, this is exactly what I want, same as with original shelly firmware.

1 Like