How to import light state from HA?

In recent ESPHome now possible to import states from HA to ESPHome:

  • Home Assistant Sensor
  • Home Assistant Text Sensor

But don’t get idea how I can import light on/off state, if it possible at all.

I tried both like this:

text_sensor:
  - platform: homeassistant
    id: current_light_state
    entity_id: ight.vykliuchatel_v_koridore

but if I toggle light I see nothing in logs.

General idea: my wall switch have status led. I want to set led state depending on light bulb state as it can be turned on from another switch or UI - and in this case have “wrong” color.

Look at spelling think it should be light.vykli…

2 Likes

That is not recent.

I also think it should work as you described. Maybe in a future version.

If don’t, there’s another choice:

  • create one template binary_sensor pulling the state of the light in ha, and pulling state of the binary _sensor in esphome
1 Like

Oh… funny thing: when I try “sensor” I use correct spelling. And then I somehow copy-paste in wrong way :frowning:

Now I got state:

[00:45:40][C][homeassistant.text_sensor:022]: Homeassistant Text Sensor 'current_light_state'
[00:45:40][C][homeassistant.text_sensor:023]:   Entity ID: 'light.vykliuchatel_v_koridore'
[00:45:40][D][homeassistant.text_sensor:016]: 'light.vykliuchatel_v_koridore': Got state 'off'
[00:45:40][D][text_sensor:015]: 'current_light_state': Sending state 'off'
[00:46:19][D][homeassistant.text_sensor:016]: 'light.vykliuchatel_v_koridore': Got state 'on'
[00:46:19][D][text_sensor:015]: 'current_light_state': Sending state 'on'
[00:46:21][D][homeassistant.text_sensor:016]: 'light.vykliuchatel_v_koridore': Got state 'off'
[00:46:21][D][text_sensor:015]: 'current_light_state': Sending state 'off'

Good day.

I have searched high and low for anything related to this, but can’t seem to get esphome to compile it due to it saying “On/Off” not declared in scope.

Is there any way of using the imported state of a light (On/Off) and use this as a trigger for an automation?

Basically…

on_press:
      then:
        - if:
            condition:
              and:
                - switch.is_on: ${sen_id}_day_night
                - lambda: 'return id(mains_volt).state > 180;'
#                use the state of the light in HA eg: off             #
            then:
              - light.turn_off:
                  id: ${sen_id}_led
              - homeassistant.service:
                  service: light.turn_on
                  data:
                    entity_id: light.bathroom_light
              - lambda: !lambda |-
                  id(timer).publish_state(true);
            else:
              - script.stop: timeout
        - if:
            condition:
              and:
                - light.is_off: ${sen_id}_led
                - switch.is_on: ${sen_id}_day_night
                - lambda: 'return id(mains_volt).state < 180;'
            then:
              - light.turn_on:
                  id: ${sen_id}_led
                  red: 100%
                  green: 95%
                  blue: 90%
              - lambda: !lambda |-
                  id(timer).publish_state(true);
            else:
              - script.stop: timeout

In my opinion better to create new topic with your question.

Anyway, this is what I have after all those years in ESPHome config to automate led depending on entity state imported from HA:

text_sensor:
  - platform: homeassistant
    id: corridor_light_state
    entity_id: light.liustra_v_koridore_1
    on_value:
      then:
      - if:
          condition:
            text_sensor.state:
              id: corridor_light_state
              state: "on"
          then:
            - output.turn_on: "${hostname}_led_2"
      - if:
          condition:
            text_sensor.state:
              id: corridor_light_state
              state: "off"
          then:
            - output.turn_off: "${hostname}_led_2"

Thank you very much, although it still says on compile that “On” & “Off” has not been declared which is odd, but at least I feel like with your bit of code, I am now a 1000 miles closer to solving it. :slight_smile: