Modbus PLC YAML help

Maybe you have to wait for the TCP connection to time out or something. Instead of a restart, try stopping HA, wait for 15 seconds or so and start it again.

Thank you for you suggestion.
Getting back to the template issue. I did find this error in the log that I missed before. Any idea?

Template variable error: 'sensor' is undefined when rendering '{{ "%0x" % (states(sensor.pool_temp) | int) }}'

TemplateError('UndefinedError: 'sensor' is undefined') while processing template 'Template("{{ "%0x" % (states(sensor.pool_temp) | int) }}")' for attribute '_attr_state' in entity 'sensor.hex_value'

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 389, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1358, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.9/site-packages/jinja2/sandbox.py", line 326, in getattr
    value = getattr(obj, attribute)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1382, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1374, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: 'sensor' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 505, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 391, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'sensor' is undefined



`modbus: 
  - name: hub1
    type: tcp
    host: 192.168.1.243    
    port: 502

`    sensors:
      - name: Pool Temp
        address: 1024
        scan_interval: 10
        slave: 255       
        data_type: int # int or float
        unit_of_measurement: "Ā°F"
        
template:
  - sensor:
      - name: "hex value"
        unit_of_measurement: "Ā°F"
        state: '{{ "%0x" % (states(sensor.pool_temp) | int) }}'

sensor.pool_temp inside states() needs to be inside quotes to be a string (otherwise, the template is looking for a variable named sensor, which does not exist).

Like this?

state: '{{ "%0x" % (states("sensor.pool_temp") | int) }}'

Yes, that should work.

You rockā€¦
That did it.
I now need to add a decimal to the value of the temp sensor. It is supposed to read 74.3

hex

Maybe something like this:

state: '{{ ("%0x" % (states("sensor.pool_temp") | int)) | int / 10 }}'

boom

Boomshakalakaā€¦ That was itā€¦HAHA

If you lived close Id take you out for a few beers. Thanks so much.

How do you know this coding? Is there a site online to learn this or to understand these formulas?

FWIW, you can specify the number base in the int function.

state: "{{ ('0x' ~ states('sensor.pool_temp')) | int(base=16) / 10 }}"

Blockquote
FWIW, you can specify the number base in the int function.

Thank you. I will experiment with all of this.

You can test your templates in developer tools in Home Assistant.
You can take a look at the templating documentation.

This seems to convert the other way around.

{% set my_state = 53 %}
{{ ('0x' ~ my_state) | int(base=16) / 10 }} returns 8.3
{{ ("%0x" % (my_state | int)) | int / 10 }} return 3.5

But Iā€™m sure my terrible template could be simplified.

Home Assistantā€™s templates use the Jinja2 templating language. Hereā€™s the documentation for the int filter.
https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.int

Hex 53 is decimal 83. Divided by 10 is 8.3.

Decimal 53 is hex 35.

And thatā€™s what OP wants, right?

1 Like

Right! My mistake. The OP has another topic where the conversion is hex to decimal and I got my wires crossed!

I donā€™t think a, b, c, d, e, or f are valid temperatures. I think this has lead op astray as heā€™s having issues with this template currently in another tread.

I know that. However, OP specifically wanted to convert it this way.

0x2329 = 9001.

It seemed really odd, but I though OP knew what they were doing.

I apologize for any confusion. I do not know what I am doing when it comes to HA and YAML lol. I created another post today because I thought everything we did here was good. I did an update yesterday and it didnā€™t like the fact the Modbus template was using int and it was not being specified as something else in the template. I thought it was related to a different problem. Hmm.

Well, as I said in the other thread, I think we need to take a step back and reverse engineer the exepcted result. For that to happen we need a few points of data, which youā€™ll have to collect.

Youā€™ll need an outside temperature reading and the value thatā€™s coming from your modbus sensor.outside_temphex. Take 3 of these readings throughout the day. More readings are better.

Once you have that information, you can make a template sensor or use the compensation integration to get the correct temperature.

i.e.
we need to fill in the following table:

sensor.outside_temphex temperature outside
hex1 temp1
hex2 temp2
hex(n-1) temp(n-1)
hex(n) temp(n)

If you have the ability, youā€™d want to fill out the same table for your pool temp. I would assume thatā€™s a bit harder to do.

I donā€™t think you need a sample size. It is a direct conversion no? See pic below. The raw data from the PLC sensor right now is 1424. Then convert it from hex to decimal and it becomes 5156. Now that is the actual temp 51 degrees F outside right now. Only thing is the decimal needs to move over 2 places to make 51.56.

temphex2