I try to write a plugin for my Octoprint installation and I want to send some sensor data to a field in the navbar, but the values don´t get updated. The value stays constant to 4.32.
What did you already try to solve it?
No, because I don´t figure out the problem.
Additional information about your setup
SpoolScale_navbar.jinja2
<div class="navbar-text">
<span id="weight" data-bind="html: settings.settings.plugins.SpoolScale.Weight"></span> g
</div>
In your Jinja template there, you're binding to the settings value. It looks like you are trying to update a value using the websocket though, so binding to the settings will not work. You need some JavaScript to receive the websocket message & handle updating the values in the frontend.
you need the js file, the easiest way is to use the cookiecutter instructions on the docs site for developing plugins as it will create all the files for you. but end goal, you need to have something like this in the js file viewModel...
Found the mistake. self.Weight must be changed to Weight. But why do I have to change it? The code snippet from NavbarTemperature use self.xy too.
I have updated my code, based on the code snippet:
SpoolScale_navbar.jinja2
<div class="navbar-text">
<span id="weight" data-bind="html: Weight()"></span> g
</div>
SpoolScale.js
$(function()
{
function SpoolscaleViewModel(parameters)
{
var self = this;
self.Weight = ko.observable("");
self.onDataUpdaterPluginMessage = function(plugin, data)
{
if((plugin != "SpoolScale") || (!data))
{
return;
}
if(data.Weight)
{
self.Weight(data.Weight);
}
};
}
OCTOPRINT_VIEWMODELS.push({
construct: SpoolscaleViewModel,
// ViewModels your plugin depends on, e.g. loginStateViewModel, settingsViewModel, ...
dependencies: [ /* "loginStateViewModel", "settingsViewModel" */ ],
// Elements to bind to, e.g. #settings_plugin_SpoolScale, #tab_plugin_SpoolScale, ...
elements: [ /* ... */ ]
});
});
But my browser (Chrome) output this error:
Could not bind view model NavigationViewModel to target #navbar : ReferenceError: Unable to process binding "html: function(){return Weight() }"
Message: Weight is not defined
What part is missing here? It looks like an error in my jinja2 template, but how can I fix it?