Button widget acts twice

My thinger resource is like:

thing [ “Value_in”] << [] (pson & in) {
some c code with no action on “in”

On the other side I defined a dashboard with a button widget type: On/Off state.
When I push the button change its state twice, and my c-code is executed 2 times.
How can fix it?

Hi @corri,

welcome to the community! some recommendations, anytime you post code in the forum, please, use the code button to correctly format it, i.e.,

thing [ “Value_in”] << [] (pson & in) {
    some c code with no action on “in”

Regarding your question, it is normal that your code is executed twice when you call it from a dashboard. What the system does, is:

  • Send the desired value to the resource
  • Execute the resource to check the actual value established, in order to transmit current state to listening dashboards.

You can check if there is incoming data or not with the is_empty. Direct example copied for our documentation: https://docs.thinger.io/quick-sart/coding-guide#show-input-resources-state-in-dashboards-and-api

thing["led"] << [](pson& in){
        in = (bool) digitalRead(pin);
        digitalWrite(pin, in ? HIGH : LOW);

Hope it helps!

Thanks a lot, now is clear how it works. I tested two solutions, one with inputValue() macro and the other one with is_empty() function and both work.
Another question, i.e. if I use the inputValue(myvar) and myvar is boolean, I can set it by means of a widget ON/OFF type, but if myvar is an integer or a string type, how can I set it from thinger side?

At this moment there are only widgets for on/off and range values based on the slider. We are working on new control widgets. In the meanwhile you could create your own HTML widget using the API, but this is not straightforward.

Hi Alvaro, many thanks, have a good job.