Dim Shelly Dimmer 2 using Homekit Node

Hi! I searched the forum for my questions, but could not find anything.

I'm a bloody newbie - so please forgive me, in case this is a silly question.
I would like to dim my new Shelly Dimmer 2 (Node: node-red-contrib-shelly) with a HomeKit Node in Service Lightbulb (Node: node-red-contrib-homekit-bridged).
With the HomeKit Node I can generate messages like:

msg.payload : Object
{ On: true }

and

msg.payload : Object
{ Brightness: 45 }

However, as I know from a different thread, the Shelly dimmer expects JSON strings like.

{"light":0,"on":true,"brightness":20}

These strings work properly, when I generate them directly from an injector node.

But I am struggling since hours by converting the output of the Homekit-Node into the desired Shelly format.
I tried different scenarios, with switches, change nodes, json nodes, etc. Without success...
Any help will be appreciated.

Do you receive 2 messages from the homekit node ? or are these messages in 1 payload msg ?

If they are 2 separate messages you try this flow, it may look a bit convoluted, but it joins the 2 together and creates it into a new message.

Flow

[{"id":"aaee9eac.c4f39","type":"inject","z":"398759ce.1de6b6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"On\": true }","payloadType":"json","x":490,"y":140,"wires":[["fe6d0da8.7a946"]]},{"id":"5fd8087a.b76f8","type":"debug","z":"398759ce.1de6b6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":160,"wires":[]},{"id":"fe6d0da8.7a946","type":"join","z":"398759ce.1de6b6","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":650,"y":160,"wires":[["be25339a.d6c9f"]]},{"id":"9e279193.430d68","type":"inject","z":"398759ce.1de6b6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"Brightness\": 45 }","payloadType":"json","x":470,"y":180,"wires":[["fe6d0da8.7a946"]]},{"id":"be25339a.d6c9f","type":"change","z":"398759ce.1de6b6","name":"create shelly msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t\"light\":0,\t\"on\":payload.On,\t\"brightness\":payload.Brightness\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":810,"y":160,"wires":[["5fd8087a.b76f8"]]}]

edit - complicated mind - simplified now

What a quick and great help. Many thanks!
I replaced the two injectors by the Homekit node.
Furthermore I created a single parallel "create shelly message" for only on/off, in case I switch directly from the bulb-icon in Homekit (without changing brightness).
Works fine!

The only thing, which now is missing: There is no output state on the Shelly Node, when I switch manually. So I cannot update the status in Homekit.
Any idea?

I am just getting into the Shelly hardware - a bunch of them were my Christmas present, purchased in the Black Friday sale :grinning_face_with_smiling_eyes:

I've only played with the LED driver and a Shelly 1 so far. However, all I did was to put them into MQTT mode and then use Node-RED to listen to and change the settings. Works really well, I don't need anything other than Node-RED and MQTT.

Here are the MQTT topics (only 2 devices showing so far):

Check out the Shelly website for the MQTT topic definitions and all the things you can receive and send.

Oh and you can get/set information via a REST API (HTTP) as well if you prefer but then you loose the ability to subscribe to changes and have to poll for them instead.

https://shelly-api-docs.shelly.cloud/

Thanks! I heard about the advantages of mqtt.
However, this means a complete new/different setup. Right?
I'd like to keep it as simple as possible - at least as long as all features will work in that way.

Well I suppose but it is certainly simpler than using Node-RED, HomeKit and the device together and you are clearly already having issues with HomeKit.

I don't use HomeKit so I can't really comment there. I'm just offering this as a different idea.

You can even define "actions" on the Shellies. They are URL's that are called when something happens on the device like a button press or a schedule firing. Those would be easily connected to Node-RED as well.

Anyway, up to you.

The shelly's also can send a http request to a http in node. You can set different request for relay/buton presses.

Like @TotallyInformation, I use MQTT to control a Shelly Dimmer2 as well as my other Shelly devices, and it works very well, especially if you plan to expand your Shelly network, but there are lots of ways to control the Shelly's. MQTT is just one of them.
I've tried most of them over the past couple of years, but I found MQTT the most reliable & flexible. It's down to personal choice.

I also control them via 'Google Home'.
Hey Google turn the light on 60%

shelly

1 Like

Thanks, Paul-Reed.
The picture looks simple. But I think I need to learn more about mqtt first.
The whole interaction is not clear to me. I know about the subscribe and publish method. However, I guess, there need to be some links within the mqtt nodes, which refer to Homekit and Shelly!?

Sorry, I don't use Homekit.

My issue is to get the status from the Shelly back to Homekit, when the Shelly is switched manually.
It's not clear to me, how this can be done via mqtt - or any other way....

Does this help - mqtt and Homekit for Shelly Dimmer (flow) - Node-RED

I also found this earlier. But I think I need more time to understand all the interactions.
I will check, when I will have some days off.
I was hoping, that I just need to change some settings in order to get the status from Shelly.
However, my initial request is answered perfectly and the basic functionality works properly now. The rest can wait.
Thanks to all of you!

Watching this playlist will help you along the way. MQTT is one of the most used eventing protocols for use in node-red and is simple to setup, but you have to understand the concepts in node-red.

Thanks a lot!
I already checked most of the playlist - and some others in parallel.

But still: mqtt will help me, connecting different sources and devices, but I don't see, how it could change the issue which I have, getting the status back from the Shelly.

I found now a way. Probably it's not the most elegant, but it works... :slightly_smiling_face:

It triggers an empty payload to the Shelly every x seconds, which pushes the status at the output. In case there is a change in the payload, it processes the status in several steps (extract On-status and brightness and convert in appropriate format) back to HomeKit.
As I said - not elegant, but working.

I haven't used the Shelley devices (though I have used other similar ones), I would have expected it to send the new status automatically via MQTT when you switch it manually. Is that not happening?

Well if you look at my gif image a few posts back, you will see that I have a MQTT node listening on the topic shellies/shellydimmer2-E0980634548C/light/0/status, and each time the brightness changes, or it's switched off, the Dimmer sends a MQTT message, which is displayed in the debug node.
In addition, the Dimmer publishes a status report every 'x' seconds ('x' is a user defined time period).

shelli

And today, I've hacked apart an old Edimax WiFi switch that had stopped working and put a Shelly 1PM into it :slight_smile:

Each Shelly, I give a custom name to that is also used as their network and MQTT name. I have a known_devices object variable in Node-RED that is also mirrored to MQTT and uses a standardised schema.

So I have a simple function (still being built) that maps the shellies topics to my known_devices topics and updates the known_devices variable. That also lets me compare the current with the previous values so I only update the known_devices operational topics when something actually changes. This is a pattern I use a lot since each type of device has its own topic/REST API structures and they often either update on a timer or you have to poll them (like the Wiser heating system).

image

known_devices

Ah, ok, now I begin to understand. Sorry.... As I said, I am really absolute beginner.
I'm getting more familiar. Watching a lot of videos. But still missing the whole picture.
Can you please tell me, how I inject the status from the Shelly to this topic. Or is it available by default? Do I need to connect my Shelly to the Cloud?
I guess, I can use exactly the same topic as you, just replacing the device ID.
shellies/shellydimmer2-DEVICE ID/light/0/status

Your device needs configuring for MQTT first.
All the information how to do this is included in the Shelly MQTT API Reference

Yes