【问题标题】:Unable to format timestamp sensor in home assistant / hass.io using strptime function无法使用 strptime 函数在家庭助理/hass.io 中格式化时间戳传感器
【发布时间】:2020-01-20 11:04:15
【问题描述】:

我正在尝试显示一个传感器状态值,它属于“时间戳”类型,格式为 %H:%M,但无法让 strptime 函数按我预期的方式工作。

例如当我将以下内容添加到“模板”部分时,在开发工具中

sunrise {{ strptime(state_attr("sun.sun", "next_rising"), "%H:%M") }} 

它仍然将输出显示为:

sunrise 2020-01-20T19:05:26+00:00 

【问题讨论】:

    标签: home-assistant


    【解决方案1】:

    每位家庭助理Templating documentation

    • strptime(string, format) 会将字符串解析为基于格式的日期时间。

    所以这并不是你所需要的。

    您可能需要应用timestamp_custom(format_string, local_boolean) filter 将时间戳转换为自定义格式,例如:

    {{ as_timestamp(state_attr("sun.sun", "next_rising")) | timestamp_custom('%H:%M') }}

    【讨论】:

      猜你喜欢
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2019-02-17
      相关资源
      最近更新 更多