How do I use String:format in Javascript rules

To share the whole rule click on the “Code” tab in the upper right corner. That will show everything, triggers, conditions, and all the actions.

Personally, I prefer to use the items dict to get Item’s states rather than using the itemRegistry. It makes for shorter and easier to understand lines of code.

var str1 = items['OpenWeatherMapOneCallAPIweatherandforecast_ForecastTomorrow_Condition'].toString();

I believe that OWM uses Units of Measurement for most of its Channels. This means the value carries units and you can compare and do math with compatible units. For example, compare one value in °C and another in °F.

if(items['MyTemp'] > new QuantityType("70 °F")){

The above will work whether MyTemp is storing a value in °C or °F. That is largely why you had to call floatValue on the states of those Items because 22.509 °C isn’t a number that String.format understands how to format. But note that the value stored in that Item is not guaranteed to be °C. You are assuming that it will be and it’s not a terrible assumption, but it’s not guaranteed.

1 Like