[SOLVED] formatBeforePublish JS not executed and passed as String

Hi after some mistakes i finally made it to send data from my channel but it still fails.

Bridge  mqtt:broker:mosquitto "Mosquitto" [ host="192.168.178.63", secure=false, clientID="openHAB2Docker"  ]
{
    Thing topic WoziThermosLeft "Thermostat Wohnzimmer Links" @ "Wohnzimmer"
    {
        Channels:
            Type number : WoZiLeSetpoint [stateTopic="zigbee2mqtt/WoZiLinks", commandTopic="zigbee2mqtt/WoZiLinks/set", transformationPattern="JSONPATH:$.current_heating_setpoint", formatBeforePublish="JS:setTempThermostat.js"]
    } 
}   

the JS file in in transform folder i tried it with and without typecast to string

(function(x) {
    var result;
    result = "{ \"current_heating_setpoint\": " + String(input) + " }";
    return result;
})(input)

If I change the Setpoint in the eventlog it logs fine the select value is set and shown in the gui but as i checked the thermostat the value was not setted.

2019-11-03 21:22:06.639 [ome.event.ItemCommandEvent] - Item 'ThermosLi_Setpoint' received command 24.0
2019-11-03 21:22:06.762 [nt.ItemStatePredictedEvent] - ThermosLi_Setpoint predicted to become 24.0
2019-11-03 21:22:06.763 [vent.ItemStateChangedEvent] - ThermosLi_Setpoint changed from NULL to 24.0

If i check zigbee2mqtt log I found:

11/3/2019, 9:22:06 PM - error: No converter available for 'state' (JS:setTempThermostat.js)

So it seems the js script was not executed and just passed as string :frowning:
JavaScriptTransformation is installed.

Try that:

(function(x) {
    var obj = { current_heating_setpoint: x }; 
    var result = JSON.stringify(obj);
    return result;
})(input)

The log message stays the same :frowning: the js File is placed right? I changed the function and make a fresh restart.

11/4/2019, 6:35:20 AM - error: No converter available for 'state' (JS:setTempThermostat.js)

Check zigbee2mqtt help forum?

I can do but zigbee2mqtt works fine :thinking: the problem is that the js function seems not be called and only the string is sended to mqtt :frowning:
If send the json

{
	"current_heating_setpoint" : 24
}


zigbee2mqtt log say this and the thermostat change the temperature.

Zigbee publish to device 'WoZiLinks', hvacThermostat - write - [{"attrId":16387,"dataType":41,"attrData":1600}] - {"manufSpec":1,"manufCode":4151} - 1
11/4/2019, 7:17:54 PM - info: MQTT publish: topic 'zigbee2mqtt/WoZiLinks', payload '{"local_temperature":18.31,"occupied_heating_setpoint":21,"unoccupied_heating_setpoint":16,"pi_heating_demand":2,"eurotronic_system_mode":1,"current_heating_setpoint":16,"eurotronic_error_status":0,"battery":100,"linkquality":55}'
11/4/2019, 7:17:59 PM - info: Zigbee publish to device 'WoZiLinks', hvacThermostat - write - [{"attrId":16387,"dataType":41,"attrData":2400}] - {"manufSpec":1,"manufCode":4151} - 1
11/4/2019, 7:18:01 PM - info: MQTT publish: topic 'zigbee2mqtt/WoZiLinks', payload '{"local_temperature":18.31,"occupied_heating_setpoint":21,"unoccupied_heating_setpoint":16,"pi_heating_demand":2,"eurotronic_system_mode":1,"current_heating_setpoint":24,"eurotronic_error_status":0,"battery":100,"linkquality":55}'
11/4/2019, 7:18:05 PM - info: Zigbee publish to device 'WoZiRehts', hvacThermostat - write - [{"attrId":16387,"dataType":41,"attrData":2400}] - {"manufSpec":1,"manufCode":4151} - 1

Then I would be worth posting an issue on Github.
Alternatively, I would use node-red for this

format is not transform.
String formatter expects format directives like
"%.2f" or
"fred %s"

I think what you need is something like
formatBeforePublish="{ \"current_heating_setpoint\":\"%s \" }"

Or for MQTT binding versions 2.5 you can use transform
transformationPatternOut="JS:setTempThermostat.js"

Hello Community,

i almost got the same problem. I use the 2.4 binding and need to send also the “current_heating_setpoint: xy” If i understand it, i need to configurate a converter. But this is not the same like transform? So I can not use the script from above?

First tried { “current_heating_setpoint”:"%.1f " }" now, but didnt work :frowning: Just send the information like: “30” but not in JSON.

Incoming works fine.

You need to update to 2.5 and use the outgoing transformation

ok thank you. First have to update openhab to 2.5 :slight_smile:

From your screenshot, you ended with a quote mark but did not begin with a quote mark. I do not know if it needs them, but they should be used in pairs.

thanks, but tried both versions. Just forget the first " by copy paste :slight_smile:

ok i got the option now. Can you pls tell me, what i have to set in the outgoing transformation? Do i have to write a script (or use the one above)?

i guess you also need the converter. see https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html -> 3. Adding converter(s).

" In order to parse the messages of your zigbee device we need to add converter(s) to node_modules/zigbee-shepherd-converters/converters/fromZigbee.js ."

and there ist also an “toZigbee.js” file

No converter was required at the zigbee2mqtt end. The erroneous use of formatBeforePublish= in the openHAB Thing resulted in the literal string "JS:setTempThermostat.js" being sent to it. It’s in the thread title.