Hass entities in conditions

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?

2 Likes

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.

hm. that might be an option. Would you post your config for that?

Which one the esp yaml or the brightness script ?

the whole lot :wink:

This is the esphomelib conf

esphomeyaml:
  name: sonoff_t1_demo
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: 'net-IoT'
  password: 'password'
  fast_connect: on
  manual_ip:
    static_ip: 10.10.4.150
    gateway: 10.10.4.1
    subnet: 255.255.255.0
# Enable logging

# debug: 
logger:
  # level: VERY_VERBOSE
  # logs:
  #   binary_sensor.component: VERY_VERBOSE

# Enable Home Assistant API
api:

ota:

output:
  - platform: gpio
    pin: 12
    id: light_1
  - platform: gpio
    pin: 5
    id: light_2
  - platform: gpio
    pin: 4
    id: light_3


## Simple Toggle Relay
binary_sensor:
  - platform: gpio
    id: button_1
    name: "button 1"
    pin:
      number: 0
      mode: INPUT_PULLUP
      inverted: True
    # on_press:
    #   then:
    #     - light.toggle:
    #         id: light_light_1
    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"
          - light.toggle: 
              id: light_light_1
      - timing:
          - ON for 1s to 4s
          - OFF for at least 0.5s
        then:
          - logger.log: "Single Long Clicked"
          - homeassistant.service:
              service: 'script.turn_on'
              data: 
                entity_id: 'script.light_1_dim'
      - timing:
          - ON for at most 1s
          - OFF for at least 0.5s
        then:
          - logger.log: "Single Short Clicked"
          - homeassistant.service:
              service: script.turn_on
              data_template:
                entity_id: >-
                  {% if states.light.light_1.state == "on" %}
                    script.light_1_off
                  {% elif states.light.light_1.state == "off" %}
                    script.light_1_on
                  {% endif %}

  - platform: gpio
    id: button_2
    pin:
      number: 9
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - light.toggle:
            id: light_light_2
  - platform: gpio
    id: button_3
    pin:
      number: 10
      mode: INPUT_PULLUP
      inverted: True
    on_press:
      then:
        - light.toggle:
            id: light_light_3


light:
  - platform: binary
    id: light_light_1
    name: 'Light 1'
    output: light_1
  - platform: binary
    id: light_light_2
    name: 'Light 2'
    output: light_2
  - platform: binary
    id: light_light_3
    name: 'Light 3'
    output: light_3

This is the script for reducing brightness

alias: light_1_dimm
sequence:
  - service_template: >-
      {% if states.light.light_1.state == "on" %}
        light.turn_on
      {% endif %}
    data_template:
      entity_id: light.light_1
      brightness: >
        {% set curr_brightness = state_attr('light.light_1', 'brightness') %}
        {% set target_brightness = (curr_brightness - 100) | int %}
        {% if target_brightness < 0 %}
          {{ 0 }}
        {% elif target_brightness > 0 %}
          {{ target_brightness}}
        {% endif %}
1 Like

If you wan tto use states from home assistant in the ESP you have to add them as sensors first:

So to clear up some confusion:

lambdas are C++ code (and not Home Assistant-like jinja2 templates - try writing a full jinja2 parser for an embedded system :wink: ), 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”.

2 Likes

Does the case sensitivity also apply to unavailable state of a light.

I want to see if a smart light is available to be toggled otherwise just toggle the relay.