MQTT, Noob needs to take it a step further

Ok, so after watching Ben’s(BRUH) video, “$15 DIY Multisensor”, I was able to build and get a couple of these Multisensors working in HA.

Now I want to start using the PIR sensor to control some lights. So what do I want to Subscribe to, in “MQTT Lens” or HA, so that I only see the value, for the PIR sensor???

The following, is the code I am using, to setup one of the sensors,

  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Humidity"
    unit_of_measurement: "%"  
    value_template: '{{ value_json.humidity | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom LDR"
    unit_of_measurement: "LUX"
    value_template: '{{ value_json.ldr }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom PIR"
    value_template: '{{ value_json.motion }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Temperature"
    unit_of_measurement: "F"
    value_template: '{{ value_json.temperature | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Real Feel"
    unit_of_measurement: "F"
    value_template: '{{ value_json.heatIndex | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom WiFi RSSI"
    unit_of_measurement: "db"  
    value_template: '{{ value_json.signal_strength }}'

On another subject, Where is Ben???

Looks like you’re already subscribed to the topic:

  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom PIR"
    value_template: '{{ value_json.motion }}'

Now you’d need to create an automation based on the PIR value. Here is a basic example:

- alias: Switch Light On by PIR
  trigger:
    - platform: state
      entity_id: binary_sensor.livingroom_pir
      to: 'on'
  condition:
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
  action:
  - service: light.turn_on
    data:
      entity_id: light.livingroom
      brightness: 255

Important Note: This assumes you’ve declared your PIR sensor as a binary_sensor

I don’t think is is a binary.sensor, when I look at it’s states list, all I see is, sensor.livingroom_pir. This is the way Ben has it setup… Can I still make it work, or do I have too rewrite the code???

Your code in the first post, I’m guessing all the entries would be located under sensor: in your configuration.yaml
All you’d need to do is to add a header below all these called binary_sensor: and move the PIR sensor there:

sensor:
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Humidity"
    unit_of_measurement: "%"  
    value_template: '{{ value_json.humidity | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom LDR"
    unit_of_measurement: "LUX"
    value_template: '{{ value_json.ldr }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Temperature"
    unit_of_measurement: "F"
    value_template: '{{ value_json.temperature | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom Real Feel"
    unit_of_measurement: "F"
    value_template: '{{ value_json.heatIndex | round(1) }}'
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom WiFi RSSI"
    unit_of_measurement: "db"  
    value_template: '{{ value_json.signal_strength }}'
binary_sensor:
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom PIR"
    value_template: '{{ value_json.motion }}'

@lolouk44

By doing what you state above, breaks the Motion sensor, in HA. I get the following warning;

2018-06-13 17:51:00 WARNING (MainThread) [homeassistant.components.binary_sensor.mqtt] No matching payload found for entity: Kitchen PIR with state_topic: climate/ki-room

It probably because, in Ben’s Sketch for the sensor module, he uses only 1 mqtt topic to get ALL the info, for the 5 different sensors, and that is “climate/li-room”. When I put this topic into a program like “MQTT Lens”, I get the following back;

{"state":"OFF","color":{"r":255,"g":255,"b":255},"brightness":255,"humidity":"58.70","motion":"standby","ldr":"388","temperature":"77.54","heatIndex":"77.75","signal_strength":"-47"}

Unless there is another way, Looks like I may need to start learning how to rewrite Ben’s sketch.

It’s probably because Ben doesn’t return standard values for motion on and motion off.
Add a couple of lines to tell HA what status means motion on and what status means motion off:

binary_sensor:
  - platform: mqtt
    state_topic: "climate/li-room"
    name: "Livingroom PIR"
    value_template: '{{ value_json.motion }}'
    payload_on: "insert status representing motion detected here"
    payload_off: "standby"

OK, so that fixed it.

But, in HA it is now showing “ON” for Motion Detected, and “OFF” for Standby(no motion). Which I can live with, but it would be nice for HA to display Motion Detected or Standby.

Thanks for the help

You’ll find more info on how too personalise your sensor here

What you need is to define the device class to motion

Thanks again, that looks better…