Celsius to Fahrenheit Rule Transformation?

Problem: I’m collecting & storing the CPU temperature of my openhab instance (Raspberry Pi) in an InfluxDB, but it is in Celcius - I want Fahrenheit. I was able to use a celcius-fahrenheit transform js script to change this in the item file, but this isn’t quite cutting it since that only changes the display for use in a sitemap or whatever.

I’m looking to convert this value in the very beginning if possible, so it gets stored as Fahrenheit, displayed, etc. Would a rule be my answer? I saw this: https://www.openhab.org/docs/configuration/transformations.html & this: https://www.openhab.org/docs/configuration/rules-dsl.html#transformations showing this:

transform("<transformation-identifier>", "<transf. expression or transf. file name>", <input-data or variable>)

but I’m not sure what a full example is - or if this is even what I’m looking for. If I did do it via a Rule, it doesn’t quite make sense to me because it would be something like “If item updated, then do conversion, then post update”…but then I’d have 2 values in openhab & the influxdb? (C & F?)

I’m also looking to do this in other use-cases so would like to figure this out. Any advice? Thanks!

No need for a transformation just use the built in units of measurement.

The missing info is “where does your data come from”?
Presumably via a binding.

Some bindings support Units of Measurement types - attaching units to numeric values, which eases conversions.
Some bindings don’t.
UoM is not a cure all - openHAB persistence does not really understand units, so there is potential for confusion writing/reading different units.

Some bindings support applying a transformation between incoming data and Item. This is binding specific.
Some bindings don’t.

Profiles allow applying a transformation to a channel, modifying data passing between binding and Item.
Channels only exist in v2 bindings, the transform profile does not currently work with Number Items.

You can always do this kind of thing in rules, where a rule listens for changes to Item X, processes the X state, and updates Item Y with a new value.

2 Likes

This particular use-case I’m getting the data from the system info binding.

Any examples anyone can post links to? I wasn’t able to find any, but want exactly sure what to search for either. Either via the profile or rules method. And if I did this via the rules method would I have both celcius & Fahrenheit measurements in influxdb & openhab? (Or does this require an additional item? Ie when sensor updates, convert it and update another item like cpu_temp_converted_to_f? Or can I do this without an additional item?)

Thanks all!

The documentation example does not show that as a Temperature number.
CPU Temperature specifications are always expressed in Celsius anyway.

Systeminfo binding does not support transformations directly, so that is one option gone.

Reminder that transform profile cannot currently be used with a Number (or Number:Temperature) Item, only String, so that may or may not be ruled out depending on your needs.

You might consider why you care about the internal value, so long as you can get it in the units you want at the time of use/display.
There might be good reasons, like charting.

1 Like

Only if you configure both Items to be persisted. Based on the information presented, to summarize your options:

  1. If you don’t need to do math or chart the value (which seems unlikely) use the transform Profile with a JavaScript transformation to convert the value from C to F before it gets to the Item. But as indicated, the transformation profile only works with String Items.

  2. Write a rule that triggers when the C Item receives an updates, calculate the F value and update the F Item. If all you care about is F in your database, only configure the F Item to be persisted.

Yes, will be using grafana charts.

Sounds like #2 is the only way to do this then given my circumstances, but sounds like it should work. I’ll report my solution for future readers when I get a chance to do this. Thanks!