2019-02-26 21:29:34 +01:00
Time & Temperature on OLED Display
==================================
.. seo ::
2021-05-20 16:00:53 +02:00
:description: Instructions for setting up a display in ESPHome to show sensor values from Home Assistant
2019-02-26 21:29:34 +01:00
:image: display_time_temp_oled_1.jpg
:keywords: Display
.. figure :: images/display_time_temp_oled_1.jpg
:align: left
:width: 75.0%
2019-02-27 18:32:47 +01:00
In this example I have used a :doc: `SSD1306 OLED Display over I²C </components/display/ssd1306>` to
show current time and two different temperature values from Home Assistant.
2019-02-26 21:29:34 +01:00
ESPHome has support for several different types of displays. The display used here is 1.3" with 128x64 monochrome pixels (`` SH1106 128x64 `` ).
Hardware configuration
----------------------
Hardware is easy! Only four connections are needed:
- `` VCC `` - Power (my display could use either 3.3V or 5V)
- `` GND `` - Ground
- `` SDA `` - Serial Data
- `` SCL `` - Serial Clock
.. warning ::
Ensure your display handles 5V if you use that.
Software configuration
----------------------
Getting Time
***** ***** **
2020-05-10 21:27:59 +02:00
Get the time from Home Assistant to sync the onboard real-time clock.
2019-02-26 21:29:34 +01:00
.. code-block :: yaml
time:
- platform: homeassistant
2019-11-29 18:16:51 +01:00
id: esptime
2019-02-26 21:29:34 +01:00
Getting Temperature
***** ***** ***** *** *
2019-02-27 18:32:47 +01:00
Next, we want to get two temperature sensors imported from Home Assistant.
2019-02-26 21:29:34 +01:00
I named them `` inside_temperature `` and `` outside_temperature `` . You will use those references later.
By adding `` internal: true `` to the sensors they won't be published back to Home Assistant.
.. code-block :: yaml
sensor:
- platform: homeassistant
id: inside_temperature
2019-02-27 18:32:47 +01:00
entity_id: sensor.mellanvaning_temperature
2019-02-26 21:29:34 +01:00
internal: true
- platform: homeassistant
id: outside_temperature
entity_id: sensor.10_00080192969d_temperature
internal: true
Define the Fonts
***** ***** ***** *
- TrueType fonts are used. If you ever worked with fonts on microcontrollers you will love this!
2020-05-10 21:27:59 +02:00
- Save font files in `` /config/esphome `` folder where your ESPHome configuration is stored.
2019-02-27 18:32:47 +01:00
- The `` .ttf `` suffix must be lowercase and of course match your filename.
2019-02-26 21:29:34 +01:00
- Selection of fonts can be a little bit tricky for small sizes to look good. Experiment and share your findings in the comments below!
.. code-block :: yaml
font:
- file: 'slkscr.ttf'
id: font1
size: 8
- file: 'BebasNeue-Regular.ttf'
id: font2
size: 48
- file: 'arial.ttf'
id: font3
size: 14
Display Definition
***** ***** ***** ***
2019-02-27 18:32:47 +01:00
Now setup the communication to the display and start fill the screen with live data!
2019-02-26 21:29:34 +01:00
2020-05-07 15:10:51 +02:00
The `` reset_pin `` was not used in my hardware configuration as the display didn't have that pin exposed.
2019-02-26 21:29:34 +01:00
Note your `` address `` and `` model `` might be different, use the scan option to find the address of your display.
.. code-block :: yaml
i2c:
sda: D1
scl: D2
2021-07-28 23:56:11 +02:00
scan: false
2019-02-26 21:29:34 +01:00
display:
- platform: ssd1306_i2c
model: "SH1106 128x64"
reset_pin: D0
address: 0x3C
lambda: |-
// Print "Mitt Smarta Hus" in top center.
it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Mitt Smarta Hus");
// Print time in HH:MM format
2019-11-29 18:16:51 +01:00
it.strftime(0, 60, id(font2), TextAlign::BASELINE_LEFT, "%H:%M", id(esptime).now());
2019-02-26 21:29:34 +01:00
// Print inside temperature (from homeassistant sensor)
2019-02-27 18:32:47 +01:00
if (id(inside_temperature).has_state()) {
2019-02-26 21:29:34 +01:00
it.printf(127, 23, id(font3), TextAlign::TOP_RIGHT , "%.1f°", id(inside_temperature).state);
2019-02-27 18:32:47 +01:00
}
2019-02-26 21:29:34 +01:00
// Print outside temperature (from homeassistant sensor)
2019-02-27 18:32:47 +01:00
if (id(outside_temperature).has_state()) {
it.printf(127, 60, id(font3), TextAlign::BASELINE_RIGHT , "%.1f°", id(outside_temperature).state);
2019-02-26 21:29:34 +01:00
}
Rendering
---------
2021-02-14 00:37:07 +01:00
- Alignment of text can use different reference points, for example `` TOP_RIGHT `` or `` BASELINE_LEFT `` , which all are defined in :apiref: `display/display_buffer.h` .
2019-02-26 21:29:34 +01:00
- The property `` has_state() `` on a sensor is useful as it can take some seconds to get the data from Home Assistant and you may not want to display `` Nan ``
- Refer to the rendering engine :ref: `display-engine` for more features (it can draw lines and circles too!)
2019-03-05 16:28:44 +01:00
Add a Text-Based Sensor
-----------------------
Below follows an example that replaces the "Mitt smarta hem" top printout with the alarm status from the alarm component in Home Assistant.
.. code-block :: yaml
text_sensor:
- platform: homeassistant
entity_id: alarm_control_panel.my_alarm_system
name: "Alarm State"
id: alarm_state
display:
- platform: ssd1306_i2c
model: "SH1106 128x64"
reset_pin: D0
address: 0x3C
lambda: |-
// Print "Alarm State: <state>" in top center
it.printf(64, 0, id(font1), TextAlign::TOP_CENTER, "Alarm State: %s", id(alarm_state).state.c_str());
2019-02-26 21:29:34 +01:00
See Also
--------
2019-02-27 10:10:09 +01:00
- :doc: `/components/display/ssd1306`
2019-02-26 21:29:34 +01:00
- :doc: `/components/display/index`
- :doc: `/components/sensor/homeassistant`
- :ghedit: `Edit`