SOLVED: Trying to display the state of a switch in ESPHOME display

Hi All

I have a switch that I would like to display in the lambda in esphome if it’s on or not.
I’ve tried to just use the switch directly, but is that possible? Or do I need to create a sensor that holds the state of the switch to be able to display the state?
This is the config I have so far:

substitutions:
  device_name: kitchendisplay
  
esphome:
  name: kitchendisplay
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "ssid"
  password: "pw"

# Enable logging
logger:
  baud_rate: 0

# Enable Home Assistant API
api:

ota:
  password: "pw"

text_sensor:
  - platform: version
    name: "${device_name} ESPHome Version"
  - platform: wifi_info
    ip_address:
      name: "${device_name} ip"
    ssid:
      name: "${device_name} ssid"
  - platform: homeassistant
    id: pumprunning
    entity_id: switch.vandsten
    internal: true
    
sensor:
  - platform: wifi_signal
    name: '${device_name} WiFi Signal'
    update_interval: 60s
    accuracy_decimals: 0
  - platform: uptime
    name: '${device_name} Uptime'
    unit_of_measurement: days
    update_interval: 300s
    accuracy_decimals: 1
    filters:
      - multiply: 0.000011574
  - platform: homeassistant
    id: pooltemp
    entity_id: sensor.pool_temperature
    internal: true

time:
  - platform: homeassistant
    id: homeassistant_time

font:
  - file: "comic.ttf"
    id: comic
    size: 20
  - file: "calibrib.ttf"
    id: headline
    size: 30
  - file: "calibri.ttf"
    id: normaltext
    size: 20
  - file: "calibri.ttf"
    id: temptext
    size: 50 
spi:
  clk_pin: D0
  mosi_pin: D1

display:
  - platform: waveshare_epaper
    rotation: 90
    cs_pin: D2
    dc_pin: D6
    busy_pin: D7
    reset_pin: D5
    model: 2.90in
    full_update_every: 300
    lambda: |-
      it.printf(148, 0, id(headline), TextAlign::TOP_CENTER, "Fribert smarthome");
      it.strftime(296, 40, id(normaltext), TextAlign::BASELINE_RIGHT, "%H:%M", id(homeassistant_time).now());
      it.printf(0, 70, id(temptext), TextAlign::BASELINE_LEFT, "Pool:");
      it.printf(0, 120, id(temptext), TextAlign::BASELINE_LEFT, "Pump:");
      if (id(pooltemp).has_state()) {
        it.printf(220, 70, id(temptext), TextAlign::BASELINE_RIGHT, "%.1f°C", id(pooltemp));
      }
      it.printf(220, 120, id(temptext), TextAlign::BASELINE_RIGHT, "%s", id(pumprunning).state.c_str());

But the pump state is not printing anything to the display.

2 Likes

Hmm, ok I made a template sensor, and that shows the state of the switch:


  - platform: template
    sensors:
      pumprunning:
        friendly_name: "Is poolpump running"
        value_template: "{{ is_state('switch.vandsten', 'on' ) }}"

I then imported that in esphome:

binary_sensor:
  - platform: homeassistant
    id: pumprunning
    entity_id: binary_sensor.pumprunning
    internal: true

And finally I wanted it to print on/off with this line:

      if(id(pumprunning).state) {
        it.printf(140, 120, id(temptext), TextAlign::BASELINE_LEFT, "on");
      } else {
        it.printf(140, 120, id(temptext), TextAlign::BASELINE_LEFT, "off");
      }

But no matter of the sensor state, it is shown as “off”.
And I don’t see anything update (not even the clock) unless I set the

    full_update_every: 30

Is that correct?

I’m running this in hass.io if that has any influence?

Ok, so I got a bit further, now it’s looking much better.
I made a binary sensor that showed the state of the switch.
I then import it to esphome.

The config looks like this:

binary_sensor:
  - platform: homeassistant
    id: pumprunning
    entity_id: binary_sensor.pumprunning
    internal: true
    
sensor:
  - platform: homeassistant
    id: pooltemp
    entity_id: sensor.pool_temperature
    internal: true

time:
  - platform: homeassistant
    id: homeassistant_time

font:
  - file: "calibri.ttf"
    id: normaltext
    size: 20
  - file: "calibri.ttf"
    id: temptext
    size: 50
    
spi:
  clk_pin: D0
  mosi_pin: D1

display:
  - platform: waveshare_epaper
    rotation: 90
    cs_pin: D2
    dc_pin: D6
    busy_pin: D7
    reset_pin: D5
    model: 2.90in
    full_update_every: 600
    lambda: |-
      it.strftime(6, 22, id(normaltext), TextAlign::BASELINE_LEFT, "%H:%M", id(homeassistant_time).now());
      if (id(pooltemp).has_state()) {
        it.printf(268, 70, id(temptext), TextAlign::BASELINE_RIGHT, "%.1f°C", id(pooltemp));
      } else {
        it.print(162, 70, id(temptext), TextAlign::BASELINE_LEFT, "offline");
      }
      if(id(pumprunning).state) {
        it.print(162, 117, id(temptext), TextAlign::BASELINE_LEFT, "on");
      } else {
        it.print(162, 117, id(temptext), TextAlign::BASELINE_LEFT, "off");
      }

So it shows the state of the switch correctly.
But when it boots it shows the temperature as offline, but as soon as the temp sensor is polled, it gets an ‘unavailable’ , and that is converted to a 0, which is not what I would like, I would like it to show offline.

When I compile, I get this warning:

src/main.cpp: In lambda function:
src/main.cpp:546:83: warning: format '%f' expects argument of type 'double', but argument 7 has type 'esphome::homeassistant::HomeassistantSensor*' [-Wformat=]
it.printf(268, 70, bigtext, TextAlign::BASELINE_RIGHT, "%.1f°C", pooltemp);
4 Likes

Thank you for following through with this post. I used this to get the status of a couple switches displayed on my screen.

1 Like

Hi.
Did you find a solution for the double warning??

edit:
I don’t know why (tried it several times!!), but this finaly works (whitout compile errors):
it.printf(20,4,id(font1), “%.1f°C”, id(current_temperature).state);