Rules + MQTT Binding + sending the state of another item as command

Hi there,

I’m on OpenHAB 4.0.3 and I’m failing to achieve the following:

I’m using a awtrix light pixel clock and now I want to show some information of my OpenHAB items within it. I successfully created the MQTT item to send information successfully, but now I want so send the state of another item as the command.

This is working as a rule:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: LocalWeatherandForecast_Aussentemperatur
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: "{ 'icon':73, 'text':'28°C' }"
      itemName: PixelClocks_weather_livingroom
    type: core.ItemCommandAction

as the temperature is “hardcoded” for my mqtt tests. But if I try to send the value of the actual item it is also only send as text. Here is the code:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: LocalWeatherandForecast_Aussentemperatur
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      command: "{ 'icon':73, 'text':(LocalWeatherandForecast_Aussentemperatur.state) }"
      itemName: PixelClocks_weather_livingroom
    type: core.ItemCommandAction

Can anyone help me with this? I don’t get any error in the openhab log…

You’ll need to use a scripting language for this I think.

For example you can use RulesDSL, although I’d suggest jruby or javascript but its corresponding addon needs to be installed first.

val output = "{ 'icon':73, 'text':'" + LocalWeatherandForecast_Aussentemperatur.state + "' }"
PixelClocks_weather_livingroom.sendCommand(output)

Jruby:

output = "{ 'icon':73, 'text':'#{LocalWeatherandForecast_Aussentemperatur.state}' }"
PixelClocks_weather_livingroom.command(output)

So the whole thing looks like:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: LocalWeatherandForecast_Aussentemperatur
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        val output = "{ 'icon':73, 'text':'" + LocalWeatherandForecast_Aussentemperatur.state + "' }"
        PixelClocks_weather_livingroom.sendCommand(output)
    type: script.ScriptAction
1 Like

Thank you so much! You made my day!!!

A better option / solution would be to redefine your MQTT item. Can you paste me the item definition for PixelClocks_weather_livingroom ?

If I would know how to paste it… :joy:

I defined a MQTT Thing with a channel, which I linked to the item PixelClocks_weather_livingroom…

But the code is only:

label: weather
type: String
category: display
groupNames: []
groupType: None
function: null
tags:
  - Point

I guess, that’s NOT what you are asking for…

I just realised, what I want is the Thing / channel definition, not the item definition.

Something like this (just to help you find the right one). It needs to have UID: mqtt:topic:xx:xxxx

UID: mqtt:topic:mosquitto:aaa
label: Generic MQTT Thing
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:mosquitto
channels:
  - id: textdata
    channelTypeUID: mqtt:string
    label: label
    description: ""
    configuration:
      stateTopic: device/state
      commandTopic: device/command

Here it is (please note that I added already more as your initial post works):

UID: mqtt:topic:1671786fee
label: PixelClocks
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:cce5679735
channels:
  - id: pixelclockLivingRoomWeather
    channelTypeUID: mqtt:string
    label: weather
    description: Shows Temperature delivered by OpenWeather on PixelClock
    configuration:
      commandTopic: awtrix_livingroom/custom/weather
  - id: pixelclockLivingroomPower
    channelTypeUID: mqtt:string
    label: livingroomPower
    description: Power usage of house displayed on Living room Pixelclock
    configuration:
      commandTopic: awtrix_livingroom/custom/power
  - id: livingRoom_TempHum
    channelTypeUID: mqtt:string
    label: livingroomTempHum
    description: Information about the living room temperature and humidity on
      livingroom pixel clock
    configuration:
      commandTopic: awtrix_livingroom/custom/room

OK so you can basically add this to your channel:

      formatBeforePublish: "{ 'icon':73, 'text':'%s' }"

And then the rule can be made simpler, like this:

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: LocalWeatherandForecast_Aussentemperatur
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "3"
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |-
        PixelClocks_weather_livingroom.sendCommand(LocalWeatherandForecast_Aussentemperatur.state.toString)
    type: script.ScriptAction

Just another option to consider.

Wow, thank you very much!!!