I’m sharing the Ruby script I wrote today in the meantime. To be perfectly clear - I still would like a Téléinfo binding in openHAB, of course
, but since in my case the data is not collected on my main instance, I have to transmit it somehow, hence the MQTT-based solution: this script will expose the meter as a Homie MQTT device. You need the mqtt
and teleinfo
gems installed (sudo gem install mqtt
, sudo gem install teleinfo
).
If you decide to use it, alter the variables at the beginning of the script to your needs, including the Téléinfo properties you want from your meter (different models provide different properties, you have to familiarize yourself with those first).
#!/usr/bin/env ruby
require 'teleinfo'
require 'mqtt'
$device = '/dev/ttyUSB0'
$broker_host = '192.168.1.141'
$homie_device = 'linky'
$homie_node = 'teleinfo'
$homie_type = 'PowerMeter'
$homie_name = 'Linky'
$teleinfo_properties = {
:base => { :name => 'Base Tariff Energy', :unit => 'kWh', :type => 'integer', :kilo => true },
:papp => { :name => 'Apparent Power', :unit => 'VA', :type => 'integer' },
:iinst => { :name => 'Instantaneous Intensity', :unit => 'A', :type => 'integer' }
}
MQTT::Client.connect($broker_host) do |c|
# Publish device descriptions
c.publish("homie/#{$homie_device}/$homie", "4.0.0", true)
c.publish("homie/#{$homie_device}/$name", $homie_name, true)
c.publish("homie/#{$homie_device}/$state", "ready", true)
c.publish("homie/#{$homie_device}/$nodes", $homie_node, true)
c.publish("homie/#{$homie_device}/$extensions", "", true)
c.publish("homie/#{$homie_device}/#{$homie_node}/$name", $homie_name, true)
c.publish("homie/#{$homie_device}/#{$homie_node}/$type", $homie_type, true)
c.publish("homie/#{$homie_device}/#{$homie_node}/$properties", $teleinfo_properties.keys.join(','), true)
$teleinfo_properties.each { |k,v|
c.publish("homie/#{$homie_device}/#{$homie_node}/#{k.to_s}/$name", v[:name], true)
c.publish("homie/#{$homie_device}/#{$homie_node}/#{k.to_s}/$datatype", v[:type], true)
c.publish("homie/#{$homie_device}/#{$homie_node}/#{k.to_s}/$unit", v[:unit], true)
}
teleinfo = Teleinfo::Parser.new(File.open($device, 'r'))
teleinfo.each { |f|
puts f.to_json
$teleinfo_properties.each { |k,v|
if v[:kilo] then
c.publish("homie/#{$homie_device}/#{$homie_node}/#{k.to_s}", f.to_hash[k] / 1000.0, true)
else
c.publish("homie/#{$homie_device}/#{$homie_node}/#{k.to_s}", f.to_hash[k], true)
end
}
#c.publish('linky/teleinfo', f.to_json)
}
end