diff --git a/src/common/datetime/format_duration.ts b/src/common/datetime/format_duration.ts new file mode 100644 index 0000000000..c05c890a39 --- /dev/null +++ b/src/common/datetime/format_duration.ts @@ -0,0 +1,28 @@ +import { HaDurationData } from "../../components/ha-duration-input"; + +const leftPad = (num: number) => (num < 10 ? `0${num}` : num); + +export const formatDuration = (duration: HaDurationData) => { + const d = duration.days || 0; + const h = duration.hours || 0; + const m = duration.minutes || 0; + const s = duration.seconds || 0; + const ms = duration.milliseconds || 0; + + if (d > 0) { + return `${d} days ${h}:${leftPad(m)}:${leftPad(s)}`; + } + if (h > 0) { + return `${h}:${leftPad(m)}:${leftPad(s)}`; + } + if (m > 0) { + return `${m}:${leftPad(s)}`; + } + if (s > 0) { + return `${s} seconds`; + } + if (ms > 0) { + return `${ms} milliseconds`; + } + return null; +}; diff --git a/src/data/script_i18n.ts b/src/data/script_i18n.ts index cfafa5abe8..7343249131 100644 --- a/src/data/script_i18n.ts +++ b/src/data/script_i18n.ts @@ -1,3 +1,4 @@ +import { formatDuration } from "../common/datetime/format_duration"; import secondsToDuration from "../common/datetime/seconds_to_duration"; import { ensureArray } from "../common/ensure-array"; import { computeStateName } from "../common/entity/compute_state_name"; @@ -98,7 +99,7 @@ export const describeAction = ( computeEntityRegistryName(hass, entityReg) || targetThing ); } else { - targets.push(targetThing); + targets.push("unknown entity"); } } } else if (key === "device_id") { @@ -106,14 +107,14 @@ export const describeAction = ( if (device) { targets.push(computeDeviceName(device, hass)); } else { - targets.push(targetThing); + targets.push("unknown device"); } } else if (key === "area_id") { const area = hass.areas[targetThing]; if (area?.name) { targets.push(area.name); } else { - targets.push(targetThing); + targets.push("unknown area"); } } else { targets.push(targetThing); @@ -140,7 +141,7 @@ export const describeAction = ( ? "based on a template" : `for ${config.delay}`; } else { - duration = `for ${JSON.stringify(config.delay)}`; + duration = `for ${formatDuration(config.delay)}`; } return `Delay ${duration}`;