Combine Template Sensor and MQTT sensor

Hi all,

I´m just trying to figure out a way to combine a template sensor and an MQTT sensor.
I´m using Node-RED with the bluelinky nodes to gather information from my car. I then send this information to my MQTT Broker.

In my sensors.yaml file I then create an MQTT sensor and read out the data. Unfortuntaly for example my trunk gives out the message true or false.

- platform: mqtt
  name: "Kona Kofferraum"
  state_topic: "kona/status"
  value_template: "{{ value_json.status.trunkOpen }}"
  unique_id: sensor.kona_kofferraum

So I then tried to convert that with a template sensor (also in sensors.yaml)

- platform: template
  sensors:
    kofferraum:
      friendly_name: "Kofferraum"
      value_template: "{% if is_state('sensor.kona_kofferraum', 'False') %}Geschlossen{% else %}Offen{% endif %}"
      icon_template: mdi:car-windshield

All of this is working but I´m trying to figure out a way to save a few lines and trying to gather and convert the values in one step. Does anyone have an idea for that?

Maybe a bit off-topic but also related to this current project.

Is there any way so maybe split these sensors to a different file as my sensors.yaml kind of starts to look like a mess with all of these mqtt sensors.

Thaks in advance for your help!

Regards
Stefan

Anyone has an idea?

Just put your logic in the mqtt sensor

Hi,

thanks for your answer and sorry for the late reply.
How would a do that for the example above. Tried serveral ways but cant seem to figure out how to do this.

Assuming trunkOpen is either True or False, this should do what you want:

- platform: mqtt
  name: "Kona Kofferraum"
  state_topic: "kona/status"
  value_template: "{{ 'Geschlossen' if value_json.status.trunkOpen == 'False' else 'Offen' }}"
  unique_id: sensor.kona_kofferraum

Perfect, thats exactly what I was looking for. Thank you so much for your help.

1 Like

I use the Bluelinky and use a MQTT Binary Sensor for doors, bonnet, boot, engine etc
I send separate MQTT message from Node-Red for just the items I’m interested in and not the whole status message, not sure what the best way is.

- platform: mqtt
  name: Bonnet
  state_topic: "kia_nr/bonnet"
  payload_on: "true"
  payload_off: "false"
  device_class: opening

Doors use a different payload:

- platform: mqtt
  name: Door Front Left
  state_topic: "kia_nr/door_fl"
  payload_on: "1"
  payload_off: "0"
  device_class: opening
1 Like

Hi,

thanks for your reply. In case you or anyone else will see this post heres how I´m doing it now.
I tried to keep it as clean and simple as possible on the node red . I do use the nodes provided by the Node Red bluelinky plugin and simpley send them to my MQTT broker. I dont use a new node for every attribute as seen in many examples. The filter behind that all sits in my Template sensors. One of the main advantages I see there, is that I can manipulate everything (Name of a satus, Icon, etc.) in the template sensor as I want.

So this is what my Node Red flow looks like:

And here you can see a snippet of my sensors.yaml

- platform: mqtt
  name: "Kona Reichweite"
  state_topic: "kona/status"
  value_template: "{{ value_json.status.evStatus.drvDistance[0].rangeByFuel.evModeRange.value }}"
  unit_of_measurement: 'km'
  icon: mdi:counter
- platform: mqtt
  name: "Türen"
  state_topic: "kona/status"
  value_template: "{{ 'Verriegelt' if value_json.status.doorLock == True else 'Entriegelt' }}"
- platform: mqtt
  name: "Motorhaube"
  state_topic: "kona/status"
  value_template: "{{ 'Geschlossen' if value_json.status.hoodOpen == 0 else 'Offen' }}"
  unique_id: sensor.kona_motorhaube

Of course this would also work for other MQTT template sensors and is not specific to my use case that I get this data from Node-RED. Every other source should work aswell.

Hope this is going to be helpful for someone also trying to find a solution for that.