I have a sensor that does uptime. That comes in seconds. But I would like to have it in a neat format, with number of days, hours, minutes and seconds. I have tried to start something, but it doesn’t work. This is the code for the root sensor, uptime:
- platform: uptime
name: "Oppetidssensor"
type: seconds
update_interval: 10s
id: uptime_sensor
entity_category: "diagnostic"
internal: true
I can convert that to hours (which is wonky without decimals, because when it is over 30 minutes it shows the full hour):
- platform: copy
source_id: uptime_sensor
name: "Oppetid i timer"
filters:
- lambda: return x / 3600.0;
unit_of_measurement: " H"
entity_category: "diagnostic"
device_class: ""
id: oppetiditimer
Same thing for minutes:
- platform: copy
source_id: uptime_sensor
name: "Oppetid i minutter"
filters:
- lambda: return x / 60.0;
unit_of_measurement: " m"
entity_category: "diagnostic"
device_class: ""
id: oppetidiminutter
And I made a copy sensor for seconds as well to have something to work out from and keep the original sensor internal:
- platform: copy
source_id: uptime_sensor
name: "Oppetid i sekunder"
filters:
- lambda: return x / 1.0;
accuracy_decimals: 0
unit_of_measurement: " s"
entity_category: "diagnostic"
device_class: ""
id: oppetidisekunder
But when I try to do math with these, as a first step to remove the hours and minutes, so only the seconds that are left when the minutes and hours are subtracted, it doesn’t work:
- platform: template
id: oppetid
update_interval: 20s
lambda: 'return id(int(oppetidisekunder).state) - (id(int(oppetiditimer).state) * 360) - (id(int(oppetidiminutter).state) * 60);'
Compiling .pioenvs/hekk/src/main.cpp.o
/config/hekk.yaml: In lambda function:
/config/hekk.yaml:787:39: error: request for member 'state' in '(int)((esphome::copy::CopySensor*)oppetidisekunder)', which is of non-class type 'int'
787 | lambda: 'return id(int(oppetidisekunder).state) - (id(int(oppetiditimer).state) * 360) - (id(int(oppetidiminutter).state) * 60);'
| ^~~~~
/config/hekk.yaml:787:71: error: request for member 'state' in '(int)((esphome::copy::CopySensor*)oppetiditimer)', which is of non-class type 'int'
787 | lambda: 'return id(int(oppetidisekunder).state) - (id(int(oppetiditimer).state) * 360) - (id(int(oppetidiminutter).state) * 60);'
| ^~~~~
/config/hekk.yaml:787:113: error: request for member 'state' in '(int)((esphome::copy::CopySensor*)oppetidiminutter)', which is of non-class type 'int'
787 | lambda: 'return id(int(oppetidisekunder).state) - (id(int(oppetiditimer).state) * 360) - (id(int(oppetidiminutter).state) * 60);'
| ^~~~~
*** [.pioenvs/hekk/src/main.cpp.o] Error 1
========================= [FAILED] Took 33.89 seconds =========================
I’m obviously doing it all wrong, but I can’t understand why, and I have no idea how to fix it. Can somebody please help me?


