Send Bluetooth over mqtt to second system

Hi,

Using a bare Hassio Pi3 with Mosquitto as a broker I have my main 2nd setup read that and create mqtt sensors. (Had the Mosquitto broker in my main setup before, but since that is a rather processor intense system, I decided to split them and it works really nice.)

Which made me wonder if I could do something alike with the now disabled bluetooth in my setup. Bluetooth caused all kinds of timing issues, so I disabled it from the main setup.

Could I enable it on the bare system, and create mqtt sensors on the main setup reading the bare systems bluetooth sensors?

Please help me in setting this up?

In fact it should not be much of a big setup, it’s only about 6 devices I’d like to track, and the odd BT tracker of passersby.
If at all possible I’d like it to be an automatic setup a la @NotoriousBDG 's Howto create battery alert without creating a template for every device

as an example I have these sensors on the main system

sensor:
  - platform: mqtt
    name: Activity selection
    state_topic: 'activity_selection'

and feeding automation action on the bare system setup:

  - service: mqtt.publish
    data_template:
      topic: 'activity_selection'
      retain: true
      payload: >
        {{ states('input_select.activity') }}

Not sure how to do this with a bluetooth sensor though…

  - alias: 'device 1 BT sensor'
    id: 'device 1 BT sensor'
    trigger:
      platform: state
      entity_id: sensor.device1_bt
    condition:
      condition: template
      value_template: >
        {{ states('sensor.device1_bt') in 
            ['home','not_home'] }}
    action:
      - service: mqtt.publish
        data_template:
          topic: 'device_1_bt_state'
          retain: true
          payload: >
            {{ states('sensor.device1_bt') }} # or trigger.to_state.state ?

and then a sensor on the second main system:

sensor:
  - platform: mqtt
    name: Device 1 Bt
    state_topic: 'device_1_bt_state'

or would I need:

device_tracker:
  - platform: mqtt_json
    devices:
      device1_bt: device_1_bt_state

would this work?

that for your insights

This is what I use to forward sensors over mqtt.

- alias: Sensor from State
  id: sensor_from_state
  trigger:
    - platform: state
      entity_id:
        - sensor.sensor1
        - sensor.sensor2
        - sensor.sensor3
  condition:
    - condition: template
      value_template: "{{ trigger.to_state.state != trigger.from_state.state }}"
  action:
    - service: mqtt.publish
      data_template:
        topic: "homeassistant/{{ trigger.entity_id | replace('.', '/') }}/config"
        retain: true
        payload: >-
          {
            "name": "{{ trigger.to_state.attributes.friendly_name }}",
            "state_topic": "homeassistant/{{ trigger.entity_id | replace('.', '/') }}/state",
            {%- if trigger.to_state.attributes.unit_of_measurement and trigger.to_state.attributes.unit_of_measurement is defined %}
            "unit_of_measurement": "{{ trigger.to_state.attributes.unit_of_measurement }}",
            {%- endif %}
            "source_entity_id": "{{ trigger.entity_id }}",
            "source_attribute": "state"
          }
    - service: mqtt.publish
      data_template:
        topic: "homeassistant/{{ trigger.entity_id | replace('.', '/') }}/state"
        # retain: true
        payload: "{{ trigger.to_state.state }}"

For device trackers, I use this:

- alias: Send Device Tracker State to MQTT
  id: Send_Device_Tracker_State_to_MQTT
  trigger:
  - platform: state
    entity_id:
    - device_tracker.device_1
    - device_tracker.device_2
    - device_tracker.device_3
  action:
  - service: mqtt.publish
    data_template:
      topic: "location/{{ trigger.entity_id.split('.')[1] }}"
      # retain: true
      payload: "{{ trigger.to_state.state }}"
1 Like

thank you very much!

Am I right the source type isn’t forwarded with the device_tracker? Would that be doable by adding that to the payload?

57

Is there a short explanation you prefer doing it like this, over using mqtt state_stream?
I tried the latter also, but had issues using both domains and entities belonging to the domain. Somehow using the domain device_tracker wouldn’t stream the entities I set for my bluetooth trackers if I used both.

Y

Yes

I don’t believe that’s possible with this method.

There are a few reasons. First, the entire configuration is on a single device. I don’t need to set everything up twice. I want the option to retain state for some entities and I can control which messages have the retain flag set. Lastly, I have the option of manipulating the data before sending it.