LCD Display and Countdown

I would like to display a time countdown on a lcd display

here is how I defined the time

how it looks like in home assistant

I exposed the countdown in the ESP

  - platform: homeassistant
    id: homeassistant_sprinklercountdown
    entity_id: timer.sprinklercountdown
    internal: false

but I’m not able to make it work

          it.printf(0, 3, "Status: %s", id(homeassistant_sprinklercountdown).state.remaining.c_str());

any ideas

You have to define the attribute in the sensor:

  - platform: homeassistant
    id: homeassistant_sprinklercountdown
    entity_id: timer.sprinklercountdown
    attribute: remaining
    internal: false

Then:

it.printf(0, 3, "Status: %s", id(homeassistant_sprinklercountdown).state.c_str());

thank @zoogara ,
I still get some error

/config/esphome/sprinkler.yaml: In lambda function:
/config/esphome/sprinkler.yaml:69:79: error: request for member 'c_str' in 'homeassistant_sprinklercountdown->esphome::homeassistant::HomeassistantSensor::<anonymous>.esphome::sensor::Sensor::state', which is of non-class type 'float'
           it.printf(0, 3, "Status: %s", id(homeassistant_sprinklercountdown).state.c_str());
                                                                               ^
*** [/data/sprinkler/.pioenvs/sprinkler/src/main.cpp.o] Error 1

What type of sensor are you defining?

I assume - because you haven’t pasted the entire yaml, that you are using sensor:. Try using text_sensor:

A bigger problem you will face is that this value will only update when the timer starts or pauses. You can see this if you look in the logs.

You may need to run your timer on ESPhome rather than HA.

Thank you I see what you mean

today I defined it as

sensor:
  - platform: homeassistant
    id: homeassistant_sprinklercountdown
    entity_id: timer.sprinklercountdown
    attribute: remaining
    internal: true

please, keep me honest here

You may need to run your timer on ESPhome rather than HA.

do you mean that I should use the countdown from homeassistant as the input for the esphome time counter?

Or something like that, if you google “home assistant timer remaining attribute does not update” you will find plenty of posts referring to this problem. The solution seems to be to pass the attribute over on start countdown, then calculate the timer yourself.

This is a post about it:

But that post doesn’t supply a solution, just points out that if you add the timer to a card it updates there… Maybe one of the other search results may help.

I’m getting closer

          it.printf(0, 2, "%s", id(homeassistant_sprinklercountdown).state.c_str());
          it.strftime(0, 3, "%H:%M:%S", id(homeassistant_time).utcnow() );

where

text_sensor:
    - platform: homeassistant
      id: homeassistant_sprinklercountdown
      entity_id: timer.sprinklercountdown
      attribute: finishes_at
      internal: true

now the trick is how subtract
"id(homeassistant_sprinklercountdown).state.c_str() - id(homeassistant_time).utcnow() "

I found it!!! thank you to @jsuanet :clap: https://community.home-assistant.io/t/esphome-ha-timer-helper-on-display/390235/6

          struct tm tm_finish{}, tm_ha_time{};
          time_t t_helper;
          double remain;
          int remain_h, remain_m, remain_s;

          strptime(id(homeassistant_sprinklercountdown).state.c_str(), "%Y-%m-%dT%T", &tm_finish); 
          t_helper = mktime(&tm_finish); 

          tm_ha_time = id(homeassistant_time).utcnow().to_c_tm(); 

          remain = difftime(t_helper, mktime(&tm_ha_time)); 

          remain_h = (int) remain/3600;
          remain_m = (int) (remain - 3600*remain_h)/60;
          remain_s = (int) remain - 3600*remain_h - 60*remain_m; 

          it.printf(0, 3, "%02d:%02d:%02d", remain_h, remain_m, remain_s); 

I will check if there is a way reduce the code, but it defenetly works!!! :partying_face:

2 Likes