Kamstrup Multical 402 Python Script

Python and python framework

Moderator: leecollings

mphilipp
Posts: 6
Joined: Thursday 29 December 2016 15:58
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Rotterdam, Netherlands
Contact:

Re: Kamstrup Multical 402 Python Script

Post by mphilipp »

Just a quick post to say thanks to the people in this thread, which helped me a lot in reading my Kamstrup 403 meter with Domoticz. I had to figure out a few things, so I thought it would be good to post them here so people who are starting with this have a little bit more info.

The scripts in the kamstrup_402.zip file work perfectly with the 403. No changes necessary.

Hardware i use:
Raspberry Pi 3 with Domoticz
IR read/write head for USB
Kamstrup Multical 403

It's a pain to get the head correctly aligned with the little 'eye' on the meter, but I managed... :roll: The advantage of the 403 meter is that it doesn't need to be 'awake' to read. You can do it anytime. I heard that the 402 had to be woken every time, or keeping it alive by reading every x minutes before it sleeps.

I use two shell scripts to call the kamstrup_single.py script:
  • one to get the actual readings like waterflow, temperatures, power use and execute that every 5 minutes
  • one to get the actual reading AND the overall consumption (ie the meter position), or enery use (GJ) and Hot Water usage in M3. This script is called every 15 minutes
I made this because at first I had just the one script and called that every minute. The Pi can't handle that... :shock: But I still wanted the actual figures for statistical purposes quite often. So I decided on every 5 minutes and the total use every 15 minutes. The Pi is much happier with that. And I can see in more detail how the hot water/heating is used.

What I read in other posts too is that everybody seems to ignore the other data that's available. Most of the data is not very useful, but there is this item 'Infoevent' (item# 113) which is basically an errorcode from the meter or installation. It's supposed to be 0.0, but if it is something else, there's a problem and you should call the energy supplier. I set this up and the moment there's an error, I get a message through Telegram. Obviously this is a very useful addition, because nobody ever walks to the meter to look for an errorcode. Unless there's a problem. But with an alarm on it, you get notified immediately.

[edit]
I noticed that USB devices like the IR read/write head get a better identifier apart from /dev/ttyUSB1 or USB0. It's the serial-by-id:

Code: Select all

ls -l /dev/serial/by-id
total 0
lrwxrwxrwx 1 root root 13 Jan 15 14:18 usb-FTDI_FT230X_Basic_UART_D306YVGM-if00-port0 -> ../../ttyUSB1
lrwxrwxrwx 1 root root 13 Jan 14 22:21 usb-FTDI_FT232R_USB_UART_AL245V2V-if00-port0 -> ../../ttyUSB0
This is a lot better, so you can just use the id and the script will keep working, no matter where you stick the USB device, of when you add something and reboot and they swap USB1/0...

So the lines in the shell script can be like:

Code: Select all

# Infoevent (moet 0 zijn, anders alert) [113] [x]
python3 /home/pi/domoticz/scripts/python/kamstrup_single.py /dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_D306YVGM-if00-port0 113 20
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Christian

First find your IR-reading head. I believe ir-reading-writing to usb any will do, do the cheapest of build one yourself.

The communication starts only when you start sending IR, so no you won't see anything with a handycam now.

No clue what protocol yours is using but am sure you get it to work once you have your ir reading device.

Cheers,

Frank
Martini77
Posts: 23
Joined: Tuesday 28 May 2019 23:08
Target OS: Linux
Domoticz version: 2023.2
Location: NL
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Martini77 »

Hi Frank,

I just received my USB IR head; attached it to my Kamstrup M403 and connected it to my Pi; device is listed via:
ls -l /dev/serial/by-id

Running the kamstrup.py script gives 'None None' as output on all variables;
I do see some light flickering (via phone camera) when running the script and checking the IR head; also a status light on the IR head is flickering indicating some data transfer is taking place (this does not happen when I turn the IR head away from the meter). Als when the IR head is attached to the meter, the 'None None' lines appear pretty quicky whereas they are written with a 2 sec. (timeout) interval when the head is away from the meter....

All together I do think there is some data transmission, but I cannot get any on my terminal output :(

Is there some way to debug this IR head device?

Update: noticed the /tmp/_kamstrup debug file, in there I see a bunch of lines like:
Start
Wr 80 3f 10 01 00 3c b2 5f 0d
Rd 80 3f 10 01 00 3c b2 5f 0d
Wr 80 3f 10 01 00 50 1f 75 0d
Rd 40 01 41 02 04 00 b0 80 80 00 50 5b 38 1e 0d
Msg CRC error
Wr 80 3f 10 01 00 56 7f b3 0d
Rd 80 3f 10 01 00 56 7f b3 0d
Wr 80 3f 10 01 00 57 6f 92 0d
Rd 00 3f 00 00 ac 12 40 21 10 e6 b4 7b 0d
Msg CRC error
Wr 80 3f 10 01 00 59 8e 5c 0d
Rd 80 3f 10 01 00 59 8e 5c 0d
Wr 80 3f 10 01 00 4a ac 0e 0d
Rd 00 3f 00 00 84 10 20 21 00 c7 a0 f1 0d

Rgds,
Martin
Running Domoticz:
- Main @ LXC (Proxmox);
- Secondary @ Pi4B (for backup / test / alerts)
- Synology (Docker) for alerts on main
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Martin

Sent me your email via PM and I get you the Kamstrup windows offical exe ( windows ) to test the IR head.
Hesmink
Posts: 168
Joined: Monday 22 June 2015 10:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The Netherlands
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Hesmink »

Martini77 wrote: Tuesday 09 June 2020 17:39 Hi Frank,

I just received my USB IR head; attached it to my Kamstrup M403 and connected it to my Pi; device is listed via:
ls -l /dev/serial/by-id

Running the kamstrup.py script gives 'None None' as output on all variables;
I do see some light flickering (via phone camera) when running the script and checking the IR head; also a status light on the IR head is flickering indicating some data transfer is taking place (this does not happen when I turn the IR head away from the meter). Als when the IR head is attached to the meter, the 'None None' lines appear pretty quicky whereas they are written with a 2 sec. (timeout) interval when the head is away from the meter....

All together I do think there is some data transmission, but I cannot get any on my terminal output :(

Is there some way to debug this IR head device?

Update: noticed the /tmp/_kamstrup debug file, in there I see a bunch of lines like:
Start
Wr 80 3f 10 01 00 3c b2 5f 0d
Rd 80 3f 10 01 00 3c b2 5f 0d
Wr 80 3f 10 01 00 50 1f 75 0d
Rd 40 01 41 02 04 00 b0 80 80 00 50 5b 38 1e 0d
Msg CRC error
Wr 80 3f 10 01 00 56 7f b3 0d
Rd 80 3f 10 01 00 56 7f b3 0d
Wr 80 3f 10 01 00 57 6f 92 0d
Rd 00 3f 00 00 ac 12 40 21 10 e6 b4 7b 0d
Msg CRC error
Wr 80 3f 10 01 00 59 8e 5c 0d
Rd 80 3f 10 01 00 59 8e 5c 0d
Wr 80 3f 10 01 00 4a ac 0e 0d
Rd 00 3f 00 00 84 10 20 21 00 c7 a0 f1 0d

Rgds,
Martin
Several possibilities, like:

1. Your IR head is not positioned correctly. It has to be on the right spot.
2. You IR head is upside down.
3. The 403 does not have the IR port enabled.
4. The 403 does not talk the kamstrup protocol. Sometimes a simpler protocol is used (see other Kamstrup topics).
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Hesmink wrote: Wednesday 10 June 2020 9:49
Several possibilities, like:

1. Your IR head is not positioned correctly. It has to be on the right spot.
2. You IR head is upside down.
3. The 403 does not have the IR port enabled.
4. The 403 does not talk the kamstrup protocol. Sometimes a simpler protocol is used (see other Kamstrup topics).
We have some progress.

With the Windhoos Kamstrup tool he can see the required fields with actual data on his laptop. :-)
Martini77
Posts: 23
Joined: Tuesday 28 May 2019 23:08
Target OS: Linux
Domoticz version: 2023.2
Location: NL
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Martini77 »

And some more progress..! :D

Turns out that the position of the IR head is more sensitive using our well known script compared to the Windows tools Kamstrup provides (https://service.kamstrup.com/en-en/metertool).

When I tilt the IR head just a bit (up until 1 cm away from the meter), the script does work on my 403 and all data is fetched. Kind of strange tho as I'm not using a self made IR head, but the one from Weidmann Elektronik (http://weidmann-elektronik.de/Produkt_IR-Kopf.html).

I have two more additions;
1. Only update when correct values are read; to avoid Domoticz is being updated on counters with value 'None'.
This requires a small change to the kamstrup_single.py script. Just before the Domoticz update takes place; add a check on the fetched value (and do not update Domoticz when incorrect; 'None'):

Code: Select all

    if str(value) != 'None':
        print("Correct data")
        print(str(value))
        requestPost = urllib.request.urlopen( "http://192.168.2.113:7080/json.htm?type=command&param=udevice&idx=" + str(index) + "&svalue=" + str(value) )

    if str(value) == 'None':
        print("Incorrect/wrong data")
2. if you want to schedule the script to run every X minutes or so there was already some suggestion to use Crontab that triggers a shell script calling the kamstrup_single.py script I don't like that one as Domoticz does not necessarily have to run + you cannot easily switch it on/off without terminal access to your Pi. Therefor I have setup a simple DZVents script that triggers the .sh (note that you need to have DZVents enabled in Settings>Other of your Domoticz instance):

Code: Select all

return {
	on = {
		timer = {
			'every 5 minutes',
		}
	},
	execute = function(domoticz)

       if (domoticz.time.minutes == 0 or
           domoticz.time.minutes == 15 or 
           domoticz.time.minutes == 30 or 
           domoticz.time.minutes == 45 ) then
          -- Every 15 minutes, run full update
          local command = '/home/pi/domoticz/scripts/kamstrup/heat_full.sh'
          os.execute(command)

       else
          -- every other 5 minutes, run small update
          local command = '/home/pi/domoticz/scripts/kamstrup/heat_selected.sh'
          os.execute(command)
       end
	end
}
Running Domoticz:
- Main @ LXC (Proxmox);
- Secondary @ Pi4B (for backup / test / alerts)
- Synology (Docker) for alerts on main
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Martin

THanks for the enhancements showed and good to read its working now.

1 little comment....

can you change :

local command = '/home/pi/domoticz/scripts/kamstrup/heat_full.sh'

into :

local command = '/home/pi/domoticz/scripts/kamstrup/heat_full.sh &'

In this way the script is executed in the background and Domoticz scripts are continued to be processed. If not the
complete script executions is waiting for your 1200 baud script to complete and come back before the next script will be executed.

Cheers,

Frank
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Anybody got the Kamstrup 403 working ?
Martini77
Posts: 23
Joined: Tuesday 28 May 2019 23:08
Target OS: Linux
Domoticz version: 2023.2
Location: NL
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Martini77 »

Jup... you should know that 😜
Running Domoticz:
- Main @ LXC (Proxmox);
- Secondary @ Pi4B (for backup / test / alerts)
- Synology (Docker) for alerts on main
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Martini77 wrote: Friday 30 October 2020 21:54 Jup... you should know that 😜
Diskspace is limited when you get older. Completely fotgot Martin. :-)

Got the 402 script working on my 403.
Now Quick and dirty.
If more people are interested I can brush-up the script and make it look nice as well.
Even working on a 3D printed holder for the sensor as the old sensor works but the position is much different and does not work without a lot of
sticky tape :-)

Cheers,
Frank
SunnyBoy
Posts: 3
Joined: Friday 11 May 2018 19:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Kamstrup Multical 402 Python Script

Post by SunnyBoy »

I also got the Kamstrup Multical 403 working via USB (on an Intel NUC, should work on any machine), I documented my findings here. The TL;DR:
  • Get head from using the hal9k.dk reader head (archive) then solder together (22 EUR including shipping in EU, a bargain :) Best to pay with PayPal because banks apparently charge a ridiculous amount to transfer money across currencies.)
  • Connect to an FTDI USB TTL reader (e.g. at tinytronics)
  • Use either use this python script to read out the sensor and push to domoticz, or use my fork to push to InfluxDB or MQTT. Note that the original script seems to use more battery than required (see issue on GitHub repo)
Useful background info on the Kamstrump Multical meter sensor here (archive) and here (archive).
jwmaas
Posts: 3
Joined: Saturday 19 December 2020 0:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Kamstrup Multical 403 Python Script

Post by jwmaas »

I have a Multical 403 meter. The provided scripts return "none, none" as output for the variables requested. Clearly something is wrong.

Martini77, perhaps you can send a picture of how you positioned the IR head on the meter? You mention 1 cm distance from the lcd screen, wonder how you did that. Can you post a picture? Is somebody using mqtt to send the data to a home automation system?
Martini77
Posts: 23
Joined: Tuesday 28 May 2019 23:08
Target OS: Linux
Domoticz version: 2023.2
Location: NL
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Martini77 »

In my case it turned out the IR light was just too powerful.. Putting a piece of paper in between fixed it... :lol:

Alternatively run the script reading all variables and at the same time move/hover the IR head in front of the ‘eye’ also tilting it a bit. Once positioned correctly, the data should appear with approx. 2 values/measurements per second.

Oh and I use http calls to feed the data to Domoticz..
Running Domoticz:
- Main @ LXC (Proxmox);
- Secondary @ Pi4B (for backup / test / alerts)
- Synology (Docker) for alerts on main
jwmaas
Posts: 3
Joined: Saturday 19 December 2020 0:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Kamstrup Multical 403 Python Script

Post by jwmaas »

I got the Multical 403 working finally. The main issue is the position of the reading head for the M403. Using the Kamstrup windows meter reading program I found out that the reader needs to be upside down compared to M402, wire going up, not down. Second positional thing is that it cannot sit on the two plastic protruberances on the meter's glass. I moved it slightly lower and also about 3 mm away from the glass.
Martini77 could be right, that the signal is just too strong and needs to be tempered. But I fixed it in position now, and hopefully will never need to move it again, because getting it in position is quite a job, sending read commands while shifting position all the time. And then not moving it, to put the sticky tape on! A challenge but doable if you are patient.

I used tvwerkhoven's generalized pymultical.py script which works well except for the MQTT bit, which I rewrote and now functions well. I capture the data in Home Assistant.
If anyone is interested in my script for the M403 with working MQTT, please drop me a line.

I have also added a crontab on my raspberry pi to ensure the meter is read every five minutes. The reading is very fast, so this frequency is no issue at all.
Martini77
Posts: 23
Joined: Tuesday 28 May 2019 23:08
Target OS: Linux
Domoticz version: 2023.2
Location: NL
Contact:

Re: Kamstrup Multical 402 Python Script

Post by Martini77 »

Have you tried to put a small piece of paper between the IR head and meter? That worked perfectly for me...
Running Domoticz:
- Main @ LXC (Proxmox);
- Secondary @ Pi4B (for backup / test / alerts)
- Synology (Docker) for alerts on main
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Guys,

I have my IR sensor attached to the Kamstrup with this 3D printed holder.
Let me know if you are interested in the .stl
Capture.JPG
Capture.JPG (32.35 KiB) Viewed 3825 times
jwmaas
Posts: 3
Joined: Saturday 19 December 2020 0:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Kamstrup Multical 403 Python Script

Post by jwmaas »

The meter reader came by and destroyed the head position on the M403 meter we have. Putting in a piece of paper between the read/write head and the meter's glass works like a charm. Apparently the signals are quite strong. With this done, the reading head with wire up, can be positioned on the two plastic protuberances on the meter's glass. Excellent suggestions in this forum.
jeroendew
Posts: 1
Joined: Monday 22 March 2021 21:02
Target OS: -
Domoticz version:
Contact:

Re: Kamstrup Multical 402 Python Script

Post by jeroendew »

freijn wrote: Tuesday 09 June 2020 22:22 Martin

Sent me your email via PM and I get you the Kamstrup windows offical exe ( windows ) to test the IR head.
Hi Frein,

Do you still have the software? I'm want to see if my IR-head is working

Best regards,
Jeroen
freijn wrote: Monday 02 November 2020 10:50
Martini77 wrote: Friday 30 October 2020 21:54 Jup... you should know that 😜
Diskspace is limited when you get older. Completely fotgot Martin. :-)

Got the 402 script working on my 403.
Now Quick and dirty.
If more people are interested I can brush-up the script and make it look nice as well.
Even working on a 3D printed holder for the sensor as the old sensor works but the position is much different and does not work without a lot of
sticky tape :-)

Cheers,
Frank

Hi Frank,

I'm interested in your script, could you sent it to me? btw, i'm using windows with python, could that work?

Best regards,
Jeroen
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Kamstrup Multical 402 Python Script

Post by freijn »

Hi Jeroen,

Please sent me your email by PM and I wetransfer the files to you.

For sure the script will work on windows, The "only" thing you need to take care of is the serial port name.

Linux is /dev/ttyxxx Windows would be com4 or so. I guess there are 100 examples on how to do that.

Cheers,

Frank
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest