Here is working jinja template:
{% set lt1 = 'light.livingroom_light_01' %}
{% set lt2 = 'light.livingroom_light_02' %}
{% set l1wc = 'sensor.livingroom_light_01_white_cool_power' %}
{% set l1ww = 'sensor.livingroom_light_01_white_warm_power' %}
{% set l1n1 = 'sensor.livingroom_light_01_night_01_power' %}
{% set l1n2 = 'sensor.livingroom_light_01_night_02_power' %}
{% set l2wc = 'sensor.livingroom_light_02_white_cool_power' %}
{% set l2ww = 'sensor.livingroom_light_02_white_warm_power' %}
{% set l2n1 = 'sensor.livingroom_light_02_night_01_power' %}
{% set l2n2 = 'sensor.livingroom_light_02_night_02_power' %}
{% if is_state(lt1, 'off') %}
{%- set _ = states('sensor.time') %}
{%- set t = as_timestamp(states.light.livingroom_light_01.last_updated) %}
{%- if t != None %}
{%- set n = now().timestamp() %}
{%- set d = n - t %}
{%- set midnight_today = as_timestamp(strptime(now().date() | string, "%Y-%m-%d")) %}
{%- set midnight_yesterday = midnight_today - 86400 %}
{%- set midnight_week_ago = midnight_today - 604800 %}
{%- if d < 60 %}
{{ d | int }} sec ago
{%- elif d >= 60 and d < 120 %}
1 min ago
{%- elif d < 3600 %}
{{ (d // 60) | int }} min ago
{%- elif d < n - midnight_today %}
Today at {{ t | timestamp_custom('%H:%M') }}
{%- elif d < n - midnight_yesterday %}
Yesterday at {{ t | timestamp_custom('%H:%M') }}
{%- elif d < n - midnight_week_ago %}
{{ t | timestamp_custom('%A at %H:%M') }}
{%- else %}
{{ t | timestamp_custom('%Y-%m-%d %H:%M:%S') }}
{%- endif %}
{% else %}
unknown
{% endif %}
{% elif is_state(lt1, 'on') %}
{{ states(l1wc) | round(1, 'floor') + states(l1ww) | round(1, 'floor') + states(l1n1) | round(1, 'floor') + states(l1n2) | round(1, 'floor')
+ states(l2wc) | round(1, 'floor') + states(l2ww) | round(1, 'floor') + states(l2n1) | round(1, 'floor') + states(l2n2) | round(1, 'floor') }}W
{% else %}
{% endif %}
I try like this but only works when light is on (something is wrong in time calculation script when light is off):
type: custom:button-card
variables:
name: KOPALNICA
lt1: light.livingroom_light_01
lt2: light.livingroom_light_02
l1wc: sensor.livingroom_light_01_white_cool_power
l1ww: sensor.livingroom_light_01_white_warm_power
l1n1: sensor.livingroom_light_01_night_01_power
l1n2: sensor.livingroom_light_01_night_02_power
l2wc: sensor.livingroom_light_02_white_cool_power
l2ww: sensor.livingroom_light_02_white_warm_power
l2n1: sensor.livingroom_light_02_night_01_power
l2n2: sensor.livingroom_light_02_night_02_power
name: '[[[ return variables.name ]]]'
styles:
card:
- height: 100px
- background: transparent
name:
- font-size: 14px
grid:
- grid-template-areas: '"n n" "n n" "temp temp"'
- grid-template-columns: 1fr 1fr
- grid-template-rows: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr
custom_fields:
temp:
- font-size: 12px
- color: grey
humi:
- font-size: 12px
- color: grey
lux:
- font-size: 12px
- color: grey
wt:
- font-size: 12px
- color: grey
custom_fields:
temp: |
[[[
if (states[variables.lt1].state == 'on') {
_ = states('sensor.time');
t = as_timestamp(states[variables.lt1].last_updated);
if (t != None) {
n = now().timestamp();
d = n - t;
midnight_today = as_timestamp(strptime(now().date() | string, "%Y-%m-%d"));
midnight_yesterday = midnight_today - 86400;
midnight_week_ago = midnight_today - 604800;
if (d < 60) {
return (d | int) + 'sec ago';
}
else if ((d >= 60) & (d < 120)) {
return '1 min ago';
}
else if (d < 3600) {
return ((d / 60) | int) +'min ago';
}
else if (d < n - midnight_today) {
return 'Today at' + (t | timestamp_custom('%H:%M'));
}
else if (d < n - midnight_yesterday) {
return 'Yesterday at' + (t | timestamp_custom('%H:%M'));
}
else if (d < n - midnight_week_ago) {
return (t | timestamp_custom('%A at %H:%M'));
}else{
(t | timestamp_custom('%Y-%m-%d %H:%M:%S'));
}
}else{
return 'unknown';
}
}
else if (states[variables.lt1].state == 'off') {
return ( states[variables.l1wc].state) + 'W';
}else {
}
]]]