openB
(Bernd)
January 7, 2025, 6:16pm
1
Hi all
I have the following problem: I want to publish three numeric values from three different items combined into an array. How can I achieve that?
Edit: And my second question would be: if I have an item in kWh and have to publish it in Wh (so basically times 1000), how can I do that? Channel outgoing format = 1000*%f or with an outgoing value transformation?
Thanks for your help,
Bernd
rlkoshak
(Rich Koshak)
January 7, 2025, 7:29pm
2
A rule using the MQTT publish Action. https://www.openhab.org/addons/bindings/mqtt.generic/#rule-actions
If not using Rules DSL, see the README for the add-on you are using to access the action. Blockly has a Block Library on the marketplace.
Do you still need the units or just the value? I.e. do you publish “1000 Wh” or just “1000” and is the Item’s state “1 kWh” or just “1”?
Is the Item a Number:Energy
or just a Number
?
Is this another Item or is this one of the three values you talk about in the first question?
openB
(Bernd)
January 7, 2025, 7:34pm
3
Regarding my 2nd question I tried the following:
Add a file multiplyBy1000.js in /etc/openhab/transform:
(function(value) {
return value * 1000;
})(parseFloat(input));
with owner/group openhab, -rw-r–r-- permissions
Then I configured the MQTT channel like so:
- id: gPlugM_Zaehler_Bezug
channelTypeUID: mqtt:number
label: gPlugM Zähler Bezug
description: ""
configuration:
formatBeforePublish: "%s"
transformationPatternOut:
- JS:multiplyBy1000.js
commandTopic: openWB/set/counter/14/get/imported
but it doesn’t work, no values are published at all. What’s wrong with this approach?
openB
(Bernd)
January 7, 2025, 7:36pm
4
It’s another item, as Number:Energy, and I need only the number without any unit.
openB
(Bernd)
January 7, 2025, 7:37pm
5
OK, so a rule is needed, will look into that - thanks a lot.
rlkoshak
(Rich Koshak)
January 7, 2025, 7:41pm
6
In that case probably the easiest would be to use a JS script transformation on the outgoing transformation. It’s simple enough to do it inline:
JS: | Quantity(input).toUnit("Wh").float
See JavaScript Scripting - Automation | openHAB and JavaScript Scripting - Automation | openHAB for more details.
openB
(Bernd)
January 7, 2025, 8:00pm
7
Hmm, doesn’t work (so far) - I’m going to investigate further tomorrow… what was wrong with my js file approach?
I’ve set both Unit (kWh) and State Description (%.3f kWh), Profile is Follow - is that a problem?
rlkoshak
(Rich Koshak)
January 7, 2025, 8:04pm
8
Maybe there’s something relevant in the logs?
openB
(Bernd)
January 8, 2025, 5:37pm
9
OK, this works:
JS(| parseFloat(input) * 1000)
Log showed something like Cannot convert 3500 to Wh (missing unit).