Updated Home assistant and now math using int not working

I just updated my home assistant and now it appears to not like my Modbus math templates. The file check works but in HA the test fails showing it doesn’t like my data type. I did try adding int32 instead of int to everything and it did work but the result was in the thousands or millions. What do I need to do to correct this issue?

poolt

modbus: 
  - name: hub1
    type: tcp
    host: 192.168.1.24    
    port: 502

    switches:
      - name: Pool Pump
        slave: 255
        address: 3199
        write_type: coil
        verify:
        
      - name: Yard Light
        slave: 255
        address: 3294
        write_type: coil
        verify:
    
      - name: Deck Lights
        slave: 255
        address: 3296
        write_type: coil
        verify:

    binary_sensors:
      - name: "Pump State"
        address: 2052
        scan_interval: 10
        slave: 255
        device_class: moving
        input_type: coil   
       
    sensors:
      - name: Pool Temphex
        address: 1024
        scan_interval: 10
        slave: 255       
        data_type: int # int or float
        unit_of_measurement: "°F"
        
      - name: outside Temphex
        address: 1026
        scan_interval: 10
        slave: 255       
        data_type: int # int or float
        unit_of_measurement: "°F"        
        
        
template:
  - sensor:
      - name: "Pool Temp"
        unit_of_measurement: "°F"
        state: '{{ ("%0x" % (states("sensor.pool_temphex") | int)) | int / 10 }}'

      - name: "Outside Temp"
        unit_of_measurement: "°F"
        state: '{{ ("%0x" % (states("sensor.outside_temphex") | int)) | int / 10 }}'

I don’t know how those ever worked. If that sensor output is hex, then the int conversion has been incorrect from the beginning. Can you post what the values of the input sensors?

1 Like

They were values from a PLC. I’m not sure I think the value was say for 32.53 degrees would originally read 3253. Someone helped me come up with the template to get 32.53 degrees

You have the sensor values in front of you, how are you not sure? Post the values that you see for the hex sensors

Is this what you are looking for? Please excuse my lack of knowledge I am still learning this stuff.

Yes, those are the values. Also, it seems your templates are working as expected and the values coming from modbus are not correct.

what should the outside temperature be for 88539136? I don’t think you want it to be 542000ºF

1 Like

I believe it is supposed to be 54.2 degrees F This was the issue before. But It did work until the last update.

Ok, but what doesn’t make sense is that you’re using ‘%0x’ as a format converter. That takes base 10 integers and converts them to base 16. For example, if your sensor.*temphex output 11, the value of your template would be b, which is not a number. So the template never worked. You were lead down the wrong path initially. So, you need to figure out what the temperature should be so we can make a correct equation.

It was to move the decimal places from what I remember. The output from the PLC to HA was 542200 or something so I assume the math was to move the decimal over to make 54.22.

Moving the decimal place is a simple division of 1000. 52000/1000 = 52. But again, your template is using `’%0x’ which changes base 10 integers to base 16.

Let me explain because it seems you still don’t follow.

Your hex sensor is: sensor.outside_temphex

Your template sensor is: sensor.outside_temp

The template for sensor.outside_temp is giving you 542000 from 88539136. That template is using %0x, which converts base 10 numbers to base 16. So if your sensor.outside_temphex were to read 11, your sensor.outside_temp would read b. is b a valid temperature? No.

So, your template is incorrect. Stop looking at sensor.outside_temp, we now need to figure out how to take the value from sensor.outside_temphex and get the correct temperature in ºF. Only you know this information.

There’s a number of ways to figure this out. Does your device output a temperature on screen (not in home assistant, on the actual device)? If yes, what is the value of sensor.outside_temphex and what is the value on screen?

Ok. I am starting to see what your saying. It has been a long while. I am not able to get to the controller now. But From what I remember it was taking an int from the controller and by the time HA received the data over LAN it was in HEX. when it was received in HEX it was then converted to an decimal again. When that happened it was 54200. Then some dude gave me a template to convert it all. All this info can be found in our discussion. This might help get caught up with what happened.

Alirght, but notice how all those posts have int(base=16) and notice how yours doesn’t? It seems you were focusing on the wrong part of the template when you were experimenting and came up with your solution.

I suggest we start over as it seems like you just tried some things and got a close answer without knowing if the answer was correct.

Ok.
This might be where it came from just found the other guy that was helping me.

Got it.
All I did was add 16 to the int on each sensor

t1

today

    sensors:
      - name: Pool Temphex
        address: 1024
        scan_interval: 10
        slave: 255       
        data_type: int16 # int or float
        unit_of_measurement: "°F"
        
      - name: outside Temphex
        address: 1026
        scan_interval: 10
        slave: 255       
        data_type: int16 # int or float
        unit_of_measurement: "°F"        
        
        
template:
  - sensor:
      - name: "Pool Temp"
        unit_of_measurement: "°F"
        state: '{{ ("%0x" % (states("sensor.pool_temphex") | int)) | int / 10 }}'
  

      - name: "Outside Temp"
        unit_of_measurement: "°F"
        state: '{{ ("%0x" % (states("sensor.outside_temphex") | int)) | int / 10 }}'