[SOLVED] Logging DSMR sensor?

I am trying to configure the DSMR sensor (but so far have had no luck yet).

In trying to diagnose this I tried to see why it did not connect, but have a hard time setting up the logger correctly to have it log.

Does anybody now how to make the DSMR integration appear in the logs? So far I have the following in configuation.yaml but all to no avail:

...
# https://www.home-assistant.io/integrations/logger/
logger:
  default: error
  logs:
    homeassistant.components.dsmr_parser: debug
    homeassistant.components.dsmr.sensor: debug
    homeassistant.components.sensor.dsmr_parser: debug
    homeassistant.sensor.dsmr_parser: debug
    homeassistant.sensors.dsmr_parser: debug
...

This is how I setup my DSMR integration. I have created a directory sensors and in there is the following file called dsmr.yaml:

---
# https://www.home-assistant.io/integrations/dsmr/

platform: dsmr
host: !secret dsmr_p1_host
port: !secret dsmr_p1_port

The sensors folder is included from confiuration.yaml like this:

sensor: !include_dir_merge_list sensors

There is nothing in the logs at all?

Nothing related to dsmr, only a custom component and HACS repo warning:

2020-10-18 10:50:55 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for google_geocode which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.
2020-10-18 10:51:04 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.

Which does not seem that strange to me as the only reference to a line logging in the sensor I see is when handling IOError or SerialException exceptions at line 184 in sensor.py.

Mine is run over a tcp connection, not sure which exceptions might be thrown in case of errors on this connection.

Could you check in .storage folder the core.config_entries and check if there is one for dsmr? There should be at least an entry which contains a serial id for the meter.

Hmmm, grep dsmr /config/.storage/core.config_entries does not yield any results. Something wrong with my configuration?

Which version of home assistant are you using?

This is the output of ha core info:

arch: armv7
audio_input: null
audio_output: null
boot: true
image: homeassistant/raspberrypi3-homeassistant
ip_address: 172.30.32.1
last_version: 0.116.4
machine: raspberrypi3
port: 8123
ssl: false
version: 0.116.4
version_latest: 0.116.4
wait_boot: 600
watchdog: true

Could you try to put everything in configuration.yaml?

Since 0.115 there is a connection validation when importing yaml. When this is unsuccessful, there will be no config entry. Strange thing is that it would log an exception when this happens, so this should be visible in the logs (unless it doesn’t validate the config)

Configuration is valid, other sensors in /config/sensors/ are included in /config/storage/,config_entries. Will try to put the dsmr configuration stanza diretcly in configuration.yaml, but do not expect much from it.

My suspicion is that there is an uncaught exception somewhere when trying to connect the create_tcp_dsmr_reader factory, see my earlier statement:

Even after adding the following stanza into configutation.yaml:

sensor:
  - platform: dsmr
    host: !secret dsmr_p1_host
    port: !secret dsmr_p1_port

instead of

sensor: !include_dir_merge_list sensors

followed by a ha core restart I still do not see dsmr listed when doing: grep dsmr /config/.storage/core.config_entries.

If you don’t have an entry in core.config_entries, you won’t reach code from sensor.py. It will get stuck somewhere in config_flow.py.

You could copy the integration sources to custom_components and try to add logging in the import function to see if it reaches that point or not.

How am I to do that? Check out the contents of the dsmr folder into /config/custom_components/dsmr/ and edit from there?

Yes.

Add a log line in

async def async_step_import(self, import_config=None)

To see if it reaches this point.

You can set log level for the integration:

logger:
  logs:
    homeassistant.components.dsmr: debug

Would you be able to do that? Otherwise I can prepare a config_flow.py with some extra logging later today.

What is your dsmr version? I don’t see you set it explicitly. By default it would set 2.2. The version number should be visible on your meter.

I think I can do this since I am proficient enough in Python.

I am currently not connected to my DSMR meter, but since I had issues I have created a simple daemon sending out telegrams for testing, when that works I will hook up the electronics to the DSMR again.

My DSMR does run with version 2.2 on it’s P1 port, so the default value should be fine.

Ok :slight_smile:

You could also try on line 157 to catch the general exception to see if there’s an uncaught exception somewhere.

At least this seems to have some effect, when my telegram generator is not running I see at least connection errors like this:

2020-10-18 15:40:09 ERROR (MainThread) [homeassistant.components.dsmr.config_flow] Error connecting to DSMR
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/dsmr/config_flow.py", line 74, in validate_connect
    transport, protocol = await asyncio.create_task(reader_factory())
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 494, in sock_connect
    return await fut
  File "/usr/local/lib/python3.8/asyncio/selector_events.py", line 526, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
ConnectionRefusedError: [Errno 111] Connect call failed ('192.168.178.42', 6638)

It seems to not use the custom_component however, judging from the path in the error message. Any clue on how to force the custom_component to have preference over the one in /usr/src/homeassistant/?
I would need this to do some further issue tracking as there seems to go something wrong after sending a few telegrams from my generator.

Apart from that this indeed only seems to work when I configure this in /config/configuration.yaml, if I use include statements with a file in sensors/ folder I see no log entry at all. :frowning:

Putting something in custom_components should override the built-in integration. You should get a warning as well that you are overriding the default one. Where exactly did you put it?

Maybe a bit obvious, but are you sure the daemon is working correctly? By putting it inconfiguration.yaml and using real hardware it still doesn’t work?

Yes, a simple putty session to IP and port receives data.

It does during the ha core restart the deamon sends 8 telegrams (one every 5 seconds) as soon as that is done somehow the connection seems to be dropped.

Since I do not seem to get the custom component running I am at a dead end with debugging.

I checked out the source from github of home-assistent core, made my changes in the components/dsmr folder in the checkout and created a symbolic link from /config/custom_components to the components dsmr folder like so:

ls -la /config/custom_components/dsmr
lrwxrwxrwx    1 root     root            40 Oct 18 12:27 /config/custom_components/dsmr -> /root/core/homeassistant/components/dsmr

The symlinked folder looks like this:

ls -la /config/custom_components/dsmr/
total 80
drwxr-xr-x    3 root     root          4096 Oct 18 12:25 .
drwxr-xr-x  966 root     root         32768 Oct 18 12:24 ..
-rw-r--r--    1 root     root          1270 Oct 18 12:24 __init__.py
-rw-r--r--    1 root     root          6483 Oct 18 15:37 config_flow.py
-rw-r--r--    1 root     root           510 Oct 18 12:24 const.py
lrwxrwxrwx    1 root     root            30 Oct 18 12:25 dsmr -> /config/custom_components/dsmr
-rw-r--r--    1 root     root           222 Oct 18 12:24 manifest.json
-rw-r--r--    1 root     root         12563 Oct 18 12:24 sensor.py
-rw-r--r--    1 root     root           168 Oct 18 12:24 strings.json
drwxr-xr-x    2 root     root          4096 Oct 18 12:24 translations