Using if on state "on" or "off"

I’m trying to show a glyph on a display if a state is “on” but I can’t seem to get the syntax right.

This always shows the image, even if the state is off

      if (id(sensor_home).state) {
        it.image(110,0,id(img_car));
      }

If I evaluate with (id(sensor_home).state == 'off' or (id(sensor_home).state == 'on' both return false.

Any ideas how to evaluate for an on state?

What is sensor_home? Please share at least the config for that entity.

1 Like

I’ve defined it as:

  - platform: homeassistant
    entity_id: binary_sensor.garage_left_bay_presence
    id: sensor_home
    internal: true

The only reference I could find for garage_left_bay_presence is in my configuration.yaml:

template:
  - binary_sensor:
      - name: "Garage Left Bay Presence"
        state: "{{ states('sensor.garage_bay_left_ultrasonic_sensor')|float(0) < 2 }}"
        device_class: presence

A friend of mine did it, I can’t seem to find more about it.

To confirm, is that a binary sensor in ESPHOME?

1 Like

So we can see what the sensor is doing, go to Developer Tools / Template and copy the following …

{{ states('sensor.garage_bay_left_ultrasonic_sensor')|float(0) < 2 }}
{{ states('sensor.garage_bay_left_ultrasonic_sensor') }}

Then let us know the result.

Not quite enough of the config. Is that under sensor: or binary_sensor:?

1 Like

Under sensor, herewith my whole code (line 123 I’m printing the state as a float and I get nan):

substitutions:
  name: esphome-web-038c9e
  friendly_name: Micro Wide Display 2

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  name_add_mac_suffix: false
  project:
    name: esphome.web
    version: '1.0'

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp8266.yaml@main
  import_full_config: true

# To have a "next url" for improv serial
web_server:

i2c:
  scl: D1
  sda: D2

font:
  - file: "gfonts://B612 Mono"
    id: font_small
    size: 12
  - file: "gfonts://Reddit Mono"
    id: font_mono_medium
    size: 14

image:
  - file: "mdi:battery"
    id: img_batt
    resize: 14x14
  - file: "mdi:power-plug"
    id: img_load
    resize: 14x14
  - file: "images/dollar_red.png"
    id: img_grid
    resize: 14x14
  - file: "mdi:arrow-down-bold"
    id: img_down
    resize: 14x14
  - file: "mdi:arrow-up-bold"
    id: img_up
    resize: 14x14
  - file: "mdi:car_back"
    id: img_car
    resize: 14x14

sensor:
  - platform: homeassistant
    id: sensor_soc
    entity_id: sensor.deye_sunsynk_sol_ark_battery_state_of_charge
    internal: true
  - platform: homeassistant
    id: sensor_load
    entity_id: sensor.deye_sunsynk_sol_ark_load_power
    internal: true
  - platform: homeassistant
    entity_id: sensor.deye_sunsynk_sol_ark_grid_power
    id: sensor_grid      
    internal: true
  - platform: homeassistant
    entity_id: sensor.deye_sunsynk_sol_ark_battery_power
    id: sensor_battery_power     
    internal: true
  - platform: homeassistant
    entity_id: binary_sensor.garage_left_bay_presence
    id: sensor_home
    internal: true
  - platform: homeassistant
    id: sensor_grid_v
    entity_id: sensor.deye_sunsynk_sol_ark_grid_voltage

time:
  - platform: homeassistant
    id: esptime

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x32"
    reset_pin: 0
    address: 0x3C
    lambda: |-
      it.printf(14,0, id(font_small), "%.0f%%", id(sensor_soc).state);
      if (id(sensor_battery_power).state < -100) {
        it.image(0,0,id(img_down));
      }
      else if (id(sensor_battery_power).state > 100) {
        it.image(0,0,id(img_up));
      }
      else {
        it.image(0,0,id(img_batt));
      }
      if (!id(sensor_home).state) {
        it.image(110,0,id(img_car));
      }
      it.printf(110,0, id(font_small), "%.2f%", id(sensor_home).state);

      it.image(40,0,id(img_load));
      it.printf(58,0, id(font_small), "%.2f% KW", id(sensor_load).state/1000.0);
      it.image(40,20,id(img_grid));
      if(id(sensor_grid_v).state < 150) {
        it.print(58,20, id(font_small), "OUT!");
      }
      else {
        it.printf(58,20, id(font_small), "%.2f% KW", id(sensor_grid).state/1000.0);
      }

      it.strftime(0, 20, id(font_small), TextAlign::TOP_LEFT, "%H:%M", id(esptime).now());

@RoadkillUK I see the following:

False
3.07

@Mahko_Mahko yes, ESPHome sensor, but the entity / state is declared in yaml.

Put it under binary_sensor: instead then:

# existing sensors up to this point
  - platform: homeassistant
    id: sensor_grid_v
    entity_id: sensor.deye_sunsynk_sol_ark_grid_voltage

binary_sensor:
  - platform: homeassistant
    entity_id: binary_sensor.garage_left_bay_presence
    id: sensor_home
    internal: true

Then you can use if(id(sensor_home).state).

2 Likes

Brilliant! Thanks! So my mistake was declaring it as a sensor in stead of a binary_sensor in the esphome config.