Using HA switch states in lambda

Tags: #<Tag:0x00007f7390b53500>

I’m trying to import the state of a switch from HA and then use it in lambdas to trigger other actions. However, after compiling, uploading and running I’m getting a ‘warning’ error that esphome “Can’t convert ‘off’ to number!”. Can anyone point me in the right direction?

relevant config:

binary_sensor:
  - platform: template
    name: Switch State
    id: switch_state
    internal: true
    lambda: |-
      if (id(my_switch).state) {
        return true;
      } else {
        return false;
      }

switch:
  - platform: template
    id: example
    internal: true
    on_turn_on:
      - light.turn_on:
          id: indicator
    on_turn_off:
      - light.turn_off:
          id: indicator
    lambda: 'return id(switch_state).state;'

sensor:
  - platform: homeassistant
    entity_id: switch.some_switch
    id: my_switch

Many TIA,

-J

Try changing sensor: to text_sensor:

I tried text sensor and am now getting a new error. :confused:

error:

src/main.cpp: In lambda function:
src/main.cpp:419:33: error: no match for 'operator==' (operand types are 'std::string {aka std::basic_string<char>}' and 'int')
       if ( fan_state_ha->state  == 'on' {
                                 ^

relevant code:

text_sensor:
  - platform: homeassistant
    entity_id: switch.some_switch
    id: my_switch

binary_sensor:
  - platform: template
    name: Switch State
    id: switch_state
    internal: true
    lambda: |-
      if (id(my_switch).state) {
        return true;
      } else {
        return false;
      }

switch:
  - platform: template
    id: example
    internal: true
    on_turn_on:
      - light.turn_on:
          id: indicator
    on_turn_off:
      - light.turn_off:
          id: indicator
    lambda: 'return id(switch_state).state;'

Perhaps a binary_sensor ?

While I’m not 100% what you want to accomplish, I think what you may want to look at is including the lambda function for triggering another action in the text_sensor itself. I’m going to link to some of my ESPHome YAML on GitHub, maybe it’ll help you get started:

The YAML is for a light switch that has red, blue, and green LED indicator lights on it. Down at the very bottom, I have a couple of homeassistant text_sensors that look at the state of some smart bulbs in the room (I’ve programmed the smart switch to control my smart bulbs) as well as the state of the garage door. Based on those sensors, the blue LED on the light switch may be solid, off, or blinking, and the action of turning the LED on/off is executed by lambda functions in the text_sensor itself.

Excellent. Thank you! Changed my single quote marks to double and started seeing some success. Using your example I was able to clean things up a bit too and was able to accomplish what I was aiming for without the intermediate sensor or switch. Thanks for the help!

what did the trick:

text_sensor:
  - platform: homeassistant
    name: "My Switch"
    internal: true
    entity_id: switch.some_switch
    on_value:
      then:
        lambda: |-
          if(x == "on") {
            auto call = id(indicator).turn_on();
            call.perform();
          } else {
            auto call = id(indicator).turn_off();
            call.perform();
          }

-J

1 Like

Glad you got it working!