#I m looking for help trying to convert

1 messages · Page 1 of 1 (latest)

gloomy tundra
#

(originally by Prax): yaml name: > [[[ return `Enabled - ${states['input_datetime.alarm_time'].state }` ]]]this will return in 24 hour time but trying to convert to 12 hour

#

So JavaScript can be finicky when it comes to date and time. Here's an example to convert the current time from 24- to 12-hours.yaml type: custom:button-card entity: none name: | [[[ var time_now_hours = new Date().getHours() var time_now_minutes = new Date().getMinutes() var am_pm = "AM" if (time_now_hours > 12) { time_now_hours = time_now_hours - 12 var am_pm = "PM" } return time_now_hours + ":" + ('00' + time_now_minutes).slice(-2) + " " + am_pm; ]]]

#

While this does, in fact, work. The card, however, will not update the time in real-time. For that, I think the best approach is just to create a template sensor. Here's one that I use (which does not meet your formatting requires but I'm just showing the example). ```yaml
sensor:

  • platform: template
    sensors:
    dashboard_time:
    unique_id: 2853690a-0b7f-4b64-876d-bcec8493cd62
    friendly_name: -H:M
    value_template: "{{ now().strftime('%-H:%M') }}"
    icon_template: mdi:clock-outline```
#

In a custom:button-card, this would show time sensor (I also have one for the date.)yaml name: >- [[[ return states["sensor.dashboard_date"].state + " / " + states["sensor.dashboard_time"].state ]]]

#

But, if you want to reference an input_datetime AND keep all the code within the card AND not make a template sensor, this will work and it updates as the time is changed elsewhere.yaml type: custom:button-card entity: none name: | [[[ var entity_state = states['input_datetime.automation_nighttime_motion_detection_end'].state var hours = entity_state.slice(0,2) var minutes = entity_state.slice(3,5) var am_pm = "AM" if (hours > 12) { var hours = hours - 12 var am_pm = "PM" } if (hours < 11 && am_pm == "AM") { hours = hours.slice(-1) } return hours + ":" + minutes + " " + am_pm ]]]

#

Screenshot shows just the state in left card on the left and the code from above in the card on the right.