Here is how to create a simple PHP script to send a water meter and other metrics in to Home assistant. You will need the MQTT addon for this to work.
Code for configuration.yml
mqtt:
sensor:
# Water Meter
- name: 'Water Meter Current Useage'
state_topic: 'homeassistant/sensor/water_meter_value/config/everblu/cyble/value'
value_template: '{{ value_json.reading }}'
unique_id: 'water_meter_value'
object_id: 'water_meter_value'
icon: 'mdi:water'
unit_of_measurement: 'L'
device_class: 'water'
state_class: 'total_increasing'
qos: '0'
force_update: 'true'
device:
identifiers : 'ID-MMYYYY-YY-SERIAL-TAG'
name: 'Water Meter'
model: 'Water Meter model'
manufacturer: 'Make'
- name: 'Water Meter Total Useage'
state_topic: 'homeassistant/sensor/water_meter_counter/config/everblu/cyble/counter'
value_template: '{{ value_json.totaluseage }}'
unique_id: 'water_meter_counter'
object_id: 'water_meter_counter'
icon: 'mdi:counter'
unit_of_measurement: 'L'
qos: '0'
force_update: 'true'
device:
identifiers : 'ID-MMYYYY-YY-SERIAL-TAG'
name: 'Water Meter'
model: 'Water Meter model'
manufacturer: 'Make'
- name: 'Water Meter Battery Status'
state_topic: 'homeassistant/sensor/water_meter_battery/config/everblu/cyble/battery'
value_template: '{{ value_json.batterystateofcharge }}'
unique_id: 'water_meter_battery'
object_id: 'water_meter_battery'
device_class: 'battery'
icon: 'mdi:battery'
unit_of_measurement: '%'
qos: '0'
force_update: 'true'
device:
identifiers : 'ID-MMYYYY-YY-SERIAL-TAG'
name: 'Water Meter'
model: 'Everblu Cyble Enhanced v2.1'
manufacturer: 'Itron'
- name: 'Water Meter Timestamp'
state_topic: 'homeassistant/sensor/water_meter_timestamp/config/everblu/cyble/timestamp'
value_template: '{{ value_json.timestamp }}'
unique_id: 'water_meter_timestamp'
object_id: 'water_meter_timestamp'
device_class: 'timestamp'
icon: 'mdi:clock'
qos: '0'
force_update: 'true'
device:
identifiers : 'ID-MMYYYY-YY-SERIAL-TAG'
name: 'Water Meter'
model: 'Water Meter model'
manufacturer: 'Make'
PHP Code to push a meter reading in to Home Assistant. You will need the package phpMQTT from git hub https://github.com/bluerhinos/phpMQTT .
<?php
require('phpMQTT.php');
$server = '1.1.1.1'; // change if necessary
$port = 1883; // change if necessary
$username = 'USER'; // set your username
$password = 'PASSWORD'; // set your password
$client_id = 'water_meter'; // make sure this is unique for connecting to sever - you could use uniqid()
$old_reading = "00000000";
$new_reading = "00000002";
$battery_charge_state = "89";
// ISO 8601 format 2012-01-18T11:45:00+00:00
$timedatestamp = date('c');
$Water_Used = $new_reading - $old_reading;
$water_meter_current_data = array(
'reading' => $Water_Used
);
$water_meter_current_data_json = json_encode($water_meter_current_data);
$water_meter_total_data = array(
'totaluseage' => $new_reading
);
$water_meter_total_data_json = json_encode($water_meter_total_data);
$water_meter_batterystate_data = array(
'batterystateofcharge' => $battery_charge_state
);
$water_meter_batterystate_data_json = json_encode($water_meter_batterystate_data);
$water_meter_timestamp_data = array(
'timestamp' => $timedatestamp
);
$water_meter_timestamp_data_json = json_encode($water_meter_timestamp_data);
$mqtt = new bluerhinos\phpMQTT($server, $port, $client_id);
if ($mqtt->connect(true, NULL, $username, $password)) {
$mqtt->publish('homeassistant/sensor/water_meter_value/config/everblu/cyble/value', $water_meter_current_data_json, 0, true);
$mqtt->publish('homeassistant/sensor/water_meter_counter/config/everblu/cyble/counter', $water_meter_total_data_json, 0, true);
$mqtt->publish('homeassistant/sensor/water_meter_battery/config/everblu/cyble/battery', $water_meter_batterystate_data_json, 0, true);
$mqtt->publish('homeassistant/sensor/water_meter_timestamp/config/everblu/cyble/timestamp', $water_meter_timestamp_data_json, 0, true);
$mqtt->close();
echo "Data Sent: $water_meter_current_data_json \n";
echo "Data Sent: $water_meter_total_data_json \n";
echo "Data Sent: $water_meter_batterystate_data_json \n";
echo "Data Sent: $water_meter_timestamp_data_json \n";
echo "Current Useage : $Water_Meter_Reading \n";
echo "Total Useage : $new_reading \n";
echo "Battery State : $battery_charge_state \n";
echo "Timestamp : $timedatestamp \n";
} else {
echo "Time out!\n";
}