Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


Recommended Posts

Posted (edited)

 

I'm not sure if many people know this but Fibaro HC's API supports fully updating a virtual device through the API.

 

Introduction

This tutorial works with both the HCL and HC2 as it's pure HTTP commands no LUA involved.

 

Everything can be updated on the fly using the following URL.

Please login or register to see this code.

Simply issuing a PUT command to this URL will update the virtual device.

We will send the data as JSON, so set the header accordingly

Please login or register to see this code.

For the purpose of this tutorial we will need two tools.


Please login or register to see this link.

this will dynamically create the buttons for us and push it to Fibaro

Please login or register to see this link.

this will be used to get the favorites and send the actions back to the Sonos device.

 

Limitations

Due to the way Fibaro issues HTTP commands no special characters can be in the URL.

To get over this we send a POST command to Node-Red which forwards the command back to the Sonos API.

Results aren't displayed instantly refresh the Fibaro page or close and re-open the app to see the changes.

 

Assumptions

This tutorial assumes that Node-Red is setup and running.

Sonos API is setup and that Sonos has favorites added.

Sonos API and Node-Red are running on the same IP.

Fibaro has a virtual device setup and you know the id of it.

 

Fibaro

Go to the Fibaro Docs page to get the body of the virtual device we want to modify.

Please login or register to see this code.

devices > GET /devices/{deviceID}

 

For this example we are going to use deviceID 67.

Click TRY IT! and copy the results into notepad.

 

Node-Red

Add a inject node (this is just a push button, so for now we don't need to configure anything with it)

Add a http request node with the following properties

Please login or register to see this code.

This node will then pull all the favorites from the Sonos API.

 

Add the change node next (this is where all the fun happens)

 

Rules (click add after each one)

  • Move

msg.payload

to - msg.sonos (this moves the favorites to a separate object for easier use)

  • Set

msg.ip

Node-red-ip (click the down arrow and choose number this is where we will send all the commands to)

  • Set

msg.port

1880 (set this to the port of node-red on your pc)

  • Set

msg.port

1880 (set this to the port of node-red on your pc)

  • Set

msg.device

67 (this example uses 67 - use the correct one for your scenario)

  • Set

msg.headers

{"Content-Type":"application/json"} (as mentioned above)

  • Set

msg.payload

click the down arrow and choose expression and then click the three dots (this contains all device properties)

 

The JSONata Expression editor is a very powerful JSON builder which will help us build the buttons we want.

 

Copy the device properties that you got from the Fibaro step into the editor window and remove the rows section.

 

You should be left with something similar to this.

 

As you can see some fields are different.

Replace the ip value with $$.ip this will then get the IP address we assigned above.

Do the same with port. Now everything should match.

 

Please login or register to see this code.

Then copy the below rows section after "visible": "true",

 

Please login or register to see this code.

$v is the each favorite value, $i is the index of each favorite and $a is the original array.

 

What is happening is it is iterating each object in the array and increasing the id by 1.

Caption is limited to 35 characters so it trims the caption to fit.

The name cannot contain any spaces so they are removed.

As mentioned above it is assumed that node-red and sonos api are on the same IP if not create another property called msg.ip2

 

The msg value contains the actions that will happen when the button is pressed

 

If ip2 exists change the last two occurrences of $$.ip to $$.ip2

 

The converted msg value looks like this

Please login or register to see this code.

The full msg.payload should look like this

 

Please login or register to see this code.

 

Connecting Node-Red to Fibaro

Create a HTTP request node

Please login or register to see this code.

Change the HC-IP to the correct value but leave device as {{{device}}} this tells Node-Red to use the variable we already setup.


Add a debug node and link all of them up.

Press the button on the inject node and if everything works you should see the message in the debug windows with the new device properties. If it doesn't then retrace your steps to see where you made a mistake.

 

Sending Node-Red commands to Sonos API

 

Once everything is working and the buttons are showing we can get Node-Red to pass the commands to Sonos API.

 

Create a http in node (this will receive the command from fibaro with a payload of the favorite and room)

Please login or register to see this code.

Link it up with a http response node with a status of 200 (this will just report that it's always a success)

Add a change node

Rules

  • Move

msg.payload.url

to - msg.url (this sets the url to the favorite we want to change to)

 

Add a http request node leave everything as default as it will be passed from the change node.

 

Add a debug node and if everything works it should show success.

 

If everything works tap yourself on the back for a job well done. It's not an easy process but it can be done.

 

Please let me know if I have missed steps or something is not working for you. If required I can provide my flow but I would prefer not to.

 

Edited by NovaG
Add full msg.paylod
Posted

btw you can use 

Please login or register to see this link.

 for call Fibaro and call VD

  • Topic Author
  • Posted

    Thanks @10der I know about that too.

     

    Was this tutorial helpful or did you know about how to do it already?

    Posted
    3 hours ago, NovaG said:

     

    Dynamically created controls still make a some troubles for HC2.

    We should be careful with it, cuz sometimes created or deleted dynamically VD’s can stuck in HC2 core. 

    Just for you info

  • Topic Author
  • Posted

    Thanks. What does that mean stuck in the core?

     

    I have a HCL.

    Posted
    1 hour ago, NovaG said:

    Thanks. What does that mean stuck in the core?

     

    I have a HCL.

    Zombies processes etc 

    as I can understand when you modify VD HC not modify correctly internal vars references on VD sections etc 

    so, create VD dynamically once - OK

    but re=create VD ie modify VD dynamically “on fly” - sometimes is not safe. Alas 

  • Topic Author
  • Posted

    Ok. So was this tutorial a waste of time?

    Posted
    37 minutes ago, NovaG said:

    Ok. So was this tutorial a waste of time?

    Yep. Thank you

    JFYI 

     

    Posted (edited)
    1 hour ago, NovaG said:

    Ok. So was this tutorial a waste of time?

     

    No, it's not a waste of time.

    You have a HCL so this is a great hack for you, and can inspire others. And if you don't have issues with "stuck in core"

    Please login or register to see this link.

    , I see no problem.

    Reading your tutorial gave me some ideas (I'm not that deep into node-red yet) that I will try out in the future.

    Thank you for contributing to the forum - and please continue to do so.

     

    Oh, and I don't mean 'hack' in a negative way - 'hacks' are the only way to get around the limitations of this box :-) 

    Edited by jgab
  • Topic Author
  • Posted

    Thanks @jgab that's all I want I got disheartened by @10der comments.

     

    But if I help someone even just a little bit it's worth it.

    • Thanks 1

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...