Get your smart electric, water and gas meter scm readings into home assistant with a RTL-SDR

so I got it working. I had to change a few things:

The code for “amrscm2mqtt” had a few syntax errors, please see below the corrected code. I also renamed it to “amrscm2mqtt.py”

#!/usr/bin/env python3
'''
Runs rtlamr to watch for IDM broadcasts from power meter. If meter id
is in the list, usage is sent to 'readings/{meter id}/meter_reading'
topic on the MQTT broker specified in settings.

WATCHED_METERS = A Python list indicating those meter IDs to record and post.
MQTT_HOST = String containing the MQTT server address.
MQTT_PORT = An int containing the port the MQTT server is active on.

'''
import subprocess
import signal
import sys
import time
import paho.mqtt.publish as publish
import settings

# uses signal to shutdown and hard kill opened processes and self
def shutdown(signum, frame):
    subprocess.call('/usr/bin/pkill -9 rtlamr', shell=True)
    subprocess.call('/usr/bin/pkill -9 rtl_tcp', shell=True)
    subprocess.call('/usr/bin/pkill -9 amridm2mqtt', shell=True)
    sys.exit(0)

signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)

auth = None

if len(settings.MQTT_USER) and len(settings.MQTT_PASSWORD):
        auth = {'username':settings.MQTT_USER, 'password':settings.MQTT_PASSWORD}

def send_mqtt(topic, payload,):
    try:
        publish.single(topic, payload=payload, qos=1, hostname=settings.MQTT_HOST, port=settings.MQTT_PORT, auth=auth)
    except Exception as ex:
        print("MQTT Publish Failed: " + str(ex))

time.sleep(30)

# start the rtl_tcp program
rtltcp = subprocess.Popen([settings.RTL_TCP + " > /dev/null 2>&1 &"], shell=True,
    stdin=None, stdout=None, stderr=None, close_fds=True)

time.sleep(5)

# start the rtlamr program.
rtlamr = subprocess.Popen([settings.RTLAMR,
    '-msgtype=scm',
    '-format=csv'], stdout=subprocess.PIPE)

while True:

    try:
        # rtlamr's readline returns byte list, remove whitespace and convert to string
        amrline = rtlamr.stdout.readline().strip().decode()
        # split string on commas
        flds = amrline.split(',')

        if len(flds) != 9:

            # proper SCM results have 9 fields
            continue

        # make sure the meter id is one we want
        meter_id = int(flds[3])
        if len(settings.WATCHED_METERS) and meter_id not in settings.WATCHED_METERS:
            continue

        # get some required info: current meter reading, current interval id, most recent interval $
        read_cur = int(flds[7])

        current_reading_in_kwh = (read_cur * settings.WH_MULTIPLIER) / 1000

        send_mqtt(
            'readings/' + str(meter_id) + '/meter_reading',
            '%s' % (current_reading_in_kwh)
        )

    except:
        time.sleep(2)

Next, I made some changes to the “amridm2mqtt.service” file. For some reason, my system did not like the “User=root” designation, so I deleted it. Also, I needed to explicitly call python3.

[Unit]
Description=AMR IDM to MQTT
After=network.target

[Service]
ExecStart=/usr/bin/python3 /opt/amridm2mqtt/amrscm2mqtt.py
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=amridm2mqtt

[Install]
WantedBy=multi-user.target

The service now auto starts and pushes MQTT messages to my broker.

I am by no means a python programmer; my strengths are in industrial automation. Therefore I have no idea if what I did was good or not… but it seems to work, so I will let it run for the next few days and see how it goes.

Thanks @biochemguy for the help

2 Likes

The data sent by the meter will be in it’s specific format. If your meter reads in therms, then it should theoretically send therms data to mqtt. You can always convert it in home assistant afterwards. As for gas / electric / water, it depends on your meter. You can see if your meter is supported in this list

Hi @david1, The Rainforest automation bridge is only for the power meters. My utility provider (EPCOR) will apparently pair the device to my power meter. I read someplace that PG&E can also use the Rainforest Automation device as well, but I cant remember for sure. I have not pulled the trigger yet because the bridge was reviewed quite poorly by a number of people; they cited security issues and constant rebooting. I am waiting until some better firmware is released

Glad you could make it work. I am by no means a programmer and learning too. Thanks for posting your findings to help others.

no problem! open source software would be nothing without a community that shares/helps

it can but im mainly interested in gas right now…, according to their website their data isn’t encrypted from meter until it reaches their wireless access points, so using an rtl-sdr should work

hi @david1, take a look at your meter. The model number and FCC ID can be compared to the list of compatible meters on rtlamr github page.

1 Like

Hi All,
Wanted to share my experience in case someone else comes across something similar.
I previously checked to see if my Itron gas meter was compatible
and it is indeed in the list. Further it is a ERT Type 2 SCM meter (bottom left corner of the label tells you which type).

However I never see mine show up, nor do I see any Type 2 meters show up but do see lots of type 7 and type 12 meters showing up from the neighborhood.
What I’ve concluded is that although my particular Itron type 2 is capable of operating in “wake up” and “bubble-up” modes mine is apparently operating in “wake up” mode which means it will only report out once a month whenever the gas company drives by to wake it up.

1 Like

Thank you, this got it working for me as well!

Having an issue translating my sensor info into the template provided, Does anybody have a moment to answer a few questions?

-Edit

Nevermind I figured it out, this works so much more smoothly than the last time i did this and fed the data straight to influxdb and then read the values with home assistant from influxdb.

where do i put in my meter id? if it is 1481976504

i am getting mine setup at the moment, are you sure that is your id?

{Time:2019-03-07T02:25:49.843 SCM:{ID:31725098 Type: 7 Tamper:{Phy:01 Enc:01} Consumption: 8188273 CRC:0xFEC4}}

is the output i get from running a non defined scan, the id for my meter is 31725098 which is 8 characters similar to every one of the other meters i found, and also the examples given above. I got this by running rtl_tcp in one terminal window, and then running rtlamr in a second window it will give you readouts of what it can see for your entire neighborhood

If that is your meter id, or you find the correct one, those get added to the /opt/amridm2mqtt/settings.py file very near the top. the file is well annotated on where to add them.

yes, its my water meter, could not get my gas or electric working, {Time:2019-03-06T20:09:45.165 R900:{ID:1481976504 Unkn1:0xA1 NoUse: 0 BackFlow:0 Consumption: 1415493 Unkn3:0x00 Leak:15 LeakNow:3}}

are they not compatible or were you having a different issue? Did you find the settings file to add the id?

what are you talking about? its a R900 meter, i am getting readings… your not being much help

I was talking about your gas and electric meters that are not working.

Have you edited the file /opt/amridm2mqtt/settings.py to include The id(s) for the meter(s) you would like it to read?

I’d like to help, i just got this setup and its fresh in my mind. But i don’t know where you are stuck at the moment.

Thanks for this write up.

I have a question.
How to you determine the meter ID? Is it printed on the meter?

If your question is where to place your meter info to send it to home assistant is with this command:

sudo nano /opt/amridm2mqtt/settings.py

In WATCHED_METERS = [insert meter data here]
You can add your mqtt configuration in this file for homeassistant too.

Yes, your meter ID will be printed on your meter. It’s with this ID that your data is being transmitted. @adamg had shared an example some days ago, see here.

Got it. Thank you.

Apparently my meter took a bit longer to pick up. My first scan of about 4-5 mins did not pickup a matching number to any on the meter. Similar but not exact.
It took about 10+ mins before I found a matching number.

1 Like