Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment