[SOLVED] Can't Convert Celsius to Fahrenheit in template widget

Thats what i was thinking too. I haven’t messed with the rules engine at all yet. So I guess it’s time to look into that.

Create a proxy item to use the converted temp. in the example rule below I used Basement_HT_Temperature1 as the proxy item.

rule "temp"
when
Item Basement_HT_Temperature received update
then 
        Basement_HT_Temperature1.postUpdate((Temperature.state as DecimalType+32*1.8).toString)
end

so i could just copy and paste this into a rules files and try to run that? I don’t have any rules, or a rules file right now because i don’t have any rules hahaha

It’s recommended to type it but you can copy and paste. I haven’t verified the rule works so there may need to be an adjustment, just run the run and post the log and we’ll go from there.

2018-12-13 10:34:23.832 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘home.rules’, using it anyway:
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
2018-12-13 10:34:23.834 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘home.rules’

So it looks like it loaded the rules file, but no change yet, Should i restart the openhab service?

Another option is use JS transformation on the item.

Number Basement_HT_Temperature "[JS(tempconverter.js):%.1f °F]" { channel=“mihome:sensor_ht:158d00023d295f:temperature” }

Then create a tempconverter.js file with the code to convert C to F.

No need to restart.

The problem is the * in the rule.

rule "temp"
when
Item Basement_HT_Temperature received update
then 
        var current =  Basement_HT_Temperature.state as Number
        var Number fahrenheit = (current * 1.8) + 32
        Basement_HT_Temperature1.postUpdate(fahrenheit)

end

Have you tried setting your system in Paper UI (Configuration>System>Regional Settings) to explicitly English as the language and United States as the region? If so according to https://www.openhab.org/docs/concepts/units-of-measurement.html#quantitytype it should use the Imperial system as default and maybe your items will start reporting temperatures in Fahrenheit without you having to do anything.

Yup I read that article too and tried it. No go.

I edited the rule above, this should work.

rule "temp"
when
Item Basement_HT_Temperature received update
then 
        var celsius = Basement_HT_Temperature.state as Number
        var Number fahrenheit = (celsius * 1.8) + 32
        Basement_HT_Temperature1.postUpdate(fahrenheit)

end

ok ill try that and let you know.

Still shows 21.3F. Should i take this line out of the template now?

{{’%.1f’ | sprintf:itemValue(‘Basement_HT_Temperature’).split(’ ‘)[0]}}{{itemValue(‘Basement_HT_Temperature’).split(’ ')[1]}}

You need to create another (don’t delete the original) item (proxy item) to use as the temp sent to the widget.

Create this item

Number Basement_HT_Temperature1 "[%.1f °F]"

Change widget item name to:

{{’%.1f’ | sprintf:itemValue(‘Basement_HT_Temperature1’).split(’ ‘)[0]}}{{itemValue(‘Basement_HT_Temperature1’).split(’ ')[1]}}

That didn’t work either. I’m thinking it might just be easier to do the javascript transform at this point

Do you see the new item showing the temp converted from C to F in the logs?

In your case it should show 70.5 if the temp is still 21.4. Also note, the rule will run when the temp changes. You may need to wait or heat the sensor with something.

I’m not sure if this will work in OH as I’m very new to JS but here’s somehting from online.

function convert(degree) {
  var x;
  if (degree == "C") {
    x = document.getElementById("c").value * 9 / 5 + 32;
    document.getElementById("f").value = Math.round(x);
  } else {
    x = (document.getElementById("f").value -32) * 5 / 9;
    document.getElementById("c").value = Math.round(x);
  }
}

Use the UOM to your advantage
First item in C
Second item in F
Item1.postUpdate(item2.state)

Sorry on the phone

As @vzorglub mentioned, use these items and this rule.

First item that gets the reading from the sensor.

Number:Temperature Basement_HT_Temperature "[%.1f °C]" { channel=“mihome:sensor_ht:158d00023d295f:temperature” }

Second item used for widget.

Number:Temperature Basement_HT_Temperature1 "[%.1f °F]"

Rule to update the second item to °F via UOM feature.

rule "temp"
when
        Item Basement_HT_Temperature received update
then 
        Basement_HT_Temperature.postUpdate(Basement_HT_Temperature1.state)

end

Ok i changed all that. Now in the custom widget should i still have this line in there?

{{’%.1f’ | sprintf:itemValue(‘Basement_HT_Temperature1’).split(’ ‘)[0]}}{{itemValue(‘Basement_HT_Temperature1’).split(’ ')[1]}}

You changed the rule to use the UOM feature or you changed to use the first rule?

Yes, I would think that the above line is still needed. Test it and let’s find out.:crossed_fingers:

I now the following 2 items

Number:Temperature Basement_HT_Temperature “[%.1f °C]” { channel=“mihome:sensor_ht:158d00023d295f:temperature” }

Number:Temperature Basement_HT_Temperature1 “[%.1f °F]”

Then I changed the rule to the following

rule “temp”
when
Item Basement_HT_Temperature received update
then
Basement_HT_Temperature.postUpdate(Basement_HT_Temperature1.state)

end