I need help creating a time object.
So let’s say I have some kind of homemade device from which I receive data over the RS485 network and
I create two templates: slave6_time
and slave6_day
,
and collect from them the total time template: slave6_time_day
(example 1).
slave6_time:
friendly_name: 'RTC Время'
value_template: "{{ states('sensor.slave6').split(',')[2] | int }}:{{ states('sensor.slave6').split(',')[3] | int }}"
icon_template: mdi:clock
slave6_day:
friendly_name: 'RTC день недели'
value_template: >-
{%- set day_num = ["ПН", "ВТ", "СР", "ЧТ", "ПТ", "СБ", "ВС"] %}
{%- set day_of_week = day_num[ (states('sensor.slave6').split(',')[1] | int)-1 ] %}
{{ day_of_week }}
icon_template: mdi:calendar
slave6_time_day:
friendly_name: 'RTC День, Время'
value_template: "{{ states('sensor.slave6_day') }}-{{ states('sensor.slave6_time') }}"
icon_template: mdi:calendar-clock
All descriptions of the time object that I could find are considered starting from what I take
system time and format it. And it works (example 2).
system_day_string:
friendly_name: 'Системное время день'
value_template: >-
{%- set day_num = ["ПН", "ВТ", "СР", "ЧТ", "ПТ", "СБ", "ВС"] %}
{%- set day_of_week = day_num[ (states('sensor.system_day') | int)-1 ] %}
{{ day_of_week }}
icon_template: mdi:calendar
system_time:
friendly_name: 'Системное время'
value_template: "{{ as_timestamp(states('sensor.date_time_iso')) | timestamp_custom('%H:%M') }}"
icon_template: mdi:clock
system_time_day:
friendly_name: 'Системное время День, Время'
value_template: "{{ states('sensor.system_day_string') }}-{{ states('sensor.system_time') }}"
icon_template: mdi:calendar-clock
The system time is reflected in the GUI correctly,
and the time from my homemade device is read ugly,
because it is not a time object, but just a text string. (example 3).
I tried to use both input_datetime
: and template using input_datetime
values: (example 4)
input_datetime:
slave6_time:
name: 'RTC Время'
has_date: false
has_time: true
initial: "21:55"
# initial: "{{ states('sensor.slave6').split(',')[2] | int }} : {{ states('sensor.slave6').split(',')[3] | int }}" #out of order
sensor:
- platform: template
sensors:
slave6_custom_time:
friendly_name: 'RTC custom'
device_class: 'timestamp'
# value_template: "{{ states('input_datetime.slave6_time') }}"
value_template: "21:55:0"
# attributes:
# hour: "{{ states('sensor.slave6_time_hour') | int}}"
but I don’t know how to assign values from my device to the hour: and minute: attributes,
Or maybe I’m going the wrong way, please tell me:
how to make a custom time object with values from my device,
constantly updating its parameters with the arrival of new time data?