Trying to set variables from binary sensor

I have a d1-mini with a single momentary push button. I would like to set a variable to “x” on short press and to “y” on long press.
Then I want to decide what to do with those values in HA automation (that part I should be ok with)

This is what I have so far, perhaps it is a simple solution but I either cannot find it or I don’t understand the results from my searches…

binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    on_click:
    - min_length: 50ms
      max_length: 500ms
      then:
        # set some variable value = x 
    - min_length: 1000ms
      max_length: 2000ms
      then:
        # set some variable value = y
    filters:
      - invert:
    name: "Bedroom toggle"

I have found part of my problem, I am now able to execute the short on_click succesfully.
Stuck on the second part as soon as I add an “if” where I have my remarks, i get the red underlines indicating there is an error. Here is my updated code:

binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    on_click:
    - min_length: 50ms
      max_length: 500ms
      then:
      - homeassistant.service:
          service: switch.toggle
          data:
            entity_id: switch.bedroom
    - min_length: 1000ms
      max_length: 2000ms
      then:
        # if a lamp is on then run HA automation 1
        # else run HA automation 2
    filters:
      - invert:
    name: "Bedroom toggle"

Yep, conditions in esphome are strange… It’s formed:

then:
   - if:
      condition:
         - your condition
      then:
         - your automation
      else:
         - your automation

This is quite a mystery but I cannot find a way to do this which should be so simple…
under the condition I tried

switch.is_on: living_room

then I get "couldn’t find ID living_room

I tried lambda but having other issues with no indication on how to fix…

what do I need to write to detect if a light/switch is ON
and how do I run an HA predefined script from esphome?

should work if correct id.
I don’t see that in your yaml

Sorry, I should have posted my complete code from the beginning.
Your last response pointed me to the need to create an id, looking at examples I added the lamp_status within the sensor section under platform homeassistant but I get an error indicating lamp_status is not a switch. Tried to add it to the switch section but getting an error indicating switch.homeassistant does not exist…

esphome:
  name: bedroom-switches
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 60min
  encryption:
    key: "1deNiN5X2/euDnmc***"

ota:
  password: "***"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom-Switches"
    password: "***"

captive_portal:

time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restart_switch

switch:
  - platform: restart
    name: "Bedroom Switches Resstart"
    id: restart_switch

binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    on_click:
    - min_length: 50ms
      max_length: 500ms
      then:
      - homeassistant.service:
          service: switch.toggle
          data:
            entity_id: switch.bedroom
    - min_length: 1000ms
      max_length: 2000ms
      then:
        - if:
            condition:
              switch.is_on: lamp_status
            then:
              # test
            else:
              #test
    filters:
      - invert:
    name: "Bedroom toggle"
sensor:  
  - platform: uptime
    name: "bedSwitch_Uptime Sensor"
  - platform: wifi_signal
    name: "Bedroom Switches WiFi Signal"
    update_interval: 60s
  - platform: homeassistant
    id: lamp_status
    entity_id: switch.living_room 

I would create a template switch in ESPHome and use that to do the HA service calls.

Something along these lines:

I’m not using HA, so difficult to answer.
Try to give it a name or attribute

Thank you @Karosm and @zenzay42 for your help.
I have a lot less hair on my head now but I finely got it working.
The only thing I could get working in the condition is lambadas
and I was not able to execute automations but it worked with scripts so I created scripts to launch the automations.
What this code does is from a d1-mini (esp8236) with a single push button; A quick push will toggle the lamps on my bed side and a long push will either turn on or off all lights in the house. The only issue with this is it will not help with weight loss.

Here is my final code:

esphome:
  name: bedroom-switches
  platform: ESP8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  reboot_timeout: 60min
  encryption:
    key: "1deNiN5X2/*****"

ota:
  password: "***"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom-Switches"
    password: "***"

captive_portal:

time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restart_switch

switch:
  - platform: restart
    name: "Bedroom Switches Resstart"
    id: restart_switch

binary_sensor:
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
    on_click:
    - min_length: 50ms
      max_length: 500ms
      then:
      - homeassistant.service:
          service: switch.toggle
          data:
            entity_id: switch.bedroom
    - min_length: 2000ms
      max_length: 5000ms
      then:
        - if:
            condition:
              lambda: 'if (id(lamp_status).state == "on") {return true;} else {return false;}'
            then:
            - homeassistant.service:
                service: script.house_off
                # turn off all lights
            else:
            - homeassistant.service:
                service: script.house_on
                # turn on all lights
                
    filters:
      - invert:
    name: "Bedroom toggle"
sensor:  
  - platform: uptime
    name: "bedSwitch_Uptime Sensor"
  - platform: wifi_signal
    name: "Bedroom Switches WiFi Signal"
    update_interval: 60s
text_sensor:
  - platform: homeassistant
    id: lamp_status
    entity_id: switch.living_room 

You should bring switch.living room in as a binary_sensor instead of a text_sensor:

binary_sensor:
  - platform: homeassistant
    id: lamp_status
    entity_id: switch.living_room 
    internal: true

The internal: true stops it being shared back to HA. Then your condition that currently has the lambda in it simply becomes:

        - if:
            condition:
              - binary_sensor.is_on: lamp_status
            then:
              - homeassistant.service:

@Troon Thanks for the info.
The only thing is, I had to do

- binary_sensor.is_on: lamp_status

because it was complaining about ID not allowing “.” but it is working well.

1 Like

Of course, sorry. Fixed above to help anyone else speed-reading the solution.