MQTT value string to integer casting

Hi,

I am sending an MQTT message from a different python machine to my Raspberry Pi 4 running home assistant.
However, I only receive strings, and I am unable to convert this string to a value. Therefore the question: How can I convert this MQTT message string to a value.

The python publish command I am using is the following:

client.publish(topic="homeassistant/sensor/tagmachine/tagnr", payload=str(iterator), qos=0, retain=False)

on the home assistant side, the yaml configuration is the following:

mqtt:
  sensor:
    - name: "Machine current Tag nr"
      state_topic: "homeassistant/sensor/tagmachine/tagnr"
      value_template: "{{ value }}"

I also tried "{{ value | int }}" and {{ int(value) }} but to no success. Does anybody know how to correctly cast this value to a number?

Thanks in advance

As a reference, here is the complete python code:

#!/usr/bin/python3
#
import time
import paho.mqtt.client as mqtt

broker = 'xxx'
port = 1883
mqtt_username = "xxx"
mqtt_password = "xxx"
delay = 1


# Send messages in a loop
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "mqtt_test")
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(broker, port)
client.loop_start()


for iterator in range(1, 10, 1):
    client.publish(topic="homeassistant/sensor/tagmachine/state", payload="Running", qos=0, retain=False)
    client.publish(topic="homeassistant/sensor/tagmachine/tagnr", payload=str(iterator), qos=0, retain=False)
    print("Loop executed sleeping")
    time.sleep(delay)


client.publish(topic="homeassistant/sensor/tagmachine/state", payload="Stopped", qos=0, retain=False)

time.sleep(delay)

hmm, what integer is ‘Stopped’ ?

Ah, absolutely right, I copied the wrong line of code in my question above (but my appended python code was correct). Sorry about that. I updated the question to make it correct.

I of course push out the iterator as a string on the python side and while I receive that string correctly I fail to convert it to an integer on the home assistant side.

Sensor states are always strings (docs)

If you want to perform operations on a sensor state that represents a number, you need to convert it at that point in time. For example, if you wanted to multiply your sensor by ten:

{{ states('sensor.machine_current_tag_nr')|int(0) * 10 }}

Yeah, this is what I also do with other variables. However in the MQTT documentation, the casting is done as follows:

"value_template": "{{ value|float }}",

which led me to believe that I should proceed in a similar manner.

Why can’t I use the value variable in a similar way?

That example is for publishing to mqtt.

Good point, I did not see that.

Therefore casting has to be done as troon suggested? Because does that not mean that the state is accessing itself?

What is the actual problem you’re trying to solve? Your MQTT sensor state will be a string just like all your other sensors.

I am trying to convert that string which I receive over mqtt to a number format such that I can plot them in a useful way instead of colored bars. Therefore the Machine current Tag nr variable shall be a number variable. And I am trying to do this “in one go” such that the variable Machine current Tag nr directly comes out as a number variable.

Does that make sense? Sorry if I have been unclear.

Then you need to add a unit_of_measurement to your sensor. It can be anything you like:

mqtt:
  sensor:
    - name: "Machine current Tag nr"
      state_topic: "homeassistant/sensor/tagmachine/tagnr"
      unit_of_measurement: "machine"     

XY problem - Wikipedia.

1 Like

supercool, that solved the problem!

So just for clarification, does HA try to parse that value automatically when the graph is being generated, or is the value parsed as a number directly when HA receives the value?

I have to apologize for the confusion, it definitly was an XY problem!

Entity states are always strings. If you give a sensor a unit_of_measurement, you are indicating that you expect the state to always be a string that “looks like” a number, and the front end will be able to draw graphs based on those “numbers”.

If your MQTT source ever publishes a non-numeric state, an error will be generated.