I have connected an OLED display (SSD1306) to my NodeMCU ESP8266; there is also a button on it with which you can switch between different “pages”.
For example, there is the “Outside” page to display the temperature outside.
My question would be whether there are any (concrete) code examples or ideas on how I need to change the code so that I can use a new entity (e.g. select.X) in HomeAssistant to select what is displayed on the screen on the “Outside” page.
Best regards and thanks for your reply!
This is my code so far:
substitutions:
name: "display"
friendly_name: ESPHome
esphome:
name: ${name}
friendly_name: ${friendly_name}
name_add_mac_suffix: false
project:
name: esphome.web
version: '1.0'
esp8266:
board: nodemcuv2 # Change the board type to nodemcuv2 for NodeMCU 0.1
# Enable logging
logger:
# Enable Home Assistant API
api:
# Allow Over-The-Air updates
ota:
# Allow provisioning Wi-Fi via serial
improv_serial:
wifi:
ssid: "ssid"
password: "password"
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Display"
password: "password"
# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:
font:
- file: "gfonts://Roboto"
id: roboto
size: 20
- file: "gfonts://Roboto"
id: roboto_small
size: 15
i2c:
sda: D1
scl: D2
sensor:
- platform: homeassistant
name: "Time"
id: Time
entity_id: sensor.time
- platform: homeassistant
name: "Wetterstation Temperature"
id: Temperature
entity_id: sensor.unknown_70_ee_50_a2_17_9a_unknown_70_ee_50_a2_17_9a_unknown_70_ee_50_a2_17_9a_aussenmodul_temperature
- platform: homeassistant
name: "Wetterstation Humidity"
id: Humidity
entity_id: sensor.unknown_70_ee_50_a2_17_9a_unknown_70_ee_50_a2_17_9a_unknown_70_ee_50_a2_17_9a_aussenmodul_humidity
- platform: homeassistant
name: "Wohnzimmer Temperature"
id: Wohnzimmer_Temperature
entity_id: sensor.wohnzimmer_temperatur
- platform: homeassistant
name: "Wohnzimmer Humidity"
id: Wohnzimmer_Humidity
entity_id: sensor.wohnzimmer_luftfeuchtigkeit
globals:
- id: display_temperature
type: bool
restore_value: true
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode:
input: true
pullup: true
inverted: true
name: Button
id: Button
filters:
# - delayed_off: 1ms
on_press:
then:
- globals.set:
id: display_temperature
value: !lambda "return !id(display_temperature);"
- if:
condition:
lambda: "return id(display_temperature);"
then:
- display.page.show: weather_outdoor_page
else:
- display.page.show: weather_indoor_page
on_double_click:
then:
- display.page.show: hello_page
time:
- platform: homeassistant
id: homeassistant_time
timezone: Europe/Amsterdam
display:
- platform: ssd1306_i2c
model: "SSD1306 128x64"
reset_pin: D0
address: 0x3C
id: my_display
pages:
- id: weather_outdoor_page
lambda: |-
if (id(display_temperature)) {
char str[17];
time_t currTime = id(homeassistant_time).now().timestamp;
strftime(str, sizeof(str), "%H:%M Aussen", localtime(&currTime)); // Nur Uhrzeit anzeigen
it.printf(0, 0, id(roboto_small), "%s", str); // Anzeige der Uhrzeit
it.printf(0, 20, id(roboto), "%.1f°C", id(Temperature).state); // Display temperature
it.printf(0, 40, id(roboto), "%.1f %%", id(Humidity).state); // Display humidity
}
- id: weather_indoor_page
lambda: |-
char str[17];
time_t currTime = id(homeassistant_time).now().timestamp;
strftime(str, sizeof(str), "%H:%M Innen", localtime(&currTime)); // Nur Uhrzeit anzeigen
it.printf(0, 0, id(roboto_small), "%s", str); // Anzeige der Uhrzeit
it.printf(0, 20, id(roboto), "%.1f°C", id(Wohnzimmer_Temperature).state); // Display temperature
it.printf(0, 40, id(roboto), "%.1f %%", id(Wohnzimmer_Humidity).state); // Display humidity
- id: hello_page
lambda: |-
it.printf(0, 0, id(roboto), "Hello!");