MQTT - State_topic issue or invalid formula

Hello,

I have been learning to use MQTT on HAOS after using Homeassistant in container mode. I had RTL_433 working fine by and using MQTT to obtain information, as well as adding in sensor in configuration file. Now I am trying to switch to HAOS, and so learning with using ‘add-ons’. I have now add RTL_433 and Mosquito addon in this manner, and configured it. Whilst I have managed to get three of 5 of my values from Mosquito, the two remaining are not playing ball, plus I get error messages, which I can’t think why, plus I have removed the offending string i.e. INT and STR. It still shows up in the log, and so formula that was working using value_json under container mode previously now doesn’t, but found I had to change it to ‘value’ instead and this gave me further result, but not for the other two which I want as well.

So here are the error messages that are showing in the homeassistant log:-

2022-06-08 15:55:51 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on ‘rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm’: ‘95’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 47, in wrapper
msg_callback(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 300, in message_received
_update_state(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/sensor.py”, line 265, in _update_state
payload = self._template(msg.payload, default=self._state)
File “/usr/src/homeassistant/homeassistant/components/mqtt/models.py”, line 159, in async_render_with_possible_json_value
return self._value_template.async_render_with_possible_json_value(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 572, in async_render_with_possible_json_value
return _render_with_context(
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 1842, in _render_with_context
return template.render(**kwargs)
File “/usr/local/lib/python3.9/site-packages/jinja2/environment.py”, line 1301, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.9/site-packages/jinja2/environment.py”, line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File “”, line 1, in top-level template code
TypeError: unsupported operand type(s) for -: ‘int’ and ‘str’

Here are the extract of the script from configuration.yaml:-

mqtt:
sensor:

Obtain number of Litres left in Tank

- name: "Bar Oil Tank Litres left"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ (123 - (value)) * 10.65 }}"
  unit_of_measurement: 'Litres'

Obtain BAR level same as that on Oil Watchman

- name: "Bar Oil Tank"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: >
   {% set actual_depth = 123 - value | int %}
   {{ actual_depth  | int // 13 + (actual_depth  | int % 13 != 0) | int }}
  unit_of_measurement: 'bar'

Obtain % left of Oil in Tank

- name: "Bar Oil Percent"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ ((123 - (value)) * 10.65) / 1300 * 100 }}"
  unit_of_measurement: '%'

Obtain Depth of Oil left

- name: "Bar Oil Depth Left"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ 123 - (value) | int }}"
  unit_of_measurement: 'cm'

Apologies I am not quite advanced on scripting as I am calling same state_topic four times, basically ‘depth_cm’ are my main number to work with, this is read as 95 or 96, which is correct, but I can’t get ‘Bar Oil Percent’ and ‘Bar Oil Tank Litres Left’ but the other two are working OK, even though it has INT in it! Everytime time I get an update (approx every 30mins) I get this errors in the log. So is it the formula or is it MQTT that are causing issue? I am on Home Assistant Core 2022.6.1 (hence the different MQTT sensor layout as advise). I have also check configuration is valid and restart it, I have also done a full shutdown. The error in the log still persists.

MQTT Explorer shows up the follow values:-
rtl_433

Any help would be appricated to ‘fix’ this!

missing int on a subtraction. The hint is in the error:

YOu’re trying to subtract (minus sign) - a string to a integer.

value is always a string… so…

123 - value…

  value_template: "{{ (123 - (value | int)) * 10.65 }}"
  value_template: "{{ ((123 - value | int) * 10.65) / 1300 * 100 }}"
- name: "Bar Oil Tank Litres left"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ (123 - (value)) * 10.65 }}"
  unit_of_measurement: 'Litres'

- name: "Bar Oil Tank"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: >
   {% set actual_depth = 123 - value | int %}
   {{ actual_depth // 13 + (actual_depth % 13 != 0) | int }}
  unit_of_measurement: 'bar'

- name: "Bar Oil Percent"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ ((123 - value | int) * 10.65) / 1300 * 100 }}"
  unit_of_measurement: '%'

- name: "Bar Oil Depth Left"
  state_topic: "rtl_433/74930c0d-rtl433/devices/Oil-SonicSmart/684266245/depth_cm"
  value_template: "{{ 123 - value | int }}"
  unit_of_measurement: 'cm'

Petro

Thank you! I hadn’t realised that what was happening, so it was trying to manipulate a string when it should be converted to integer!

It’s now fixed my problem, thank you.