Motion MQTT message won't be interpreted correctly

I’m trying to move the Philips Hue integration to Mosquitto as I had too much delay between receiving the Motion trigger (Hue Motion sensor) and action on this (Philips Hue spot).
This was caused by the polling interval for the Hue integration which can;t be adjusted, but a workaround would be using MQTT in between.

What I have is that a motion will create the following MQTT message:

Topic: hue/sensor/hal
Payload:
{
    "battery": 100,
    "motion": {
        "active": true,
        "motion": true
    }
}

What I have tried is:

  - platform: mqtt
    name: 'test 1'
    state_topic: 'hue/sensor/hal'
    value_template: "{{ value_json.motion }}"
    payload_on: true
    payload_off: false
    device_class: 'motion'
  - platform: mqtt
    name: 'test 2'
    state_topic: 'hue/sensor/hal'
    value_template: "{{ value_json.motion.motion }}"
    json_attributes_topic: "hue/sensor/hal"
    json_attributes_template: "{{ value_json.motion | tojson }}"

BotH don’t work, as the status is not updated when motion is detected.

Also tried to setup an Automation directly on MQTT.

Trigger
- Type : MQTT
- Subject: hue/sensor/hal
Condition
Type: Template
Value: {{ trigger.payload_json['motion']['motion'] == true }}

This automation, but get some error in the log:

Logger: homeassistant.helpers.condition
Source: helpers/condition.py:458
First occurred: 21:33:06 (66 occurrences)
Last logged: 21:59:53
Error during template condition: UndefinedError: 'dict object' has no attribute 'motion'

So question is how I get:

  1. the entity correctly configured
  2. the automation working without creation errors

Btw: I’m running core-2021.2.3

Thanks!

Philips Hue is a Zigbee system, how did you manage to create MQTT messages of it, without using Zigbee2MQTT?

Whoops…you are right.
Use node-red for retrieving the messages from the Hue Bridge and defining the mqtt json

I had the same situation and I don‘t see how Node Red can speed up things, as you still need to pull the sensor‘s state from the Bridge.

Instead, I bought Zigbee stick, installed Zigbee2mqtt and moved my Hue devices (light bulbs, LED strips and motion sensors) there.

Now motion is reported almost immediately, as is switching done. Zigbee2mqtt uses the MQTT discovery feature of Home Assistant and associated devices are automatically added to Home Assistant.

Hue bridge is now on eBay.

By using Node-red you can adjust the poll interval (to e.g. 500 ms) which is faster then HASS.
This setup was based on multiple discussions on internet.

But still doesn’t answer my question how I can set the enitity / trigger correct in HASS.

You are right. But I have no experience with node red. Maybe the device‘s page at Zigbee2MQTT is helpful, it explains the appropriate HA entity setup.

Try :

  - platform: mqtt
    name: 'test 1'
    state_topic: 'hue/sensor/hal'
    value_template: "{{ value_json.motion.active }}"
    payload_on: true
    payload_off: false
    device_class: 'motion'

or

  - platform: mqtt
    name: 'test 1'
    state_topic: 'hue/sensor/hal'
    value_template: "{{ value_json.motion.motion }}"
    payload_on: true
    payload_off: false
    device_class: 'motion'

@francisp Thanks, that did the (first) trick! :slight_smile:

Next is that I now get

Logger: homeassistant.helpers.template
Source: helpers/template.py:514
First occurred: 12:11:33 (71 occurrences)
Last logged: 12:43:13

Error parsing value: 'dict object' has no attribute 'motion' (value: {"uid":"###hashed_by_me###","battery":100,"temperature":{"celcius":23.52}}, template: {{ value_json.motion.motion }})
Error parsing value: 'dict object' has no attribute 'motion' (value: {"uid":"###hashed_by_me###","battery":100,"brightness":{"lux":65,"lightlevel":18161,"dark":false,"daylight":false}}, template: {{ value_json.motion.motion }})

as I also get the following MQTT message:

{
  "uid": "###hashed_by_me###",
  "battery": 100,
  "brightness": {
    "lux": 124,
    "lightlevel": 20927,
    "dark": false,
    "daylight": false
  }
}

Can I also sort this out ?
Or do I need to split the topics, so 1 for motion, 1 for temperatute and 1 for light level ?
I now combined them into \hue\sensor\hal as all info is send from the same sensor ,only different message content.

If you can split them out, it is the easiest way to fix.