Sense Energy Monitor (Enhanced to include devices)

I’ve had a Sense Energy Monitor for just over a year, with ~40 discovered devices. I wanted to pull those devices into Home Assistant so that I could trigger automations (ex: dryer turned off) or send notifications (ex: stove left on).

Initially I achieved this with IFTTT and the webhooks service, basically creating a bunch of HTTP Sensors. This worked okay to get the on/off status, but the sensors were lost after any reboot and anyone whose used IFTTT will be familiar with the latency and reliability.

I then began trying to integrate the sensors using Node-RED and node-red-contrib-unofficial-sense. This allowed me to pull in “live” energy usage for each device (rather than just on/off) and worked really well, but it was hitting the HTTP API a lot. (I was polling every 30 seconds). I tried to switching over to MQTT discovery, but I was running into some compromises I wasn’t happy with. Basically, the Node-RED node only reports the energy usage of devices that are “on”, so every poll I would run an SQL query against the Home Assistant DB to pull all of the sense sensors, and any that weren’t reporting energy usage I would set to 0. The problem with MQTT was that there’s no way to specify the entity_id of a dynamic sensor and I needed the entity_id to be sensor.sense_XXX for my SQL query. I could have named the sensors “Sense XXX” or “XXX Sense” or something like that which would have set the entity_id in a useful way but I didn’t want Sense in the name in the frontend and I didn’t want to manually customize 40+ sensors with more being discovered by sense all the time. I also could have used flow/global variables in Node-RED to keep track of the sensors but I was worried about things getting out of sync if Home Assistant/Node-RED were rebooted.

This all led me to finally taking a shot at doing some native HA development.

TL;DR: This is a fork of the “official” sense component. This is my first real Python programming, and my first foray into development for Home Assistant. I’m sure the code is rough - I appreciate any constructive criticism.

To install, all you have to do is add sense.py to config/custom_components/sensor/sense.py, then you have to add devices: true to the sense sensor section of your configuration.yaml

    # Example configuration.yaml entry
    sensor:
      platform: sense
      email: CLIENT_ID
      password: CLIENT_SECRET
      monitored_conditions:
        - active_usage
        - active_production
        - daily_usage
        - daily_production
      devices: true

NOTE: I did make a couple key changes from the official component

  1. ALL sensors (including the active_usage, active_production, daily_usage, etc.) have an entity_id of sensor.sense_XXX. I didn’t have to make this change because the entity_ids could really be whatever, but in my mind it makes it a lot easier to find the sensors and identify them, and reduces the chance of conflicts in naming with sensors from other platforms. This is a “breaking change”, and means if you have any customization, automations, etc. with sensors from the official component you’ll have to update the entity_ids accordingly.

  2. I added in the force_update property. What this means is that if a device is at the same state across two polls it will still update the state and the last_updated time, as well as recording into history (if enabled). I did this because I want to know if the sensors are actually at the same state or if they’re not communicating or whatever. I should probably make this configurable, but just haven’t yet. Let me know if you want the regular behavior and I’ll add the configuration option.

11 Likes

Here’s an example of a Grafana graph I have integrated into my frontend using the data from the devices of this component:

image

And here’s what the detailed state view of a few sensors looks like:

1 Like

Just wanted to say this is really good, i just recently got a Sense box and was looking to expand the official one to support devices now that i got my first one! But then i found yours and it works perfectly! Good job

This is awesome!

Any guide or config you want to share on the grafana setup?

This is perfect, thank you! :slight_smile:

How does everyone find the Sense Energy Monitor, does it pick up all of your devices better now that its been out for a awhile and more people are using it? I’m currently looking for something to monitor all the things that my smart plugs currently can’t. Is this unit still US only?

Thanks

Here’s my setup:
image

In the influxdb section of configuration.yaml I have:

tags_attributes:
    - friendly_name

I’m using iframe cards with lovelace to bring the graphs into the UI

sense.py updated to pull in upstream changes:

In my environment (Hass.io running on an Ubuntu VM) I had to delete the folders “sense_energy” and “sense_energy-0.3.1.dist-info” under /config/deps/lib/python3.6/site-packages for it to properly use the newer sense_energy version.

Is it possible to modify grafana to have a tiered billing output for different times of day?

I’m very experienced with traditional relational databases, but to be honest I’m just learning time series databases myself, so I have no idea how you’d do that with InfluxDB/Grafana.

Can you tell me how much that accesses the internet? I’m looking to do some home energy monitoring, but I have very unreliable internet. Consequently, I try to do as much stuff locally as possible. I want to try a sense, but if it needs internet connectivity all the time it isn’t going to work well for me.

Thank you!

Both my modified component and the official component without device support use the sense_energy Python library, which uses an unofficial API over the internet. As far as I know, even the Sense app communicates over the internet which no local communication.

Thanks for the info! Looks like Sense isn’t going to work for me.

sense.py updated to pull in upstream changes:

FYI, local monitoring can be achieved via an Iotawatt device.

1 Like

I took a look, and I think it looks perfect for my needs. Even does InfluxDB! Thanks for the tip!

I just installed, and I’m getting this error:

File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 223, in async_update_ha_state
yield from self.async_device_update()
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity.py”, line 352, in async_device_update
yield from self.hass.async_add_job(self.update)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/home/homeassistant/.homeassistant/custom_components/sensor/sense.py”, line 204, in update
self._current = round(device[‘c’])
KeyError: ‘c’

The log suggests an issue with what’s returned from the sense_energy package, although I just checked my system and everything seems to be working. Is the Sense app working for you? If so, I would try rebooting HA and see if that’s enough to fix it.

Continuing the discussion from Sense Energy Monitor (Enhanced to include devices):

Sense app is working fine. I changed my config to: devices: false, and it is working fine with your custom sense app.

The electrician just installed the sense unit, and I used your app for the first time not more than an hour after installation. Very possible that since there are no devices found yet, brand new, the code is getting tripped up. Perhaps a test/try statement on the devices until at least one is initialized…