Read and set time for modbus register which need DEC values

Hey folk,
I need your help for my problem as following:

I want to read and set a time register for my inverter via modbus but i do not how to translate the values for read/set for the input_datetime field.
The register (uint16) itsself is read-writeable and contains e,g, a value “773” [Dec] for 03:05 am which is also ‘0305’ (Hex). hh:mm.

My code is for reading:

input_datetime:

  only_time:
    name: gw25_my_47515_starttime_1
    has_date: false
    has_time: true
    icon: mdi:calendar-clock

automation:

# Start Time1  reading
  - id: gw25_mb_47515_rw_ecostarttime_1_read
    alias: GW25 Start Time 1 State - Read
    trigger:
      - platform: state
        entity_id: sensor.gw25_mb_47515_rw_ecostarttime_1_state
      - platform: time_pattern
        hours: '*'
        minutes: '*'
        seconds: '/15'      
    action:
      service: input_datetime.set_datetime
      target:
        entity_id: input_datetime.gw25_my_47515_rw_ecostarttime_1
      data:
        option: "{{ states('sensor.gw25_mb_47515_rw_ecostarttime_1_state') }}"

ok, the “data option” from the sensor is ‘773’ but I need it as 03:05 for my field.

Later on I want to set the time to 05:15 which is 1295 [Dec] or '050F [Hex] but how to do the translation of the value.
Many thanks in advance for help.

Edit:
Value to change for reading “Dec->Hex>hh:mm”
and for writing “hh:mm->Hex->Dec”

Try this:

Read

      data:
        option: >-
          {% set raw_value = states('sensor.gw25_mb_47515_rw_ecostarttime_1_state') | int %}
          {% set hours = (raw_value // 256) | bitwise_and(0xFF) %}
          {% set minutes = raw_value | bitwise_and(0xFF) %}
          {{ '{:02}:{:02}'.format(hours, minutes) }}

Set

  action:
    service: modbus.write_register
    data:
      hub: your_modbus_hub
      unit: 1
      address: 47515
      value: >-
        {% set time = states('input_datetime.gw25_my_47515_newtime') %}
        {% set hours, minutes, seconds = time.split(':') | map('int') %}
        {{ (hours * 256) + minutes }}

Test in devtools

WRITE:
{%- set time = '03:05:00' %}
{%- set hours, minutes, seconds = time.split(':') | map('int') %}
input (str): {{time}}
hours: {{hours}}
mins: {{minutes}}
output: {{ (hours * 256) + minutes }}


READ:
{%- set raw_value = 773 %}
{%- set hours = (raw_value // 256) | bitwise_and(0xFF) %}
{%- set minutes = raw_value | bitwise_and(0xFF) %}
input (int): {{raw_value}}
hours: {{hours}}
mins: {{minutes}}
output: {{ '{:02}:{:02}'.format(hours, minutes) }}

Obviously create the gw25_my_47515_newtime sensor. The input_datetime time only sensors return ‘HH:MM:SS’, so we catch it in the split, but we dont use the seconds var.

many many thanks, i will try it tomorrow and i come back here

Dear baudneo,

well, your script for the conversion runs super, thanks :slight_smile:

But now my “Input_datetime sensor” did not refresh anyway and as you wrote, that i need another input-sensor esp. for hh:mm. but i have no idea how to do. May be you can help me again?