Combine two sensors (MQTT and template)

Is it possible to combine these two sensors:

sensor:
  - platform: mqtt
    name: "Sonoff PIR2 Staircase"
    state_topic: "tele/sonoff-bridge-1/RESULT"
    payload_on: "XXXXXX"
    expire_after: 10
    value_template: '{{ value_json["RfReceived"]["Data"] }}'

  - platform: template
    sensors:
      sonoff_pir2_staircase_template:
        friendly_name: "Beweging" # Movement
        value_template: >-
          {% if is_state('sensor.sonoff_pir2_staircase', 'XXXXXX') %}
            Ja # Yes
          {% else %}
            Nee # No
          {% endif %}

I’ve tried the following but it didn’t work:

sensor:
  - platform: mqtt
    name: "Sonoff PIR2 Staircase"
    state_topic: "tele/sonoff-bridge-1/RESULT"
    payload_on: "XXXXXX"
    expire_after: 10
    value_template: >-
      {% if is_state('{{ value_json["RfReceived"]["Data"] }}', 'XXXXXX') %}
        Ja # Yes
      {% else %}
        Nee # No
      {% endif %}

You can’t use is_state like that, but depending on what XXXXX is (or could be) you could probably use an operator?

Something like…

{% if {{ value_json["RfReceived"]["Data"] }} == 'XXXXXX' %}

The XXXXXX is a hex code I receive from my Sonoff Bridge. This is a unique identifier (The Sonoff can send up to 16 different RF codes).

I tried to use your given code example:

- platform: mqtt
  name: "Sonoff PIR2 Staircase"
  state_topic: "tele/sonoff-bridge-1/RESULT"
  payload_on: "XXXXXX"
  expire_after: 10
  value_template: >-
    {% if {{ value_json["RfReceived"]["Data"] }} == 'XXXXXX' %}
      Ja
    {% else %}
      Nee
    {% endif %}

but I get the following error:

Failed config
  sensor.mqtt:
    platform: mqtt
    expire_after: 10
    name: Sonoff PIR2 Staircase
    state_topic: tele/sonoff-bridge-1/RESULT
    value_template: {% if {{ value_json["RfReceived"]["Data"] }} == 'XXXXXX' %}
      Ja
    {% else %}
      Nee
    {% endif %}

Am I doing it right?

I did say “something like”, you’ll have to make sure that the template resolves correctly.

Also, you are using a multi-line template so you need to put it like…

value_template:  >
    {% if {{ value_json["RfReceived"]["Data"] }} == 'XXXXXX' %}
         Ja
      {% else %}
          Nee
      {% endif %}

(obviously lined up better, I’m on my mobile atm)

… And finally, forget the code checker and run the code, the error message you get when it doesn’t work is far more helpful than ‘failed config’, which gives us no clue what’s going wrong.

I just flashed my sonoff bridge and i have my pir2 motion sensor as well. What i strugle with is how can I connect them to use it with MQTT as in your example?

Here are two links to my code:

https://github.com/maartenpaauw/home-assistant-config/blob/master/sensors/sonoff_pir2_staircase.yaml
https://github.com/maartenpaauw/home-assistant-config/blob/master/sensors/sonoff_rf_bridge_1.yaml

The code D23EBE is the code the devices (RF Bridge) learned from the PIR sensor.

Hope this helps.

I was able to set the bridge in leaning mode and then activated the pir2 to learn the signal. How am I know able to know what code he got from it? I looked in the console of the bridge but nothing showed up there.

Go to the console and send: RfKeyX 2 - Learn RF data for RfKey1 to RfKey16

X is one of the buttons.