How to look for boolean values in value_template (MQTT Automation)

I’m not very experienced with MQTT, so this could be a stupid question.

Anyway I have a body movement sensor and a simple light bulb which send their states through MQTT. I want it to check for no movement, and then turn off the light (and on when true).

This is how I currently have the automation set up:

- id: '123456789'
  alias: light bedroom2
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/movement_sensor_room_x
  condition:
    condition: template
    value_template: "{{ trigger.payload_json['occupancy'] == false }}"
  action:
  - data:
      payload: 'OFF'
      topic: zigbee2mqtt/light_bedroom_2/set
    service: mqtt.publish

It however does pretty much nothing.

This is the payload of the sensor:

{"occupancy":false,"linkquality":73,"battery":100,"voltage":3025}

And of the light:

{"state":"ON","linkquality":70,"brightness":254}

I’ve also tried something like this which also didn’t work:

automation:
  trigger:
    platform: mqtt
    topic: zigbee2mqtt/movement_sensor_room_x
    payload: '{"occupancy":false}'
  action:
    service: mqtt.publish
	- data:
	    payload: 'OFF'
	    topic: zigbee2mqtt/outlet_livingroom/set

I’m pretty sure the error is in this bit: value_template: “{{ trigger.payload_json[‘occupancy’] == false }}” but quite frankly I’m not sure how to fix it. Any ideas?

Maybe you need quotes around “false” or it won’t be a string but the special state “false” == 0

value_template: "{{ trigger.payload_json['occupancy'] == 'false' }}"

There’s nothing wrong with this:

    value_template: "{{ trigger.payload_json['occupancy'] == false }}"

or with this:

    value_template: "{{ trigger.payload_json.occupancy == false }}"

I tested both and they work (using the sample payload you provided).

- alias: 'boolean test'
  trigger:
  - platform: mqtt
    topic: test/sensor
  condition:
    condition: template
    value_template: "{{ trigger.payload_json['occupancy'] == false }}"
  action:
  - service: mqtt.publish
    data:
      payload: 'abc'
      topic: test/light

Go to Home Assistant’s States page and confirm the automation is on.

Thanks for the reponse. The automation was indeed off! Never would’ve thought to look there.

To prevent this happening in future, set:

initial_state: 'true'