But to begin I do not find where the / in the key name is removed. I found where it is removed for nodeid but not for the key. Is it removed in an other place when it is displayed in the website ?
By default there is no loop around different nodeids - the nodeid is picked up from the topic in emon\topic and the function runs once for one node for every item of published data. It would need a complete refactor to do anything else.
I am not familiar at all with php but I will try to modify the code to add this functionnality.
My idea is to remame the actual message function as message_std, and then write a new message function. if the decoded json with depth 3 is OK, I call message_std with the original message, if a depth error , I decode with depth 4, and then loop like this:
foreach($jsondata as $topic=>$payload) {
$loopmessage->topic= $topic;
$loopmessage->payload=json_encode($payload);
message_std($loopmessage);
}
Your entire project seems to be based on this assertion. Have you actually measured/tested it? How much more efficient will it be and is this improvement in efficiency important? Otherwise your project is likely to result in you having a non-standard system to maintain
My system is customized to fit my needs and so already “non standard”.
In order to be independent from inverter manufacturers , I have added Eastron AC and DC meters on DC strings (2x South, East + West), Battery , and AC for the two inverters, as well as the Gridmeter. So with 8 modbus meters I wrote my own python script to handle the data readings rather than using emonhub.
And as my only mean to control battery charge and discharge is by fooling the hybrid inverter with modified gridmeter data, I have implemented a fake slave modbus gridmeter. But everything shall work in realtime, i.e. 1 second loop time, it is why I hit the MQTT access limit and needed this special multinode publish implementation.
Concerning the single mqtt access for multiple nodes, this works well by publishing to a MultiNodes topic.
rename the message function to message_singlenode
define a new message function:
function message($message)
{ global $log;
try {
$value = $message->payload;
if ($message->topic == 'emon/MultiNodes' ) {
$jsondata = json_decode($value,true,4);
if ((json_last_error() === JSON_ERROR_NONE) && is_array($jsondata)) {
// multiple nodeid JSON,
$log->info("Multiple Node JSON received, looping to original mqttmessage process");
foreach($jsondata as $topic=>$payload) {
$loopmessage= new StdClass();
$loopmessage->topic= $topic;
$loopmessage->payload= json_encode($payload);
$log-info("Multiple Node loop ".$topic.'-->'.$loopmessage->payload);
message_singlenode($loopmessage);
}
}
}
else {
message_singlenode($message);
}
}
catch (Exception $e) {
$log->error($e);
}
}