Converting Decimal value from Modbus device

Hi all,

I am still working on the Modbus communication between my Siemens LOGO and HomeAssistant.
So far, most of the things I wanted to implement are working - but now, I am playing around with some additional features.

So - basically, my LOGO is running fully automated - turning the lights in my terrarium on and off based on an astronomical clock.

Now, I already know how to read some of the variables my logo is using - for example, the time settings when the light will be turned on - and off…
The problem here is, that the LOGO does provide these values in decimal values…

Example:
Sunrise = 1799
Sunset = 5940

For sure, this isn’t going to work in my Dashboard - and I want to convert these into a readable format…
The best option would be, if I can do this directly within the yaml code for the sensor, since I can define the device_class there.

      - name: Sonnenaufgang (t1)
        unique_id: "t1_sunrise"
        address: 0
        input_type: holding
        #device_class: timestamp
        scan_interval: 10
        swap: word

      - name: Sonnenuntergang (t1)
        unique_id: "t1_sunset"
        address: 1
        input_type: holding
        #device_class: timestamp
        scan_interval: 10
        swap: word

I already found some information on how to convert this decimal value into a string … (I’ve done this with NodeRed previously, but want to get rid of the NodeRed implementation)

in node red:

var asHex = parseInt(msg.payload).toString("16").padStart(4,"0");
var hh = asHex.substring(0,2);
var mm = asHex.substring(2,4);
msg.payload = `${hh}:${mm}`
return msg;

this will then provide a payload like: 17:34 or 07:04 - but only as string.

So: the goal:
I want to format the incomming decimal value into a ISO timestamp string.
Unfortunaltey, I don’t think that the modbus-plattform does support something like a value formatting…?
( in this case… why isn’t such option available? I could imagine, that many Modbus devices are using formats as decimal, hex or whatever to deliver the raw data…)

in HA try
as_timestamp(value)

but where exactly?
I’m not sure if I can directly use this within the Modbus Sensor definition?

      - name: Sonnenaufgang (t1)
        unique_id: "t1_sunrise"
        address: 0
        input_type: holding
        #device_class: timestamp
        scan_interval: 10
        swap: word

This needs to be a template then, right?

No personal experience with Modbus, but the docs don’t specify that templates are possible.

You’d need a template sensor — this one does the same job as the NodeRed code above, I think:

template:
  - sensor:
      - name: Sonnenaufgang
        device_class: timestamp
        value_template: >
          {% set x = ('%04x' % states('sensor.sonnenaufgang_t1')|int(0)) %}
          {{ today_at('%s:%s' % (x[:2],x[-2:])) }}

Template is probably the way to go or do it in NodeRed maybe.

I have a Date/Time conversion node listed here that might give you some ideas.

thanks all :slight_smile:
I’ll probably go with the template in HA… NodeRed doesn’t make sense, when I am switching my current NodeRed stuff to get the data from the LOGO to HomeAssistant built in functionallities :slight_smile:

It would be really great, if HA would support value formatting directly within the ModbusIntegration, because I think LOGO will not be the only device that provides some data in specific formats that are not “human readable” out of the box.
Therefore creating sensors - and then additional templates are possible, but not really a nice way to go … usually, such formatting will be done within an integration → therefore, the Modbus Platform should support value-formatting options by default in my opinion :slight_smile:

I think you are completely right with most modbus devices not supporting human readable outputs.
In fact I think it will be very rare that the output is human readable, so I suggest you make a feature request for that.
Remember to vote for your own feature request. :wink:

already did this :slight_smile: