The following script (script.create_sensors
) contains five service calls. Each service call is an mqtt.publish
creating a sensor via MQTT Discovery. You can easily see all of the sensor information you provided is included in the payload of each mqtt.publish
call.
The payloads are published as retained messages which means they will be stored on the MQTT broker; their configuration information will be instantly available to Home Assistant on startup. You don’t have to run the script more than once (unless you want to change the configuration of one or more of the five sensors).
In addition to creating the five sensors, it groups them together within a single device called ESP9. This is only possible with MQTT Discovery and cannot be done with manual MQTT configuration. I’ve included this in the script as a convenience so when you go to Configuration > Devices > ESP9, you will see all the sensors related to it.
IMPORTANT
Do not execute this script before you first remove your existing five MQTT sensors from your system. If you don’t remove them and run this script, you will create duplicated sensors.
I suggest you delete, or comment out, the existing 5 sensor configurations then execute Configuration > Server Controls > Reload Manually Configured MQTT Entities to purge them from Home Assistant. Then run the script and they should all re-appear but this time they will be created via MQTT Discovery.
For more information about MQTT Discovery, refer to its documentation.
Show script
script:
create_sensors:
alias: "Create sensors via MQTT Discovery"
sequence:
- service: mqtt.publish
data:
topic: homeassistant/sensor/temperatura_dnevna/config
retain: true
payload: >
{
"name": "Temperatura Dnevna",
"unique_id": "esp9_temperature_dneva",
"device_class": "temperature",
"unit_of_measurement": "°C",
"state_topic": "home/esp9/dnevna",
"value_template": "{{ value_json.temperatura }}",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}
- service: mqtt.publish
data:
topic: homeassistant/sensor/vlaznost_dneva/config
retain: true
payload: >
{
"name": "Vlažnost Dnevna",
"unique_id": "esp9_vlaznost_dneva",
"unit_of_measurement": "%",
"state_topic": "home/esp9/dnevna",
"value_template": "{{ value_json.vlaznost }}",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}
- service: mqtt.publish
data:
topic: homeassistant/sensor/datum_dneva/config
retain: true
payload: >
{
"name": "Datum Dnevna",
"unique_id": "esp9_datum_dneva",
"state_topic": "home/esp9/dnevna",
"value_template": "{{ value_json.datum }}",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}
- service: mqtt.publish
data:
topic: homeassistant/sensor/cas_dneva/config
retain: true
payload: >
{
"name": "Čas Dnevna",
"unique_id": "esp9_cas_dneva",
"state_topic": "home/esp9/dnevna",
"value_template": "{{ value_json.cas }}",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}
- service: mqtt.publish
data:
topic: homeassistant/sensor/senzor_dneva/config
retain: true
payload: >
{
"name": "Senzor Dnevna",
"unique_id": "esp9_senzor_dneva",
"state_topic": "home/esp9/pinstatus",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}
- service: mqtt.publish
data:
topic: homeassistant/sensor/status_dneva/config
retain: true
payload: >
{
"name": "Status Dneva",
"unique_id": "esp9_status_dneva",
"state_topic": "home/esp9/dummy",
"device": {
"identifiers": ["esp9"],
"name": "ESP9",
"model": "ESP",
"manufacturer": "Espressif",
"sw_version": "1.XX"
}
}