diff --git a/athom-energy-monitor-x2.yaml b/athom-energy-monitor-x2.yaml index 76cb1db..363017a 100644 --- a/athom-energy-monitor-x2.yaml +++ b/athom-energy-monitor-x2.yaml @@ -335,30 +335,46 @@ text_sensor: entity_category: diagnostic device_class: timestamp + # Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds - platform: template name: "Uptime" - id: uptimes - icon: mdi:clock-start entity_category: diagnostic lambda: |- - int seconds = (id(uptime_sensor).state); + int seconds = (int) id(uptime_seconds).state; int days = seconds / (24 * 3600); - seconds = seconds % (24 * 3600); + seconds %= (24 * 3600); int hours = seconds / 3600; - seconds = seconds % 3600; - int minutes = seconds / 60; - seconds = seconds % 60; - if ( days > 3650 ) { - return { "Starting up" }; - } else if ( days ) { - return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( hours ) { - return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( minutes ) { - return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; + seconds %= 3600; + int minutes = seconds / 60; + seconds %= 60; + + if (days > 3650) { + return std::string("Starting up"); + } else if (days) { + return std::string( + std::to_string(days) + "d " + + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (hours) { + return std::string( + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (minutes) { + return std::string( + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); } else { - return { (String(seconds) +"s").c_str() }; - } + return std::string( + std::to_string(seconds) + "s" + ); + } + icon: mdi:clock-start + time: - platform: sntp diff --git a/athom-energy-monitor-x6.yaml b/athom-energy-monitor-x6.yaml index de5ec7e..322a386 100644 --- a/athom-energy-monitor-x6.yaml +++ b/athom-energy-monitor-x6.yaml @@ -535,30 +535,45 @@ text_sensor: entity_category: diagnostic device_class: timestamp + # Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds - platform: template name: "Uptime" - id: uptimes - icon: mdi:clock-start entity_category: diagnostic lambda: |- - int seconds = (id(uptime_sensor).state); + int seconds = (int) id(uptime_seconds).state; int days = seconds / (24 * 3600); - seconds = seconds % (24 * 3600); + seconds %= (24 * 3600); int hours = seconds / 3600; - seconds = seconds % 3600; - int minutes = seconds / 60; - seconds = seconds % 60; - if ( days > 3650 ) { - return { "Starting up" }; - } else if ( days ) { - return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( hours ) { - return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( minutes ) { - return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; + seconds %= 3600; + int minutes = seconds / 60; + seconds %= 60; + + if (days > 3650) { + return std::string("Starting up"); + } else if (days) { + return std::string( + std::to_string(days) + "d " + + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (hours) { + return std::string( + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (minutes) { + return std::string( + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); } else { - return { (String(seconds) +"s").c_str() }; - } + return std::string( + std::to_string(seconds) + "s" + ); + } + icon: mdi:clock-start time: - platform: sntp diff --git a/athom-garage-door.yaml b/athom-garage-door.yaml index e251028..99e336b 100644 --- a/athom-garage-door.yaml +++ b/athom-garage-door.yaml @@ -215,28 +215,44 @@ text_sensor: entity_category: diagnostic device_class: timestamp + # Creates a sensor of the uptime of the device, in formatted days, hours, minutes and seconds - platform: template name: "Uptime" entity_category: diagnostic lambda: |- - int seconds = (id(uptime_sensor).state); + int seconds = (int) id(uptime_seconds).state; int days = seconds / (24 * 3600); - seconds = seconds % (24 * 3600); + seconds %= (24 * 3600); int hours = seconds / 3600; - seconds = seconds % 3600; - int minutes = seconds / 60; - seconds = seconds % 60; - if ( days > 3650 ) { - return { "Starting up" }; - } else if ( days ) { - return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( hours ) { - return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( minutes ) { - return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; + seconds %= 3600; + int minutes = seconds / 60; + seconds %= 60; + + if (days > 3650) { + return std::string("Starting up"); + } else if (days) { + return std::string( + std::to_string(days) + "d " + + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (hours) { + return std::string( + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (minutes) { + return std::string( + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); } else { - return { (String(seconds) +"s").c_str() }; - } + return std::string( + std::to_string(seconds) + "s" + ); + } icon: mdi:clock-start time: diff --git a/athom-mini-relay-v2.yaml b/athom-mini-relay-v2.yaml index 339a285..af3379f 100644 --- a/athom-mini-relay-v2.yaml +++ b/athom-mini-relay-v2.yaml @@ -315,24 +315,39 @@ text_sensor: name: "Uptime" entity_category: diagnostic lambda: |- - int seconds = (id(uptime_sensor).state); + int seconds = (int) id(uptime_seconds).state; int days = seconds / (24 * 3600); - seconds = seconds % (24 * 3600); + seconds %= (24 * 3600); int hours = seconds / 3600; - seconds = seconds % 3600; - int minutes = seconds / 60; - seconds = seconds % 60; - if ( days > 3650 ) { - return { "Starting up" }; - } else if ( days ) { - return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( hours ) { - return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( minutes ) { - return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; + seconds %= 3600; + int minutes = seconds / 60; + seconds %= 60; + + if (days > 3650) { + return std::string("Starting up"); + } else if (days) { + return std::string( + std::to_string(days) + "d " + + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (hours) { + return std::string( + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (minutes) { + return std::string( + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); } else { - return { (String(seconds) +"s").c_str() }; - } + return std::string( + std::to_string(seconds) + "s" + ); + } icon: mdi:clock-start time: diff --git a/athom-smart-plug.yaml b/athom-smart-plug.yaml index c1c2f7c..0a1f78f 100644 --- a/athom-smart-plug.yaml +++ b/athom-smart-plug.yaml @@ -350,24 +350,39 @@ text_sensor: name: "Uptime" entity_category: diagnostic lambda: |- - int seconds = (id(uptime_sensor).state); + int seconds = (int) id(uptime_seconds).state; int days = seconds / (24 * 3600); - seconds = seconds % (24 * 3600); + seconds %= (24 * 3600); int hours = seconds / 3600; - seconds = seconds % 3600; - int minutes = seconds / 60; - seconds = seconds % 60; - if ( days > 3650 ) { - return { "Starting up" }; - } else if ( days ) { - return { (String(days) +"d " + String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( hours ) { - return { (String(hours) +"h " + String(minutes) +"m "+ String(seconds) +"s").c_str() }; - } else if ( minutes ) { - return { (String(minutes) +"m "+ String(seconds) +"s").c_str() }; + seconds %= 3600; + int minutes = seconds / 60; + seconds %= 60; + + if (days > 3650) { + return std::string("Starting up"); + } else if (days) { + return std::string( + std::to_string(days) + "d " + + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (hours) { + return std::string( + std::to_string(hours) + "h " + + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); + } else if (minutes) { + return std::string( + std::to_string(minutes) + "m " + + std::to_string(seconds) + "s" + ); } else { - return { (String(seconds) +"s").c_str() }; - } + return std::string( + std::to_string(seconds) + "s" + ); + } icon: mdi:clock-start time: