Code Monkey home page Code Monkey logo

amridm2mqtt's Introduction

AMRIDM2MQTT: Send AMR/ERT Power Meter Data Over MQTT

Copyright (c) 2018 Ben Johnson. Distributed under MIT License.

Using an inexpensive rtl-sdr dongle, it's possible to listen for signals from ERT compatible smart meters using rtlamr. This script runs as a daemon, launches rtl_tcp and rtlamr, and parses the output from rtlamr. If this matches your meter, it will push the data into MQTT for consumption by Home Assistant, OpenHAB, or custom scripts.

TODO: Video for Home Assistant

Changes from Original Project

This repository fork has been altered to send the full JSON payload for both SCM and IDM messages via MQTT instead of sending an array of values. See below for a sample setup for Home Assistant.

  • Messages are posted with ‘retain’ flag enabled so that the meter information is immediately available to HASS on restart
  • Messages are published to topics in more unique namespaces for easier filtering
  • Messages are published to an availability topic so that HASS can update accordingly when the service is up or down
  • A last_reset hack value is posted to workaround the issues with MQTT sensor and last_reset, this hack is only needed for Home Assistant 2021.8, but it is retained for backwards compatibility.

Example Topic: amr/reading/SCM/2/32109876/message

Example MQTT payload:

{
  "Time" : "2021-09-01T09:23:49.559057273-06:00",
  "Offset" : 0,
  "Length" : 0,
  "Type" : "SCM",
  "Message" : {
    "ID" : 32109876,
    "Type" : 2,
    "TamperPhy" : 1,
    "TamperEnc" : 0,
    "Consumption" : 322827,
    "ChecksumVal" : 23210
  }
}

Docker

If you use Docker and would rather launch this under a container see <README.Docker.md>.

Requirements

Tested under Raspbian GNU/Linux 9.3 (stretch)

rtl-sdr package

Install RTL-SDR package

sudo apt-get install rtl-sdr

Set permissions on rtl-sdr device

/etc/udev/rules.d/rtl-sdr.rules

SUBSYSTEMS=="usb", ATTRS{idVendor}=="0bda", ATTRS{idProduct}=="2838", MODE:="0666"

Prevent tv tuner drivers from using rtl-sdr device

/etc/modprobe.d/rtl-sdr.conf

blacklist dvb_usb_rtl28xxu

git

sudo apt-get install git

pip3 and paho-mqtt

Install pip for python 3

sudo apt-get install python3-pip

Install paho-mqtt package for python3

sudo pip3 install paho-mqtt

golang & rtlamr

Install Go programming language & set gopath

sudo apt-get install golang

https://github.com/golang/go/wiki/SettingGOPATH

If only running go to get rtlamr, just set environment temporarily with the following command

export GOPATH=$HOME/go

Install rtlamr https://github.com/bemasher/rtlamr

go get github.com/bemasher/rtlamr

To make things convenient, I'm copying rtlamr to /usr/local/bin

sudo cp ~/go/bin/rtlamr /usr/local/bin/rtlamr

Install

Clone Repo

Clone repo into opt

cd /opt

sudo git clone https://github.com/ragingcomputer/amridm2mqtt.git

Configure

Copy template to settings.py

cd /opt/amridm2mqtt

sudo cp settings_template.py settings.py

Edit file and replace with appropriate values for your configuration

sudo nano /opt/amridm2mqtt/settings.py

TODO

TLS setup is not performed, so you can only use the non-secure TCP port which is 1883 by default.

Install Service and Start

Copy armidm2mqtt service configuration into systemd config

sudo cp /opt/amridm2mqtt/amridm2mqtt.systemd.service /etc/systemd/system/amridm2mqtt.service

Refresh systemd configuration

sudo systemctl daemon-reload

Start amridm2mqtt service

sudo service amridm2mqtt start

Set amridm2mqtt to run on startup

sudo systemctl enable amridm2mqtt.service

Configure Home Assistant

To use these values in Home Assistant,

sensor:
  - platform: mqtt
    state_topic: "amr/reading/SCM/4/12345678/message"
    name: "Electric Meter"
    unique_id: electric_meter_01
    unit_of_measurement: kWh
    device_class: energy
    state_class: measurement
    availability_topic: amr/status/availability
    last_reset_topic: amr/status/last_reset
    value_template: "{{ value_json.Message.Consumption | float }}"
    json_attributes_template: "{{ value_json.Message | tojson }}"
    json_attributes_topic: "amr/reading/SCM/4/12345678/message"

Note that we are publishing status information to the amr/status topic:

amr/status/availability: online|offline when the service starts or stops, respectively.
amr/status/last_reset: 1970-01-01T00:00:00+00:00 see last_reset_topic for more information.

Testing

Assuming you're using mosquitto as the server, and your meter's sends SCM messages with id 12345678, you can watch for events using the command:

mosquitto_sub -t "amr/reading/SCM/4/12345678/message"

Or if you've password protected mosquitto

mosquitto_sub -t "amr/reading/SCM/4/12345678/message" -u <user_name> -P <password>

If all else fails, you can listen to the base topic to see what's actually getting posted

mosquitto_sub -t "amr/#" -u <user_name> -P <password>

Most electric meters are going to be type 4 or 7. For a list of meter types and ERT types, see: https://github.com/bemasher/rtlamr/blob/master/meters.csv

amridm2mqtt's People

Contributors

bassrock avatar brent20 avatar ragingcomputer avatar skullydazed avatar tonyapuzzo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.