Skip to content

Instantly share code, notes, and snippets.

@debsahu
Last active July 8, 2024 17:48
Show Gist options
  • Save debsahu/ee33914e8dea896ff3956f02970e38b9 to your computer and use it in GitHub Desktop.
Save debsahu/ee33914e8dea896ff3956f02970e38b9 to your computer and use it in GitHub Desktop.
RTLAMR: RTL-SDR to read electric/gas/water meters
shell_command:
rtlamr_start: 'python3.6 /home/pi/work/rtlamrmqtt.py'
kill_rtlamr: 'pkill -f rtlamr'
automation:
- action:
- data: {}
service: shell_command.rtlamr_start
alias: Start RTLAMR Script
condition: []
id: '4367493654362'
trigger:
- event: start
platform: homeassistant
- action:
- data:
payload_template: '{{ states(''sensor.gas_meter'') }}'
retain: 'true'
topic: home/gas_prior_day
service: mqtt.publish
alias: Post Day's Gas Reading for cumulative calculation
condition: []
id: '54745735557534'
trigger:
- at: 00:00:01
platform: time
- action:
- data:
payload_template: '{{0}}'
retain: 'true'
topic: home/gas_month_usage
service: mqtt.publish
alias: Current Month Gas Reading Zeroing
condition:
- before: '23:59:55'
condition: time
id: '25752745752725475'
trigger:
- platform: template
value_template: '{{ now().strftime("%d") == "01" }}'
- action:
- data:
payload_template: '{{ states(''sensor.gas_month_usage'') | float + states(''sensor.gas'') | float }}'
retain: 'true'
topic: home/gas_month_usage
service: mqtt.publish
alias: Cumulative Monthly Gas Usage
condition: []
id: '546725745752752'
trigger:
- at: '23:59:59'
platform: time
sensor:
- platform: mqtt
state_topic: "home/gasmeter"
name: "Gas Meter"
qos: 0
unit_of_measurement: "gal"
value_template: "{{ value | round(1) }}"
- platform: mqtt
state_topic: "home/gas_prior_day"
name: "Gas Prior Day"
qos: 0
unit_of_measurement: "gal"
value_template: "{{ value | round(1) }}"
- platform: mqtt
state_topic: "home/gas_month_usage"
name: "Gas Month Usage"
qos: 0
unit_of_measurement: "gal"
value_template: "{{ value | round(1) }}"
- platform: template
sensors:
gas:
value_template: '{%- if not (is_state("sensor.gas_meter","unknown") or is_state("sensor.gas_prior_day","unknown") )-%} {{ ((states.sensor.gas_meter.state | float) - (states.sensor.gas_prior_day.state | float)) | max (0) | round(1) }} {%- endif -%}' ## ensure calc is no lower than zero!
friendly_name: "Gas Today"
unit_of_measurement: "gal"
import subprocess
import paho.mqtt.client as mqtt
import time
import json
client = mqtt.Client()
client.connect("localhost", 1883, 60)
client.loop_start()
try:
while True:
completed = subprocess.run(['/home/pi/go/bin/rtlamr','-filterid=xxxxxxxxx', '-single=true', '-format=json', '-duration=10m'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
try:
data=json.loads(completed.stdout.decode("utf-8"))
except ValueError:
print("Error")
else:
reading = data['Message']['Consumption']
client.publish("home/gasmeter",reading,0,True);
print("Reading:",reading)
except KeyboardInterrupt:
print("interrupted!")
client.loop_stop()
client.disconnect()
@gbdavidx
Copy link

gbdavidx commented Mar 8, 2019

Traceback (most recent call last):
File "water.py", line 12, in
completed = subprocess.run(['$HOME/projects/bin/rtlamr -server=127.0.0.1','-filterid=1481976504', '-single=true', '-format=json', '-duration=10m'], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
AttributeError: 'module' object has no attribute 'run'

@debsahu
Copy link
Author

debsahu commented Mar 13, 2019

@fat-tire
Copy link

fat-tire commented Apr 9, 2022

Cool beans.. A quick tip: if doing rtlamr with an utility meter and you want to be picked up by the energy tracker feature in Home Assistant (and to have a history recorded of energy usage), might wanna add bits like:

    state_class: total_increasing
    device_class: gas

to the sensor. If it's an electric meter the device_class should be energy. Here's a list of all the classes. You can also see which state_class to use. There are a few depending on whether the reported usage is reset periodically or if it's an ever-increasing total.

Cheers!

@Lenny43
Copy link

Lenny43 commented May 15, 2023

I need help getting this started: I'm getting this error and timing out.. wget https://github.com/airspy/rtl_tcp/archive/master.zip

--2023-05-15 19:11:05-- https://github.com/airspy/rtl_tcp/archive/master.zip
Resolving github.com (github.com)... 140.82.114.4
Connecting to github.com (github.com)|140.82.114.4|:443... connected.
HTTP request sent, awaiting response... 404 Not Found
2023-05-15 19:11:06 ERROR 404: Not Found.

I downloaded the rtlamr, I want to do the instance of rtltcp and then run the code since everything else doesn't work.

I'm using a Mac and can't get your code to work.

@debsahu
Copy link
Author

debsahu commented May 15, 2023

@Lenny43 Maybe get in touch with @airspy as that is his/her/their code. I have an add on to make everyone’s life simpler: https://github.com/debsahu/hassio-rtlamr-addon

@Lenny43
Copy link

Lenny43 commented May 16, 2023

Please tell me the exact steps to do this project. I did everything in your Youtube video. Plus some AI help when I need help. I still can't get it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment