Hi I’m trying to have a condition in an automation in ESPHome.
binary_sensor:
- platform: gpio
name: "Button"
pin:
number: D3
inverted: True
# filters:
# - delayed_on: 100ms
# - delayed_off: 100ms
on_multi_click:
- timing:
- ON for at most 1s
- OFF for at most 1s
- ON for at most 1s
- OFF for at least 0.2s
then:
- logger.log: "Double Clicked"
- homeassistant.service:
service: light.toggle
data:
entity_id: light.plate01_backlight
- timing:
- ON for at least 1s
then:
- logger.log: "Single Long Clicked"
- homeassistant.service:
service: light.toggle
data:
entity_id: light.bedroom_lights
- if:
condition:
lambda: 'return states.light.bedroom_lights.state == "on";'
then:
- homeassistant.service:
service: light.turn_off
data:
entity_id: light.plate01_backlight
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- logger.log: "Single Short Clicked"
- homeassistant.service:
service: light.toggle
data:
entity_id: light.nightstand
Basically I only want the backlight part to be turned off, if the light in the room is on.
But this does not work. So I’m guessing I cannot use states from hass as a condition?
I still haven’t got the grasp of lambda, but yes I think it cannot evaluate against ha states only internal esp devices…but homeassistant.service can use data_template(but not service_template yet). Yesterday I was testing to have single click to turn on a smart light and long click to reduce the brightness of the same, what I did was to run a ha script via esphome ha service then in the script evaluate the state with the templates.
lambdas are C++ code (and not Home Assistant-like jinja2 templates - try writing a full jinja2 parser for an embedded system ), so return states.light.bedroom_lights.state == "on"; won’t work. You need to first import the State into ESPHome with the “Home Assistant Text Sensor” as @tom_l said, then you can use return id(id_of_ha_text_sensor).state == "ON"; (beware of case sensitivity!)
data_template in homeassistant.service is a bit different - here HA-style templates are supported. The ESP just sends Home Assistant the full template and tells Home Assistant: “evaluate this on your own”.