bremby
(bremby)
November 5, 2022, 6:12pm
1
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
zoogara
(Daryl)
November 5, 2022, 8:37pm
2
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());
bremby
(bremby)
November 5, 2022, 10:56pm
3
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
zoogara
(Daryl)
November 6, 2022, 12:08am
4
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.
bremby
(bremby)
November 8, 2022, 6:58am
5
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?
zoogara
(Daryl)
November 8, 2022, 7:56am
6
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:
I am still new with HA but I am looking at the Timer Helper and I can see if I “start” it from the GUI the timer counts down in seconds, on the screen, like the first image
[image]
Does anyone know how I can get a similar countdown showing on a button in a dashboard?
I thought if I created a sensor and show it’s state, like below, it might work, but it doesn’t update and just shows the initial value.
friendly_name: "test timer"
value_template: "{{ state_attr('timer.test_60_se…
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.
bremby
(bremby)
November 9, 2022, 9:01pm
7
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() "
bremby
(bremby)
November 9, 2022, 9:36pm
8
I found it!!! thank you to @jsuanet 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!!!
2 Likes