Observe your Home Energy Consumption with Home Assistant and Dynatrace

Renewable energy and intelligent management of private and industrial energy consumption are prominent topics today.

In a recent post I did show how to use a DIY ESP8266 sensor in combination with Home Assistant in order to collect real time data about your energy consumption as well as about the amount of energy that you produce with your solar panels.

Along with collecting real time data about your energy consumption, Home Assistant is a great choice for automating selected tasks within your home in order to reduce the energy consumption or to optimize the consumption in relation to your solar energy gain.

Within this post I will also share a convenient method for forwarding those MQTT based measurements with a monitoring platform such as Dynatrace, in order to alert on anomalies or simply to keep track of your long term energy consumption over time.

Use a RESTful command to forward measurements to Dynatrace

A convenient way of triggering all kinds of HTTP integrations within Home Assistant are the so-called RESTful commands

Those RESTful commands simply consist of a declared HTTP call, along with its payload that can be used within any kind of action or trigger within your Home Assistant system.

Those RESTful commands are used a lot for various practical tasks, for example to regularly announce my local IP to a DynDNS provider or to simply trigger a task through a Dashboard button.

In this case, I wanted to forward some of my collected MQTT energy measurements to my Dynatrace monitoring environment.

Therefore, I declared a RESTful command as it is shown below to send a plaintext payload to a Dynatrace metric ingest HTTP API endpoint.

Please mind that the extra empty text line between the metric line protocol is necessary to resemble a newline between those individual metrics.

Also note that the RESTful command uses parameter placeholders, e.g.: {{ energy_from_grid }} that need to be set from any triggering action, as it is shown further below within the automation definition.

It’s not possible to use the sensor state directly within the RESTful declaration. It took me some while to find that out.

See below the RESTful command definition within the Home Assistant Configuration.yaml file that is used to send three energy measurements to the given Dynatrace environment.

rest_command:
  send_metrics_to_dynatrace:
   url: 'https://<YOUR_ENVIRONMENT>.live.dynatrace.com/api/v2/metrics/ingest'
   method: post
   content_type: 'text/plain; charset=utf-8'
   headers:
     authorization: 'Api-Token <YOUR_TOKEN>'
   payload: >
     homeassistant.energy.fromgrid {{ energy_from_grid }}
 
     homeassistant.energy.togrid {{ energy_to_grid }}
 
     homeassistant.energy.pv {{ energy_from_pv }}

Automation to forward the MQTT metric to Dynatrace

Besides the definition of the RESTful command, we also need a scheduled automation that triggers the command in a regular interval to forward the current metrics to Dynatrace.

See below the definition of an automation that triggers the RESTful command once a minute to forward those three metrics to your Dynatrace environment.

A sensor state needs to be extracted by using the states variable. It’s assigned to a named variable, e.g.: energy_from_grid that can then be used within the payload of your RESTful command.

You can define and assign any kind of metric from any kind of sensor within your Home Assistant system. You are not limited to just those three metrics that I am sending below.

See below the automation definition within the automation.yaml file:  

- id: send_metrics_to_dynatrace
 alias: Send Home Automation Metrics to Dynatrace
 trigger:
   platform: time_pattern
   minutes: /1
 action:
   service: rest_command.send_metrics_to_dynatrace
   data:
     energy_from_grid: "{{ states.sensor.amis_smartmeter_power_received_w.state| float }}"
     energy_to_grid: "{{ states.sensor.amis_smartmeter_power_returned_w.state| float }}"
     energy_from_pv: "{{ states.sensor.power_ac_fronius_inverter_1_192_168_0_124.state| float }}"

After successfully defining both, the RESTful command as well as the automation schedule, you can receive and chart your energy metrics within Dynatrace as it is shown below:

Summary

Observing your home’s energy consumption in real time is an amazing possibility to raise your awareness of your own power consumption and to optimize its usage.

Both Home Assistant as well as Dynatrace are great platforms so it’s beneficial to forward your energy measurements into Dynatrace for further analysis and long term storage.

By using a simple RESTful command along with the genius metric ingest API of Dynatrace it is easily possible to analyze and chart your home’s energy usage also within Dynatrace.