Decimal to Hex?

Hi,
I have a device that I can pull data from using a local API. There is one problem: I uses HEX values. So for example: 0D0807E7 = 13.08.2023 (Aug. 13th 2023). If I want to pull the data from the current date, I need to send an API-command that includes the current date in a HEX-string (plus some letters before the actual date-string).

Long story short: Does anybody know how I can “easily” convert the current date into the HEX format needed?

I was thinking of creating a huge template sensor that “simply” pulls the current date and then parses the individual numbers into HEX on a “manual” basis (i.e. day, month, year) and then puts them back together again based on a “library” of decimal → hex numbers. But that seems overly complicated and hardcoding something never sounded like a good idea…

Any ideas would be highly welcome!

Thanks!

I can do this in the template editor.

{{ 0x0D0807E7 }}

or

{{ ‘%0x’ % 218630119 }}

Remember that when using values from states and templates, then explicitly typeconvert them, like this.

{{ ‘%0x’ % (states(‘sensor.date’) | int) }}

1 Like

That lets me convert the hex into the decimal. I need it the other way around… I want to send the device a hex value. (For example to send the above mentioned date, I’d have to send “http://192.168.0.133/api/rest/FB000D0807E7”. The “FB00” at the beginning of the hex-string are the device code for “give me the value of this parameter” and then the “0D0807E7” is the date.) So for today (May 16th 2024) the code would need to be “FB00100507E8”)

Just saw your edit. That could work. Thanks!

Perhaps @WallyR you have an idea… If I use your solution to get the current date, it spits out 1057e8 . Perfect. However, I need each “figure” to be a multiple of 2 bits long. i.e. “05” instead of “5” or “07e8” instead of “7e8”. Do you know of an easy way to achieve this?
Thanks so much!

FB00{{ '%02X' % now().day }}{{ '%02X' % now().month }}{{ '%04X' % now().year }}
1 Like