- sensor:
    - name: "LOGO Czas PLC"
      unique_id: logo_czas_plc
      device_class: timestamp
      state: >
        {% set raw = states('sensor.logo_rtc_raw_bytes').split(',') %}
        {% if raw | length == 8 %}
          {% set dt_str = '20%02d-%02d-%02d %02d:%02d:%02d' | format(
            raw[1] | int,
            raw[2] | int,
            raw[3] | int,
            raw[4] | int,
            raw[5] | int,
            raw[6] | int
          ) %}
          {{ strptime(dt_str, '%Y-%m-%d %H:%M:%S') | as_local }}
        {% else %}
          unknown
        {% endif %}

    - name: "LOGO Odchyłka czasu NTP"
      unique_id: logo_odchylka_czasu_ntp
      unit_of_measurement: "s"
      state: >
        {% set plc_time = states('sensor.logo_czas_plc') | as_datetime %}
        {% if plc_time is not none %}
          {{ (now() - plc_time).total_seconds() | abs | round(1) }}
        {% else %}
          unknown
        {% endif %}
