Send JSON command with MQTT?

I installed WallPanel on my Tablet and would like to wake the display after hue sensor detected motion with {"wake": true} as described here.

However, I have no clue how to do this in HA. This is what I tried (I created a switch for the sake of faster testing):

switch:
  - platform: mqtt
    name: "Bathroom Tablet"
    command_topic: "/wallpanel/mywallpanel/command"
    payload_on: '{"wake": "true"}'
    payload_off: "OFF"

It doesn’t work, I don’t even know where to start looking for mistakes. btw, I set up the hue motion sensor with mqtt and it works fine.

1 Like

try no quotes around the true

also double check your command_topic - especially check if it has a leading / (at the start of the topic)

Create an automation using the motion sensor as trigger and use this as action:

service: mqtt.publish
      data_template:
        topic: 'wallpanel/entrada/command'
        payload_template: '{"wake":true}'

this is what I have now, but still no success, unfortunately:

- action:
  - service: mqtt.publish
  data_template:
    topic: 'wallpanel/mywallpanel/command'
    payload_template: '{"wake":true}'
  trigger:
  - entity_id: binary_sensor.hue_sensor_motion
    from: 'off'
    platform: state
    to: 'on'

Does it give any error on the log? Your spacing is wrong and that might be the problem

hm, I changed it to this:

- action:
  - service: mqtt.publish
    data_template:
      topic: 'wallpanel/mywallpanel/command'
      payload_template: '{"wake":true}'
  trigger:
  - entity_id: binary_sensor.hue_sensor_motion
    from: 'off'
    platform: state
    to: 'on'

No errors with this code (but there was an error with the previous config).

Still no reaction from the tablet.

Edit:

when I run
mosquitto_pub -h 192.168.0.26 -p 1883 -t "wallpanel/mywallpanel/command" -m '{"wake": true}'

i get this error: Error: No route to host

Your automation has a wrong structure. See some examples here:

The right structure for the automation is:

- alias: 'Tablet Screen On'
  trigger:
    platform: state
    entity_id: binary_sensor.hue_sensor_motion
    from: 'off'
    to: 'on'
  action:
    - service: mqtt.publish
      data_template:
        topic: 'wallpanel/mywallpanel/command'
        payload_template: '{"wake":true}'

Hi @j.assuncao, @ESEL3000

I don’t think the automation structure is the problem (when using the automation editor in HA the structure is completely different than in the examples).

I think that the problem is that MQTT it’s not running or the address is incorrect (“No route to host”).

Afterwards, there might be a problem with the trigger of the automation. Using a binary sensor as a trigger, it needs an “on” payload and an “off” payload to be able to switch from off to on, however it needs to have someone (or something) sending these two payloads. The “on” payload is simple, is sent by Hue sensor when motion is detected. However, as I think the Hue sensor doesn’t also send the “off” payload, you need to provide it.

Does the Hue sensor send also the off payload? Otherwise, the binary sensor state will be all the time “on”.

Do you need help setting up the automation for setting the binary sensor to off?

Have you tried MQTT command on Dev Tools? I’m using the same automation and it works…

The error points in that way.

thank you for your input.

I am pretty sure that MQTT is running because all my other automations with the hue motion sensor work fine (I followed a tutorial on github to set it up with mqtt).

Also, I pinged the tablets ip address and its fully reachable.

The motion sensor switches some (non hue) lights on and off, so I guess it does send an “off” payload.

Hi @ESEL3000

What does the sub command show? It should list all the messages received by the broker.

mosquitto_sub -h 192.168.0.26 -d -t “#”

with mosquitto_sub -h 192.168.0.26 -d -t “#” I get this: Error: Invalid subscription topic '“#”', are all '+' and '#' wildcards correct?

@ESEL3000

"#" should be used as identifier for topic instead of “#”

or simply '#'

silly me… with the actual topic I’m getting Error: Connection refused

Just noticed that I could use REST instead… so I created a notifier:

notify:
  - name: NOTIFIER_TABLET
    platform: rest
    resource: http://192.168.0.26:2971/api/state

automations:

- alias: 'Tablet Screen On'
  trigger:
    platform: state
    entity_id: binary_sensor.hue_sensor_motion
    from: 'off'
    to: 'on'
  action:
    - service: notify.notifier_tablet
      data:
        message: '{"wake":true}'

Still nothing

You need to escape the quotes in the JSON with backslashes:
'{\"wake\":true}'

And you don’t want to publish a payload_template because it’s not a template, it’s just formatted as JSON:

- alias: 'Tablet Screen On'
  trigger:
    platform: state
    entity_id: binary_sensor.hue_sensor_motion
    from: 'off'
    to: 'on'
  action:
    - service: mqtt.publish
      data:
        topic: 'wallpanel/mywallpanel/command'
        payload: '{\"wake\":true}'

thanks for the suggestion, but still no success. also no errors.

I tried the dev tool service with mqtt.publish as well…

this is the command line to view error log, right?:
tail -f /home/homeassistant/.homeassistant/home-assistant.log

Just to be sure I didn’t mess anything else, due to my limited knowledge of basically everything about mqtt: is there anything special I need to add to the configuration.yaml?

This is what I currently have:

mqtt:
 broker: 192.168.0.12
 port: 1883
 client_id: home-assistant-1
 username: mqtt
 password: xxxxxxxx

IP of my Pi: 192.168.0.12
IP of my tablet: 192.168.0.26

Mosquito is installed as well, obviously. I set up the hue motion sensor with the help of this tutorial.

Anything I might be missing?

This explains why you have connectivity issues with some of the commands - you need to address your messages and commands to the broker, not any individual client. And on the tablet you need to connect it to your broker IP. So you should subscribe (ie run this command from your pi’s command line and watch the result) by:

mosquitto_sub -h 192.168.0.12 -t "#" -v

Then activate the motion sensor and you should see your JSON payload get published to the broker and displayed in the terminal window.

My guess at this point is that your tablet isn’t connected to the broker, so it’s not picking up any messages, but the above test will confirm at least whether HA is properly publishing the message.

1 Like

Also the spacing isn’t right on your config snippet, it should look like this:

mqtt:
  broker: 192.168.0.12
  port: 1883
  client_id: home-assistant-1
  username: mqtt
  password: xxxxxxxx

May explain why HA isn’t connecting to mqtt if that’s the problem.

1 Like

thank you so much for your help, I really appreciate it!

with mosquitto_sub -h 192.168.0.12 -t "#" -v it get this:

hue/motion3000/presence true
hue/motion3000/lastupdated 2018-01-04T19:18:26
wallpanel/mywallpanel/command {\"wake\":true}

As for the tablet, I attached a photo. Its all the mqtt settings it’s got (default). The readme of the App says:

For MQTT:
• WallPanel subscribes to topic [baseTopic]/command
  • Default Topic: wallpanel/mywallpanel/command
• Publish a JSON payload to this topic

So I guess its already subscribed?