I'm trying to use if in ESPHome

I’m trying to do something like if while “input_number.meeting” is 1 than turn on “GPIO_27” else if while “input_number.meeting” is 2 than turn on “GPIO_33” etc.

Here is my existing code

output:
  - platform: gpio
    id: "GPIO_2"
    pin: 2
  - platform: gpio
    id: "GPIO_27"
    pin: 27
  - platform: gpio
    id: "GPIO_33"
    pin: 33
  - platform: gpio
    id: "GPIO_32"
    pin: 32


light:
  - platform: binary
    output: "GPIO_2"
    name: "Sleep"
  - platform: binary
    output: "GPIO_27"
    name: "Come In"
  - platform: binary
    output: "GPIO_33"
    name: "Meeting"
  - platform: binary
    output: "GPIO_32"
    name: "Inportent Meeting"


binary_sensor:
   - platform: gpio
     pin:
       number: 14
       mode: INPUT_PULLUP
       inverted: True
     name: "Come In"
     on_press:
     - homeassistant.service:
         service: input_number.set_value
         data:
           entity_id: input_number.meeting
           value: "1"
   - platform: gpio
     pin:
       number: 0
       mode: INPUT_PULLUP
       inverted: True
     name: "Meeting"
     on_press:
     - homeassistant.service:
         service: input_number.set_value
         data:
           entity_id: input_number.meeting
           value: "2"
   - platform: gpio
     pin:
       number: 4
       mode: INPUT_PULLUP
       inverted: True
     name: "Inportent Meeting"
     on_press:
     - homeassistant.service:
         service: input_number.set_value
         data:
           entity_id: input_number.meeting
           value: "3"
   - platform: gpio
     pin:
       number: 15
       mode: INPUT_PULLUP
       inverted: True
     name: "Sleep"
     on_press:
     - homeassistant.service:
         service: input_number.set_value
         data:
           entity_id: input_number.meeting
           value: "4"

Since you’re already calling the homeassistant service to set the input_number, you could just add a command to turn on the respective lights (giving them an id first).

light:
  - platform: binary
    id: come_in
    output: "GPIO_27"
    name: "Come In"

binary_sensor:
  - platform: gpio
    pin:
      number: 14
      mode: INPUT_PULLUP
      inverted: True
    name: "Come In"
    on_press:
      - homeassistant.service:
          service: input_number.set_value
          data:
            entity_id: input_number.meeting
            value: "1"
      - light.turn_on: come_in

Or you could import the input_number into ESPHome and act on any changes

text_sensor:
  - platform: homeassistant
    name: "Meeting number"
    entity_id: input_number.meeting
    on_value:
       then:
       ...
1 Like

Yes but I’m going to change the number in Home Assistant and with other ESPs

Well then use the second option of importing the input_number into ESPHome.

what do you mean

I mean the second option I showed above. Import the input_number as a text_sensor and act on the changes in the on_value event. Use some if conditions and use light.turn_on action to turn on the respective lights.

thanks this should work perfectly

You could, of course, also just set up an automation in HA that triggers on state_change of the input_number and use the Choose action to figure out what light to turn on.

sorry to bother you again but could you give me an example while on value 1 it’s on but on any other value is off

I just started with ESPHome

also I tried this but it did not work

  - platform: homeassistant
    name: "Meeting number"
    entity_id: input_number.metting
    on_value: 1
      then:
        - light.turn_on: come_in

You can do it like this:

  - platform: homeassistant
    id: meeting_number
    name: "Meeting number"
    entity_id: input_number.metting
    on_value:
      - if:
          condition:
            text_sensor.state:
              id: meeting_number
              state: '1'
          then:
            - light.turn_on: come_in
      - if:
          condition:
            text_sensor.state:
              id: meeting_number
              state: '2'
          then:
            - light.turn_on: meeting

or using a lambda:


  - platform: homeassistant
    id: meeting_number
    name: "Meeting number"
    entity_id: input_number.metting
    on_value:
      - lambda: |-
          if (x == "1") {
             auto call = id(come_in).turn_on();
          } elseif (x == "2") {
             auto call = id(meeting).turn_on();
          }
          call.perform();