Display time from HA template on ESPHome display

I have a template sensor that displays the time half an hour before sun set.

sensor:
- platform: template
  sensors:
    sun_sun_time_sunset_offset_template:
      value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_setting) -1800) | timestamp_custom("%H:%M") }}'

I want to display it on a SSD1306 display on an ESPHome managed nodemcu.

In ESPHome I defined the sensor:

sensor:
  - platform: homeassistant
    id: sun_sun_time_sunset_offset_template
    entity_id: sensor.sun_sun_time_sunset_offset_template
    internal: true

and added this sensor to the display lambda

lambda: |-
  it.printf(  0,  8, id(my_font1), "%s", id(sun_sun_time_sunset_offset_template).state);

The error I get is:

src/main.cpp: In lambda function:
src/main.cpp:466:84: warning: format '%s' expects argument of type 'char*', but argument 6 has type 'double' [-Wformat=]
       it.printf(  0,  8, my_font1, "%s", sun_sun_time_sunset_offset_template->state);

I don’t understand how to set it correct.

1 Like

Try with:

lambda: |-
  it.printf(  0,  8, id(my_font1), "%s", id(sun_sun_time_sunset_offset_template).state.c_str());

It will change type to string

Thanks for the reply.
Changed it and I get the following error.

src/main.cpp:482:85: error: request for member 'c_str' in 'sun_sun_time_sunset_offset_template->esphome::homeassistant::HomeassistantSensor::<anonymous>.esphome::sensor::Sensor::state', which is of non-class type 'float'
       it.printf(  0,  8, my_font1, "%s", sun_sun_time_sunset_offset_template->state.c_str());
                                                                                     ^
*** [.pioenvs/nodemcu01/src/main.cpp.o] Error 1

After also changing from sensor to a text_sensor. I don’t get any errors.

1 Like