- 积分
- 4847
- 金钱
- 4073
- 威望
- 0
- 贡献
- 0
- HASS币
- 0
论坛元老
Freddy.
- 积分
- 4847
- 金钱
- 4073
- HASS币
- 0
|
本帖最后由 南柯醉一梦丶 于 2019-4-29 14:44 编辑
【前提】
需要组件systemmonitor 并开启 last_boot 参数(时间戳的格式)
【效果】
【代码】
- platform: template
sensors:
since_last_boot:
friendly_name: 系统运行
value_template: >-
{% set up_time = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}
{% set days = (up_time // (60 * 60 * 24)) %}
{% set weeks = (days// 7) %}
{% set hours = (up_time// (60 * 60)) - days * 24 %}
{% set minutes = (up_time // 60) - (days * 24 * 60) %}
{% set days = ((up_time // (60 * 60 * 24))) - (weeks * 7) %}
{% set minutes = (minutes) - (hours * 60) %}
{% macro phrase(value, name) %}
{%- set value = value | int %}
{{-'{}{}{}'.format(value,name,end) if value | int > 0 else''}}
{%- endmacro %}
{% set text = [ phrase(weeks,'周'),phrase(days,'天'), phrase(hours,'小时'), phrase(minutes,'分钟') ] | select('!=','') | list | join('') %}
{{ text }}
- platform: template
sensors:
start_boot:
friendly_name: 开机时间
value_template: >-
{{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%Y-%m-%d ') }}{{ as_timestamp(states('sensor.last_boot')) | timestamp_custom('%H:%M:%S') }} |
评分
-
查看全部评分
|