home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.4k stars 30.65k forks source link

Error integrating with EmonCMS. #123726

Open paulhollands opened 2 months ago

paulhollands commented 2 months ago

The problem

Hi,

I'm getting the following error within the HA logs when trying to integrate with Emon CMS. Anyone have any suggestions?

Error while setting up emoncms platform for sensor Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 366, in _async_setup_platform await asyncio.shield(awaitable) File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/emoncms/sensor.py", line 126, in setup_platform EmonCmsSensor( File "/usr/src/homeassistant/homeassistant/components/emoncms/sensor.py", line 195, in init elif elem["value"] is not None:


KeyError: 'value'

My configuration.yaml is setup as follows; 

sensor:
  - platform: emoncms
    api_key: <api-key-removed>
    url: https://emoncms.org
    id: 5

### What version of Home Assistant Core has the issue?

2024.6.3

### What was the last working version of Home Assistant Core?

_No response_

### What type of installation are you running?

Home Assistant OS

### Integration causing the issue

Emon CMS

### Link to integration documentation on our website

https://www.home-assistant.io/integrations/emoncms

### Diagnostics information

_No response_

### Example YAML snippet

```yaml
sensor:
  - platform: emoncms
    api_key: <api-key-removed>
    url: https://emoncms.org
    id: 5
```

### Anything in the logs that might be useful for us?

_No response_

### Additional information

_No response_
home-assistant[bot] commented 2 months ago

Hey there @borpin, @alexandrecuer, mind taking a look at this issue as it has been labeled with an integration (emoncms) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `emoncms` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign emoncms` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


emoncms documentation emoncms source (message by IssueLinks)

alexandrecuer commented 2 months ago

I guess you want only the feed number 5 ?

If yes, could you please try with the include_only_feed_id parameter in your yaml conf ?

For example :

sensor:
  - platform: emoncms
    api_key: <api-key-removed>
    url: https://emoncms.org
    id: 1
    include_only_feed_id:
      - 5
paulhollands commented 2 months ago

I've just tried a few things

sensor:

This resulted in the following error

Invalid config for 'sensor' from integration 'emoncms' at configuration.yaml, line 15: required key 'id' not provided, please check the docs at https://www.home-assistant.io/integrations/emoncms

I then tried the following which results in NO errors, but I still don't get a emonCMS entity. I did a reboot on this change too.

sensor:

alexandrecuer commented 2 months ago

Sorry, the id key is indeed mandatory to identify the emoncms instance, should be 1 in your case (only one emoncms instance which is emoncms.org) but i dont see why you dont get any entity in home assistant...

paulhollands commented 2 months ago

I've changed the ID to 1 and restarted, but still no entity

Below is the top portion of my configuration file if there is something above which is causing an error? I also have an old instance of HA running on another server which I'm currently migrating away from. This is still connecting to Emon and I wasn't going to disable it until I could get the new instance working. I'd assumed that since its using an API, that both HA instances could pull the data? Or at the least there would be a conflict/rejection error message on the new HA instance?

Loads default set of integrations. Do not remove.

default_config:

Load frontend themes from the themes folder

frontend: themes: !include_dir_merge_named themes extra_module_url:

automation: !include automations.yaml script: !include scripts.yaml scene: !include scenes.yaml

sensor:

recorder: db_url: mysql:// purge_keep_days: 365

alexandrecuer commented 2 months ago

So it means that your feed id is not 5. As you are on emoncms.org, it should be something higher, because there are a lot of feeds on emoncms.org....

To catch your feed id, just interrogate the emoncms API : https://emoncms.org/feed/list.json?apikey=your_api_key

You should see something like that : image

here the feed id is 11 but I am using a local instance of emoncms

or you can get the id from the feed page :

image

paulhollands commented 2 months ago

I get this within a browser. Note sure if the order is important and I tried changing the ID number in the configuration file to 457137, but still no emoncms entity

FYI: my energy value is currently negative since I'm exporting electricity.

[{"id":"457137","userid":"44421","name":"1","tag":"5","public":"","size":"28617712","engine":"5","unit":"W","server":"0","value":"-2853","time":"1723553382"}]

alexandrecuer commented 2 months ago

So, your yaml should be like that :

sensor:
  - platform: emoncms
    api_key: <api-key-removed>
    url: https://emoncms.org
    id: 1
    include_only_feed_id:
      - 457137

Is this the case ?

paulhollands commented 2 months ago

I've now changed it to exactly as you've show and it all works ok!

Strange since in my earlier version of HA (2023.5.4), I didn't need to specify the include_only option and clearly ID just refers to the internal HA ID for the sensor rather than the EmonCMS feed ID? Also maybe there could be a future enhancement to include a HA error message somewhere stating "feed not found"?

Many thanks for your help!

alexandrecuer commented 2 months ago

it should have worked without the include_only_feed but as i don't know your setup, I use this as a starting point. It is more easy to deal with one feed than with 100 :-)

The enhancement is to go to config flow so that the integration can be configured through the home assistant UI : once the emoncms server is added, the user can configure it through a feed list and manually chooses which feeds to add...

Many people are confused and think that the id parameter in the yaml is the feed id, once the integration will be migrated to config flow, this will not be anymore a problem I think....