Create a mqtt sensor with multiple entities

Hi, I’d love to get assistance from you.
The add-on Backup to Google Drive provides mqtt message with its last action results.
The payload looks like

# {"backupTimestamp": "2021-04-10T16:44:18.371463", "fromPattern": "/backup/*.tar", "backupDirID": "secretefolder", "fileCount": 7, "alreadyCount": 7, "backedUpCount": 0, "deletedCount": 0}

I’d like to have a lovelace card with an extract of the payload, e.g.:
backupTimestamp
fileCount
alreadyCount
backedUpCount
deletedCount

For this I’ve created a sensor:

sensor:
  - platform: mqtt
    name: "Google Backup Results"
    state_topic: "googlebackup/result"
    json_attributes_topic: "googlebackup/result"
#    json_attributes_template: '{ { value_json.backedUpCount } { value_json.deletedCount } }'
#    unit_of_measurement: ""
#    value_template: '{{ value_json.backedUpCount }}'

Actually I expected to a get sensor with more entities based on the topic dictionary but it does not work like this. I’m getting just the whole payload string.

Next I tried to get single values: value_template: ‘{{ value_json.backedUpCount }}’ This works fine. But I do not want to create 5 sensors listening to the same message and extracting just one value after another.

Question is it possible (and how) to create one sensor with 5 attributes / entities I can then use on lovelace such as:

type: entities
entities:
  - entity: sensor.google_backup_results_backupTimestamp
  - entity: sensor.google_backup_results_fileCount
state_color: false
show_header_toggle: true

And just by the way is there any nice way to translate the time stamp from
2021-04-10T16:44:18.371463
to
2021-04-10 16:44:18
For now I use template:

{{ value_json.backupTimestamp[:10] }} {{ value_json.backupTimestamp[11:19] }}

But it looks odd to me.

Any suggestion is very much welcome.
Cheers
P.

Are you saying the following fails to create any attributes?

sensor:
  - platform: mqtt
    name: Google Backup Results
    state_topic: googlebackup/result
    value_template: '{{ value_json.backedUpCount }}'
    json_attributes_topic: googlebackup/result

Not at all!
The statement you have provided and I have already tested creates just one attribute backedUpCount, which is also properly reported - no problem at all.

My question is how to break down the complete payload into 5 properties I’m looking for to get on lovelace.

I wanted to have something similar to my zigbee thermometer, which provides temp, battery and pressure and this is available via mqtt.

Right now I can create 5 separate sensors using five dedicated “value_templates” to pickup the right dictionary values from the payload. However I found this solution far from the right way and I wonder if there is a better, smarter solution.

Thank you for your farther assistance!
P.

That’s odd because when I use the same MQTT Sensor configuration, with your sample payload, I get seven attributes (plus the usual friendly_name):

If I use this Entities Card configuration:

Click to reveal
type: entities
title: Google Backup Results
show_header_toggle: false
entities:
  - type: attribute
    entity: sensor.google_backup_results
    attribute: backupTimestamp
    name: Timestamp
    icon: 'mdi:clock-time-ten-outline'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: fromPattern
    name: Pattern
    icon: 'mdi:filter-outline'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: backupDirID
    name: Directory
    icon: 'mdi:folder-outline'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: fileCount
    name: Files
    icon: 'mdi:counter'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: alreadyCount
    name: Already
    icon: 'mdi:counter'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: backedUpCount
    name: Backed Up
    icon: 'mdi:counter'
  - type: attribute
    entity: sensor.google_backup_results
    attribute: deletedCount
    name: Deleted
    icon: 'mdi:trash-can-outline'

It appears like this in the Lovelace UI:

Screenshot from 2021-04-11 09-06-58

You should be able to replicate my results (and then we can address the timestamp’s appearance).

Strange, here is my result:
sensor config:

  - platform: mqtt
    name: Google Backup Results
    state_topic: googlebackup/result
    value_template: '{{ value_json.backedUpCount }}'
    json_attributes_topic: googlebackup/result

and the results:



I still cannot get the entities with 7 properties.

Any idea for further troubleshooting?

There may be some confusion with the use of Home Assistant’s terminology.

Your screenshot clearly shows you have a single sensor entity that has several attributes.

What you called “properties” is what is called “attributes” in Home Assistant.

The value of the sensor’s state and the values of its attributes can be displayed by an Entities Card. The YAML code for doing that is posted in my previous message (as well as a screenshot of its appearance in the UI). You only need to create an Entities Card, copy-paste the YAML code I have provided you, and you will get the same card in your UI.


NOTE

The second screenshot in your post is from Configuration > Entities. You should only expect to see entities listed there and so sensor.google_backup_results will appear there (not its attributes).

Great, many, many thanks, I overlooked your hidden yaml code of the card. Copy&paste solved it, no you solved it. So I can check now the backup task. Notification will come later.

May I come back to your earlier comment and bother your again:

(and then we can address the timestamp’s appearance).
Screenshot from 2021-04-11 20-14-16

I’m pretty new to the HA and hassio, and haven’t yet left the starting position thus all comments are very much appreciated - also those clarifying the naming conventions.
BR
P

It isn’t difficult to use a template to convert the following timestamp:

2021-04-10T16:44:18.371463

to this:

2021-04-10 16:44:18

However, you cannot use a template in the Entities Card so that prevents us from easily converting the backupTimestamp attribute into the desired format.

Where one can use a template is, of course, in the configuration of the MQTT Sensor. That allows for an easy workaround but only if you accept having backupTimestamp as the sensor’s state value instead of backedUpCount.

If that’s acceptable, then you need to change the MQTT Sensor’s configuration to this (the value_template is different from the original configuration):

- platform: mqtt
  name: Google Backup Results
  state_topic: googlebackup/result
  value_template: '{{ as_timestamp(value_json.backupTimestamp)|timestamp_local }}'
  json_attributes_topic: googlebackup/result

That will produce the desired timestamp format in the sensor’s state value:

All that remains is to modify the code in the existing Entities Card. Change this first part:

entities:
  - type: attribute
    entity: sensor.google_backup_results
    attribute: backupTimestamp
    name: Timestamp
    icon: 'mdi:clock-time-ten-outline'

to this:

entities:
  - entity: sensor.google_backup_results
    name: Timestamp
    icon: 'mdi:clock-time-ten-outline'

Here’s how it looks in the UI. The timestamp is now in the desired format.

Screenshot from 2021-04-11 16-52-05


NOTE

If it is not acceptable to change the sensor’s state value to backupTimestamp (and you want to keep it as backedUpCount) then things get more complicated.

Hi @123
Again many thanks, now I’ve got it and it looks as I wanted.

Just one remark for other beginners:
I did my homework with the templates manual apparently not carefully enough. The function as_timestamp is a key to convert datetime string to UNIX timestamp and then convert it to local time, therefore my test failed:

Best regards
P.