how does the button widget work ?

Hi,

i created a new feed (i tried virtual and normal) and then i added a button widget, selected the new feed and choose to set value to 1 i added another button widget selected same feed and set value to 0. 

i then wanted in nodered to reed from that feed, if a change is detected and the value 1 is one, my wemo insight should turn on if it's 0 it should turn off.

However this does not seem to work, whenever i click on a button widget it briefly turns green and then back to red but no value is being written to the feed.

Is this normal ? How is this supposed to work the button widget ? cause i can't seem to make it work or to make it write a value to a (new) feed

 

thanks

joyrider3774's picture

Re: how does the button widget work ?

nevermind i got it working had to change source code, i can now succesfully change the value of a feed to 1 or 0  using the button widget read from node-red and switch my plug on or off

 

this post pointed me in the right direction: http://openenergymonitor.org/emon/node/6394

joyrider3774's picture

Re: how does the button widget work ?

here's what i changed

in the button widget the render function (not sure if everything is needed):

function button_events()

{

  $('.button').on("click", function(event) {

    var feedid = $(this).attr("feedid");

   

    if (associd[feedid] !== associd[feedid]) {

     if ($(this).attr("value") !== $(this).attr("value")) $(this).attr("value",0);

     associd[feedid] = $(this).attr("value");

     }

    var outval = 0;

    var invalue = associd[feedid]['value']*1;

    if ((invalue) == 0) outval = 1;

    if ((invalue) == 1) outval = 0;

    feed.update(feedid,parseInt((new Date()).getTime()/1000),outval);

    $(this).attr("value",outval);

    var id = "can-"+$(this).attr("id");

    draw_button(widgetcanvas[id], outval + 4);

    associd[feedid]['value'] = outval;

  });

}

 

in the modules\feed\feed.js add the following function between one of the others

 

'update':function(id, time, value)

   {

     var result = {};

     $.ajax({ url: path+"feed/update.json", data: "id="+id+"&time="+time+"&value="+value, async: false, success: function(data){} });

     return result;

   },

 

and this is my flow for node red:

[{"id":"73bf269f.f3eff8","type":"emoncms-server","z":"bbadf73d.4c57f8","server":"http://localhost:8083/emoncmsnew","name":"wamp emoncms"},{"id":"42a2eb3c.3c6578","type":"wemo out","z":"bbadf73d.4c57f8","ipaddr":"192.168.0.114","name":"","x":1195,"y":474,"wires":[]},{"id":"830fedae.c692","type":"template","z":"bbadf73d.4c57f8","name":"","field":"payload","format":"handlebars","template":"0","x":975,"y":453,"wires":[["42a2eb3c.3c6578"]]},{"id":"9b5b4dec.514a5","type":"template","z":"bbadf73d.4c57f8","name":"","field":"payload","format":"handlebars","template":"1","x":973,"y":514,"wires":[["42a2eb3c.3c6578"]]},{"id":"76275f8.c795ca","type":"switch","z":"bbadf73d.4c57f8","name":"","property":"payload","rules":[{"t":"lt","v":"1"},{"t":"gt","v":"0"}],"checkall":"true","outputs":2,"x":774,"y":500,"wires":[["830fedae.c692"],["9b5b4dec.514a5"]]},{"id":"1a74170e.c52085","type":"rbe","z":"bbadf73d.4c57f8","name":"","func":"rbe","gap":"","x":622,"y":501,"wires":[["76275f8.c795ca"]]},{"id":"ef0ddc67.dff1a8","type":"emoncms in","z":"bbadf73d.4c57f8","name":"Emoncms","emonServer":"73bf269f.f3eff8","feedid":"129","x":446,"y":295,"wires":[["d3e38c9f.01cf8","1a74170e.c52085"]]},{"id":"d3e38c9f.01cf8","type":"debug","z":"bbadf73d.4c57f8","name":"","active":false,"console":"false","complete":"false","x":772,"y":294,"wires":[]},{"id":"f65cdd82.a5dd5","type":"inject","z":"bbadf73d.4c57f8","name":"","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":true,"x":223,"y":294,"wires":[["ef0ddc67.dff1a8"]]}]

 

 

the only problem it's having is that emoncms does not detect if i switch off my light / wemo manually (so without using the button switch), i'm going to try and see if i can edit my flow to check for this first and then change the value of the feed accordingly

joyrider3774's picture

Re: how does the button widget work ?

if you add this flow the status is updated on the button widget in emoncms so when i turn of my light using the wemo node red posts the value to emoncms, emoncms then logs this input value to the feed that the button is using. i even noticed the color changing on the button when i externally switch off my wemo insight either by my app on smartphone or manually change wemo state by clicking on the button on the wemo switch so using this flow as well as the flow i posted earlier everything gets updated and i can control the wemo insight from within emoncms

 

[{"id":"73bf269f.f3eff8","type":"emoncms-server","z":"bbadf73d.4c57f8","server":"http://localhost:8083/emoncmsnew","name":"wamp emoncms"},{"id":"3b4434ed.dcbce4","type":"emoncms","z":"bbadf73d.4c57f8","name":"Emoncms","emonServer":"73bf269f.f3eff8","nodegroup":"15","x":773,"y":68,"wires":[]},{"id":"d67b323f.d1c408","type":"template","z":"bbadf73d.4c57f8","name":"","field":"payload","format":"handlebars","template":"lampcontrol:0","x":527,"y":37,"wires":[["3b4434ed.dcbce4"]]},{"id":"9fee3a4c.7e9c18","type":"template","z":"bbadf73d.4c57f8","name":"","field":"payload","format":"handlebars","template":"lampcontrol:1","x":524,"y":86,"wires":[["3b4434ed.dcbce4"]]},{"id":"e2c5e277.565c58","type":"switch","z":"bbadf73d.4c57f8","name":"","property":"payload","rules":[{"t":"eq","v":"0"},{"t":"eq","v":"1"}],"checkall":"true","outputs":2,"x":308,"y":73,"wires":[["d67b323f.d1c408"],["9fee3a4c.7e9c18"]]},{"id":"be396fdb.75952","type":"wemo in","z":"bbadf73d.4c57f8","ipaddr":"192.168.0.114","name":"","x":110,"y":76,"wires":[["e2c5e277.565c58"]]}]

tgmaxx's picture

Re: how does the button widget work ?

joyrider3774,

I modified the two files with your changes (button_render.js and feed.js). The button widget now works as expected.

We should get the two files changed in the code for everyone.

Thanks,

Tom

 

tgmaxx's picture

Re: how does the button widget work ?

I did some more testing and found that if not logged in, the button widget changes noted above do not work. Apparently due to the inability to update the data in the feed. I added the apikeystr (as in other functions in feed.js), but that doesn't work. How can I get/add the api write keyin feed.js?

 

joyrider3774's picture

Re: how does the button widget work ?

i'm confused how would you be able to click the button widget if you are not logged in ? unless you perhaps use the "public" dashboards thing ? i'm not actually using that and whenever i might click the button i'm logged in so for my needs it works. Just wondering how your using it then ?

tgmaxx's picture

Re: how does the button widget work ?

My emoncms is local, running on a Raspberry Pi. I can view dashboards and click the button when not logged in - it changes to the first color, but then reverts back to the original, because there is no feedback from the update function. Anyway, the more I thought about it, that is probably a good thing, because you don't want to control things if not logged in. It's a feature!

 

joyrider3774's picture

Re: how does the button widget work ?

ah i'm also running a local version although on windows, never knew you could access the dashboards when not logged in i thought they had to be set to public or something. Anyway i was think the same thing, my emoncms installation is accessible from to internet and don't want anyone that might find out a way to display the dashboards to be able to press the button. So i think it's indeed safer if you need to be logged in, in order to be able to change the feed value

tgmaxx's picture

Re: how does the button widget work ?

All,

I tried to publish a MQTT message to a Node-Red flow when a button press causes the input to change. That makes more sense than always polling the feed for a change from Node-Red. But, I found the input never changes value, even though the feed does change (0 to 1 to 0....).

Since the input doesn't change, it never sends a MQTT message. Does anyone have an idea why the input doesn't change on a button press? That's what needs to happen so we can publish to MQTT on press.

Thanks,

Tom

 

joyrider3774's picture

Re: how does the button widget work ?

tgmax, the reason is that the update function uses the feed api to change the feed value if you want have the input changed use the input api insead in the update function but i don't think that's possible since you don't actually know the input id you only know the feed id thus the reason it's using the feed api and not the input api  and why the input never changes nor a mqtt message is send. I don't think you can get the input id based on a feed id

tgmaxx's picture

Re: how does the button widget work ?

I found a solution to get MQTT messages -- I used a virtual feed, with a "Source Feed" of the button feed, and "Publish to MQTT" in the virtual feed's Process list.

I get MQTT messages about every 2 to 5 seconds, regardless if the button has been pressed. But when pressed, the state updates and I get the correct value, 0 or 1, in the message.

I created the initial feed using a Node-Red flow, having a Emoncms node configured with the node number I wanted.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.