I am struggling to read the last part of a MQTT topic and want to put this number (#) into a sensor.
The last part of the topic (#) is a zone number. This number can be 1 - 8.
As example the topic can be: opensprinkler/station/7
With the following: "{{ (("opensprinkler/station/#").split("/")[2]).split("#") [-1] }}" I succeed to get the number out of the total topic.
The problem is that the data is in a string format and I have no idea how to convert it to a number.
This is the sensor I have created to get the zone number. The result is empty.
Does anybody has an idea how to solve this?
If I see correctly, the result in your sensor includes quotes, which most likely come from your template definition. Try without the outer quotes in value_template, like this:
value_template: >
{{ ((("opensprinkler/station/#").split("/")[2]).split("#") [-1]) | int (default=0) }}
All states are strings. There’s nothing you can do about that. You can have it treated as a number (for graphs and such) by adding any unit_of_measurement. Or you can put it in an attribute. Attributes support other types than string.