I think it is not working because messagio is an object and you only need the state formatted as a string. It should work with the following code:
if (messaggio.state.toString.length() > 160) {
postUpdate(messagio, messaggio.state.toString.substring(0, 160))
}
Greetz
Michael
P.S.: Not sure if you need the brackets after length
Edit: sorry, didn’t read your last post. But here with me substring is working in OH2 correctly, Do you have the variable definition in the same rule? I always got problems when the variables are defined outside of the rule.