Finding Energy Usage with SQL

I have a whole home energy monitor and a bunch of monitoring plugs. I also have a few things I can’t easily monitor. My dryer is 220 and those monitors are expensive. My HVAC furnaces is electric and seems to be an energy hog. I decided to add a Watt sensor when it is on and a kWh sensor so that it can show on the energy monitor:

sensor:
  - platform: integration
    name: "Dryer [kWh]"
    source: sensor.dryer_w
    unit_prefix: k
    method: left
...
template:
- sensor:
    - name: "Dryer [W]"
      unique_id: dryer_w
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      attributes:
        last_reset: '1970-01-01T00:00:00+00:00'
      state: >-
        {% if is_state('binary_sensor.dryer_accelerometer', 'on') %}
          1950
        {% else %}
          0
        {% endif %} 

The question is “where did I get 1950 from?”

To do this, I have to know how much electricity it used. I used the following tools and devices:

  1. Whole home energy monitor: sensor.home_energy_meter_electric_consumption_w
  2. Dryer Accelerometer: binary_sensor.dryer_accelerometer
  3. Mariadb add-on
  4. phpMyAdmin add-on
  5. SQL

I used SQL to determine the time ranges of when the device is on, the average watts from the energy meter while it is on, the average watts from the energy meter 10 minutes before and 10 minutes after.

Here is the SQL statement:

 SELECT *,
       Round(a.avg_watts - a.watts_before_after) used_watts
FROM   (SELECT old_states.created                                            "start",
               states.created                                                "end",
               Timestampdiff(minute, old_states.created, states.created)     "minutes",
               (SELECT Round(Avg(sw.state))
                FROM   states sw
                WHERE  sw.entity_id = "sensor.home_energy_meter_electric_consumption_w"
                       AND sw.created > old_states.created
                       AND sw.created < states.created)                      avg_watts,
               (SELECT Round(Avg(sw2.state))
                FROM   states sw2
                WHERE  sw2.entity_id = "sensor.home_energy_meter_electric_consumption_w"
                       AND ( ( sw2.created > states.created
                               AND sw2.created < Timestampadd(minute, 30, states.created) )
                              OR ( sw2.created > Timestampadd(minute, -30, old_states.created)
                                   AND sw2.created < old_states.created ) )) watts_before_after
        FROM   states states,
               states old_states
        WHERE  states.entity_id = "binary_sensor.dryer_accelerometer"
               AND states.old_state_id = old_states.state_id
               AND old_states.state = "on"
               AND states.state = "off"
               AND states.created > "2022-03-22"
               AND Timestampdiff(minute, old_states.created, states.created) > 1) a;  

This found the average watts used while the dryer’s accelerometer is moving and the average of the watts reported 10 minutes before and 10 minutes after. I limited it to states created after March 22 so you may need to change this. The results look like this:

If you just want an average, change the first select to:

SELECT avg(
       Round(a.avg_watts - a.watts_before_after)) used_watts
FROM   (SELECT old_states.created  ...

I know this is a niche task but hopefully it is helpful to someone!