Sometimes sensor data is only available in Home Assistant, or can't be transformed by the glue logic into the appropriate MQTT message to be picked up by the COFYY-cloud component. This page will show you how to set up automations in Home Assistant to push data to the COFY-cloud.
You first need to find the entity IDs of the sensors/states which you want to forward (these will be our 'ingredients'). There are several ways to find these.
Settings
> Devices and services
> Devices
. Open the device and click on the sensor. A pop-up should appear displaying the entity ID, e.g. sensor.shelly_em_98cdac1f2b1a_meter_energy_1
.Developer tools
> States
. Locate the required entity ID(s) in this list.Settings
> Automation & Scenes
. In the tab Automations
, click on the Create automation
button. In the pop-up window, select Start with an empty automation
.Edit in YAML mode
. We can now write our 'recipe'.A basic recipe, which takes the energy (kWh) value of a Shelly, encapsulates it in the COFY payload format and sends it to the appropriate MQTT topic to be picked up by the COFY cloud component looks like this:
alias: Shelly-to-utility-meter energy
description: ''
trigger:
- platform: state
entity_id:
- sensor.shelly_em_98cdac1f2b1a_meter_energy_1
condition: []
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/total_energy_consumed
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityImport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy consumed",
"value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_consumed"}
This recipe is triggered by a 'state change' (new value) of the sensor.shelly_em_98cdac1f2b1a_meter_energy_1
entity. It then calls the mqtt.publish
service and sends the payload
to the configured topic
.
Save
button and return to the automation overview by clicking the arrow in the top left corner. You should see your new automation appearing there. Check if the automation is enabled (slider to the right).You can test your automation recipe before writing the actual automation.
Developer tools
> Services
. Select the MQTT: Publish
service from the drop-down list.Go to YAML mode
.service: mqtt.publish
data:
topic: test
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityImport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy consumed",
"value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_consumed"}
mosquitto_sub -v -t test
.Call service
, you should see the MQTT message appearing in the shell. If not, you should see an error message in Home Assistant, allowing you to debug your recipe.Automation recipes can be templated, allowing mathematical operations on the sensor values, combining or substracting values, ...
Example of a recipe combining two energy values into one
alias: Shelly-to-utility-meter energy
description: ''
trigger:
- platform: state
entity_id:
- sensor.shelly_em_98cdac1f2b1a_meter_energy_1
- sensor.shelly_em_98cdac1f2b1a_meter_energy_2
condition: []
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/total_energy_consumed
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityImport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy consumed",
"value": {{ (states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float + states.sensor.shelly_em_98cdac1f2b1a_meter_energy_2.state | float) | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_consumed"}
Example of a recipe converting Wmin to kWh (and rounding it to 2 decimal places)
alias: Shelly-to-utility-meter energy conversion
description: ''
trigger:
- platform: state
entity_id:
- sensor.shelly_em_98cdac1f2b1a_meter_energy_1
condition: []
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/total_energy_consumed
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityImport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy consumed",
"value": {{ ((states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float) / 60000) | round(2) | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_consumed"}
Example of a recipe taking a sensor indicating the active power from a PV inverter and forwarding it to the COFY-cloud.
alias: "MQTT: push solar active power"
description: ""
trigger:
- platform: state
entity_id:
- sensor.solar_active_power
condition: []
action:
- service: mqtt.publish
data:
topic: data/devices/solar/total_active_power
payload: >
{"entity":"solar_inverter",
"metric":"PvElectricityPower", "metricKind":"gauge", "unit":"W",
"friendly_name":"Solar active power injection", "value": {{
states.sensor.solar_active_power.state | float | string }},
"sensorId":"solar.total_active_power"}
mode: single
The following are the standard COFY topics and payload formats for the utility_meter
device.
Total energy consumed
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/total_energy_consumed
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityImport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy consumed",
"value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_0.state | float | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_consumed"}
Total energy injected
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/total_energy_injected
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityExport", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Utility meter total energy injected",
"value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_energy_1.state | float | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.total_energy_injected"}
Voltage phase 1
action:
- service: mqtt.publish
data:
topic: data/devices/utility_meter/voltage_phase_1
payload: >
{"entity":"utility_meter",
"metric":"GridElectricityVoltage", "metricKind":"gauge",
"unit":"V", "friendly_name":"Utility meter voltage phase 1",
"value": {{ states.sensor.shelly_em_98cdac1f2b1a_meter_voltage_0.state | float | string }},
"timestamp": {{ now().timestamp() | int | string }},
"sensorId":"utility_meter.voltage_phase_1"}
The following are the standard COFY topics and payload formats for the solar_inverter
device.
Total energy
action:
- service: mqtt.publish
data:
topic: data/devices/solar/total_energy_injected
payload: >
{"entity":"solar_inverter",
"metric":"PvElectricityProduction", "metricKind":"cumulative",
"unit":"kWh", "friendly_name":"Solar total energy injected", "value":
{{ states.sensor.solar_total_energy_injected.state | float | string }},
"sensorId":"solar.total_energy_injected"}
mode: single
Total active power
action:
- service: mqtt.publish
data:
topic: data/devices/solar/total_active_power
payload: >
{"entity":"solar_inverter",
"metric":"PvElectricityPower", "metricKind":"gauge", "unit":"W",
"friendly_name":"Solar active power injection", "value": {{
states.sensor.solar_active_power.state | float | string }},
"sensorId":"solar.total_active_power"}
mode: single