Changing a decimal value into a hex value (jinja)

templates
Tags: #<Tag:0x00007f47fb40bf88>

#1

In the jinja template, I can do this:

{{ '%0x' % 255 }}

to convert 255 into hex ‘ff’

I’m trying to get the same with this value

{{ '%0x' % states.input_slider.mote_value_red }}

But it comes up empty, what am I doing wrong?


#2

I find that I often need tell templates the type of the var. It’s possible that the state is a string. Run it through the “float” filter first and see if that helps!


#3

Thanks I’ll give that a go!

I’ve just installed AppDaemon so I reckon I’ll try that out too.

Yaml is leaving me a little syntax’d out :wink:


#4

Hi there

I have a similar issue - I am trying to create a RESTful sensor to give me a temperature sensor.

  • platform: rest
    resource: http://192.168.1.109:1234/@*****?/
    name: Temperature
    value_template: '{{ “%+0x” | format(value_json.data[4:6] | int }}'
    unit_of_measurement: “°C”

The device returns the temperature in hexadecimal format. I then need to convert it to a decimal number.

I tried the above but it seems the json data is returned as a string and so the conversion fails. Typical data that is returned from the sensor is “1D” for 29 degrees Celcius. Would appreciate your assistance!


#5

When you’re experimenting on this, remember that you can use the template dev tool to test your formatting.

{% set value = "VAL RETURNED BY SENSOR" %}
{{ "%+0x" | format( value | int }}

Input slider to HEX MQTT message
#6

Hi, thanks for that. I actually didn’t realise that it was possible to test from within HASS!
The following worked for me:

{% set value = “VAL RETURNED BY SENSOR” %}
{{ value | int(value,16) }}


#7

I was just parroting back the template you were using above. My point is that you can do a lot of testing with the template dev tool. Faster than rebooting each iteration.


#8

First, thank you to everyone for sharing your thoughts. It has been helpful.

I’m working on pulling electricity demand from a Rainforest Eagle Energy Monitor API. I’ve been trying to work through a value template but can’t seem to figure it out. I’m trying to pull (hex) data from a rest api (that part works = 0x0001e6) and then convert the hex to an integer which does not work (- unknown). I sense i’m missing something but I cannot figure it out. Any thoughts?

  - platform: rest
    name: instant demand
    resource: http://192.168.###.###/cgi-bin/post_manager?Content-Type=application/x-www-form-urlencoded HTTP/1.1
    method: POST
    payload: '{ <Command><Name>get_instantaneous_demand</Name><MacId>#############</MacId><Format>JSON</Format>
</Command> }'
    headers:
      Content-Type: application/x-www-form-urlencoded
    value_template: '{{ value_json["InstantaneousDemand"]["Demand"]}}'

  - platform: template
    sensors:
      energy_meter:
        value_template: '{{ (sensor.instant_demand) | int((sensor.instant_demand),16) }}'
        #value_template: '{{ value | int(value,16)}}'
        friendly_name: Energy Meter
        unit_of_measurement: 'kW'