[SOLVED] OpenHab3 + Zigbee2mqtt + item publish payload

Deaqr I need your help for the following issue

I have a tuya thermovalve model TS0601

in things file I have the following channel:

        Type string : auto_lock  [ stateTopic="zigbee2mqtt/Termovalvola_Test1", transformationPattern="JSONPATH:$.auto_lock", commandTopic="zigbee2mqtt/Termovalvola_Test1/set"]                                

and the following item:

String Termovalvola_Test1_auto_lock "auto_lock [%s]"      {channel="mqtt:topic:mosquitto:Termovalvola_Test1:auto_lock" } 

so I have to publish a message to change the state with the following payload

{"auto_lock" : "manual"}

when the item changes,

but the only way I found was creating a rules:

rule "Termovalvola_Test1 auto_lock"

when

        Item Termovalvola_Test1_auto_lock received command 

then

        //logInfo("FILE", "Termovalvola_Test1_auto_lock.state " + Termovalvola_Test1_auto_lock.state)

        val mqttActions = getActions("mqtt","mqtt:broker:mosquitto")

        var jsonString = "{\"auto_lock\":\"" + receivedCommand + "\"}"

        mqttActions.publishMQTT("zigbee2mqtt/Termovalvola_Test1/set",jsonString)

        logInfo("FILE",jsonString)

        logInfo("FILE", "Termovalvola_Test1_auto_lock.state " + receivedCommand)

end

there is a faster way or a REVERSE JSONPATH trasformation to simply publish the entire payload string?

thanks in advance for your help

Yes, use formatBeforePublish.


		Type string : auto_lock  [
			stateTopic="zigbee2mqtt/Termovalvola_Test1",
			transformationPattern="JSONPATH:$.auto_lock",
			commandTopic="zigbee2mqtt/Termovalvola_Test1/set",
			formatBeforePublish="{\"auto_lock\" : \"%s\"}"
		]
1 Like

What are you going to use to control the string item the main UI or sitemap?

You may want to create a switch channel and use a map transformaton.

Map would be ON=yourstring

I would be lazier and just create a Switch widget in the Sitemap with relevant mappings. Something like:

Switch item=Termovalvola_Test1_auto_lock label="[Mode %s]" mappings=[manual="Manual", auto="Auto"]

This will provide a Manual button and an Auto button which will set the Item to either “manual” or “auto”. Assuming the Item is linked to the Channel, anytime the Item is changed it will send that string via the %s in formatBeforePublish.

1 Like

Yep thats one method that I used in oh2.

I am not sure I going to continue using sitemap with OH3 you can do the same thing with icons in main UI

I will have to do some testing next year

this doesn’t work

I’m using new OH3 UI,

I changed channel as follow

        Type string : preset  [ stateTopic="zigbee2mqtt/Termovalvola_Test1", transformationPattern="JSONPATH:$.preset", commandTopic="zigbee2mqtt/Termovalvola_Test1/set", formatBeforePublish="{\"preset\" : \"%s\"}" ] 

in the yaml configuration i have the following

                          - component: oh-list-item
                        config:
                          title: Preset
                          action: options
                          actionItem: Termovalvola_Test1_preset
                          actionOptions: schedule,manual,boost,complex,comfort,eco
                          after: =items.Termovalvola_Test1_preset.state

the result is

when I change the option, the result in mqtt explorer is the following

the value doesn’t change…it seems that send only the value without brackets and “preset” string

I would contend that formatBeforePublish absolutely works, and is the answer to

Now there’s a UI problem.

Is that what your screenshot is supposed to show? I can’t see the result you describe…

Awesome your 99% there.

Can you delete the channel save it the recreate or paste it back save it again and see if that works.?

Next try a restart?

Next try clear cache?

Report back, I on mobile at work so I can’t test

yest, you are right, it works

i better check configuration option for list-item component and it’s possible to define a list of values with format value=description, so i changed the page UI yaml file as follow:

                      - component: oh-list-item
                        config:
                          title: Preset
                          action: options
                          actionItem: Termovalvola_Test1_preset
                          actionOptions: '{"preset":"schedule"}=schedule, {"preset":"boost"}=boost'
                          after: =items.Termovalvola_Test1_preset.state

note the value=description in line: actionOptions: '{“preset”:“schedule”}=schedule, …'

and please note the ’ at the begin and at the end

thanks for your help

Interesting! So what does your Thing configuration now look like, finally?

For this scope the formatBeforePublish was not necessary, so I removed it from .thing file.
The UI doesn’t change, is the same as previous picture with option list with simple description: schedule, manual, boost and so on

Maybe formatBeforePublish will be useful for switch item or for numeric item, I’m just doing some tests

1 Like

Just finished some tests

for numeric item there is not actionOptions in yaml/UI configuration so i try with publishBeforePublish in thing file and,first, it’s doesn’t work I expect, I received the wrong string AUTO instead of right string {“auto_lock”:“AUTO”}

I try changing the string in the channel configuration to

Type string : auto_lock  [
		stateTopic="zigbee2mqtt/Termovalvola_Test1",
		transformationPattern="JSONPATH:$.auto_lock",
		commandTopic="zigbee2mqtt/Termovalvola_Test1/set",
		formatBeforePublish="{\"auto_lock\" : \"" + %s + "\"}"
	] 

adding the " + %s + ", this generate an error in mqtt bridge that forced the unload, I suppose, of mqtt bridge, so in the log i saw a lot of error message stating “…changed from UNINITIALIZED to UNINITIALIZED (HANDLER_MISSING_ERROR)”

after restoring the thing file deleting the string " + %s + " the bridge reload and the formatBeforePublish starts to work correctly.

so I made a second test without force the error but simply stopping the mqtt from things UI page but this didn’t give the expected result, the formatBeforePublish didn’t work.

I generate again the error and restore again the thing file and it works…
So I ask, what do I have to stop/restart for make it work correctly without generate error in thing file?

Ah, this is an absolute classic: openHAB has a bug, which it has had for a long time, where it doesn’t always properly load a Things file after it has been saved.

Restart your openHAB for a quick fix, or do this. To be honest, deliberately generating the error in the Things file is the fastest and easiest method as it forces openHAB to unload all the Things in that file. Repairing the Things file then forces openHAB to load all the Things back up.

1 Like

You can also use a simple switch to get you auto manual to work.

Try using the main UI to create a generic thing its easy and has a simple form to fill in. Once you get it setup you can add channels in the code section.

So for your example with outgoing format using auto manual in the code tab enter

channels:
  - id: Auto_Lock
    channelTypeUID: mqtt:switch
    label: "BLAH BLAH "
    description: ""
    configuration:
      postCommand: true
      formatBeforePublish: '{"auto_lock":"%s"}'
      commandTopic: zigbee2mqtt/Termovalvola_Test1/set
      stateTopic: zigbee2mqtt/Termovalvola_Test1
      transformationPattern: JSONPATH:$.auto_lock
      off: manual
      on: auto