Read MQTT string and put result in a sensor

Hi,

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?

mqtt:
   sensor: 
     - name: "Irrigationzone"
       state_topic: "opensprinkler/station/#"
       value_template: >
        "{{ (("opensprinkler/station/#").split("/")[2]).split("#") [-1] }}"

Did you try adding int at the end, like this:

value_template: >
        "{{ ((("opensprinkler/station/#").split("/")[2]).split("#") [-1]) | int (default=0) }}"

Beware, if there is no value or value unknown, it will be defaulted to 0 (but you can define another default, if you like).

This is the result when I add int (default=0) :

If I select another zone, the result is always "0"

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.