Energomonitor

Hey, does anyone use https://www.energomonitor.com to monitor power consumption. Curious to know if its possible to intergrate into home assistant.

I installed this quite a while back. Would be interested to know what energy monitor I could replace it with to get energy use data into Home Assistant in Australia.

Thanks.

Iā€™d be interested in this too. I have two setups running in two houses and the data out is really stable and informative. An ā€œEnergomonitorā€ Integration would be great to have if there were such a thing.

Thanks

I have HA grabbing data from the energomonitor API. Itā€™s not an official integration but has been running quite successfully for a few years. I grab the main value, and also some from the individual appliance monitors. Only pain point is having to generate a new API token every 30 days.

Youā€™ll need to generate a API token on the https://app.energomonitor.cz site (link in footer of page), and use the API Demo to find the feed_id and stream_id (e.g. Mains W, Mains Wh, or appliance1 W, etc) of the channels you want data for.

Put your API token in secrets.yaml:

energomonitor_api_key: 'Bearer [your_api_key]'

The ā€˜Bearerā€™ part tripped me up for while, donā€™t forget to include it. You should end up with something like energomonitor_api_key: 'Bearer CfsfkxXbRVeZuYjP7TutKo9brmc0eY9'

and in your sensors:

sensor
  - platform: rest
    name: power_mains
    resource: https://api.energomonitor.com/v1/feeds/[feed_id]/streams/[stream_id_for_mains_W]/data?limit=1
    headers: { 'Authorization': !secret energomonitor_api_key }
    value_template: '{{ value_json[0][1] }}'
    unit_of_measurement: W

  - platform: rest
    name: power_washing_machine_iam
    resource: https://api.energomonitor.com/v1/feeds/[feed_id]/streams/[stream_id_for_washingmachine_W]/data?limit=1
    headers: { 'Authorization': !secret energomonitor_api_key }
    value_template: '{{ value_json[0][1] }}'
    unit_of_measurement: W
    force_update: true

?limit=1 grabs the most recent value.
value_json[0][1] grabs just the value. Iā€™m ignoring the timestamp from the response.

I ended up using the washing machine power to send notifications when the cycle was done. :sunglasses:

1 Like

I got it working and I can see the graph on settingsā€“Devices&Sensorsā€“Entities

image

But how can I use that to track consumption
Sensor is not in the drop down menu.

image

Got it workingā€¦Needed to create a Helper with Grid power input.

Soā€¦Energmonitor API pulls the date to sensor.power_input

configuration.yaml:

Thenā€¦create a new Helper

image

image

This can be an input to Energy tab

Thanks @marpelto - thatā€™s the bit I didnā€™t have. Iā€™ve now added an entry to my sensor.yaml


- platform: integration
  source: sensor.power_mains
  name: energy_spent_mains
  unit_prefix: k
  round: 2

then added that to the Energy Dashboard as you outlined. Itā€™s been running nicely overnight. 8)

1 Like

To overcome the 30 days limitation, I created a script that renews the API token via CURL message. It parses the answer and writes the new token directly to the secrets.yaml file as per syntax.

You may put this sctipt to cron to run every 30days.

#bash
echo -n ā€œenergomonitor_api_key: ā€˜Bearer " > /config/secrets.yaml
echo -n $(curl --user ā€œusername:passwordā€ -X POST ā€œhttps://api.energomonitor.com/v1/authorizationsā€ |grep -o ā€˜ā€œtokenā€: ā€œ[^ā€]ā€™ | grep -o '[^"]$ā€™) >> /config/secrets.yaml
echo "ā€™ā€ >> /config/secrets.yaml
ha host restart

Bolded in one row

Thanks marpelto

I hadnā€™t thought about anything like that. Iā€™ve tweaked your bash script a bit to work for me.

the single > would have overwritten the whole secrets file. And the grep commands needed an *, without the * I was only getting a single character.

Iā€™ve changed my set up to write the energomonitor api key to itā€™s own file: /config/energomonitor_api_key.yaml and updated my secrets.yaml to include it:

#secrets.yaml
energomonitor_api_key: !include energomonitor_api_key.yaml
energomonitor_credentials: "<user>:<pass>"

It also stores the energomonitor credentials for requesting a new key

I created an automation to run every 28 days:

#automations.yaml
- id: 'automation.energomonitor_refresh'
  alias: 'Energomonitor refresh api key'
  trigger:
    - platform: time
      at: '5:30:00'
  condition: "{{ (now() - as_datetime('19700101T00Z')).days % 28 == 0 }}"
  action:
    service: shell_command.refresh_energomonitor_api_key
    data_template:
      energomonitor_credentials: !secret energomonitor_credentials

that calls a shell_command:

#configuration.yaml
shell_command:
  refresh_energomonitor_api_key: "bash /config/shell/refresh_energomonitor_api_key.sh {{ energomonitor_credentials }}"

which calls the bash script and passes in my credentials

#/bin/bash

TEMP_KEY=`curl --user "$1" -X POST "https://api.energomonitor.com/v1/authorizations" | grep -o '"token": "[^"]*' | grep -o '[^"]*$'`
KEY_SIZE=${#TEMP_KEY}

if [ $KEY_SIZE -gt 24 ]; then
	echo -n "Bearer $TEMP_KEY" > /config/energomonitor_api_key.yaml
	ha host restart
fi

I scheduled it a few times today and it seems to be working well.

Thanks for the pointer.

I tested the script and it worked fine with me. So I started wondering, how itā€™s possibe.
Apparently, when I copied the line here it removed those ā€œ*ā€ as they are used in editing.
This picture shows the how this is seen on ā€œeditā€ more and on the right side as ā€œseen on webā€ mode

Sorry about thatā€¦didā€™n check the actual publication. Totally missed that. But something to remember.
Although, your sample was okā€¦strange.

OKā€¦got it. There is an apostrophe ā€œ`ā€ on your line, and not on mine.

without apostrophe
-X POST ā€œhttps://api.energomonitor.com/v1/authorizationsā€ |grep -o ā€˜ā€œtokenā€: ā€œ[^ā€]ā€™ | grep -o '[^"]$ā€™) >> /config/secrets.yaml

With apostrophe
-X POST "https://api.energomonitor.com/v1/authorizations" |grep -o '"token": "[^"]*' | grep -o '[^"]*$') >> /config/secrets.yaml

Thanksā€¦I was planning to do a cron for that, never would think of that way doing it.

It looks like you are using curly quotes (or smart quotes) rather than straight quotes.

From much searching the forum and googling, it seems that you canā€™t use cron with hassos (cron entries disappear after a reboot) and all the suggestions were to use an automation instead.

I didnā€™t put much effort on checking the cron yet as I run the script manually once to verify that it works. But thanks saving my time. :+1:

How can I test this say every 15min? I will try this:

   trigger:
     platform: time
     minutes: '/15'

Youā€™d have to use time_pattern not time:

  trigger:
    platform: time_pattern
    minutes: '/15'

and also the condition restricts it to only run on every 28th day (based on the unix epoch) so youā€™ll want to disable that while testing your 15 minute run. Today (Jan 18th 2023) is a 27 not a 0 according to {{ (now() - as_datetime('19700101T00Z')).days % 28 }} so it wouldnā€™t have run today.

Thanks @wade I tested and all works nicely :slight_smile:

Although, first I wonderend why the key was saved as ā€œenergomonitor_api_key.yaml?ā€ with a ā€œ?ā€ at the and of file.The script did add some special, invisible characters also. And because of those the file save did not work.

I realised that there is faint markings at the end of the line. Seen only on HA File-editor

egā€¦

image

Howeverā€¦it suppose to be this

image

I used nano and the File Editor in HA. But I canā€™t repeat the issue. So I donā€™t know how I did this?

Sorry, you are on your own here. Iā€™m guessing itā€™s a platform/line endings thing. Are you on Windows?

I do all my editing via SFTP and my normal text editor (Transmit for SFTP, BBEdit for editing) so all my files are set to UTF-8, Unix (LF) line endings.

Yeah, Iā€™m on Windows.
All works now OK. Iā€™m just curiousā€¦

Quite old topic, but yet still relevant for me. Iā€™m a new in HA (third day since having HA green) and trying to get Energomonitor work with me. Just to make sure, does these still work for you @marpelto and @wade?