domoticz in synology as package! Available now.

Others (MiLight, Hue, Toon etc...)

Moderator: leecollings

Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Gravityz wrote: Thursday 16 April 2020 18:48 i have build 2020.1.839 running

i also use homekit-homebridge edomoticz-domoticz
can you tell me what problems you are experiencing because sofar i can tell everything is workin
Thanks for checking up on this.
There are two main issues which i experience;
  1. When a switch gets changed (manual/physical switch inside the room, e.g. connected to Sonoff Mini with Tasmota) the status of the switch doesn't automatically update in Homekit/Homebridge, only after a complete reload of the app (quit app and reopen, no force quit needed) the status is in sync again. This is really annoying
  2. Secondly the sensors do not automatically update. For example I have a 'door contact sensor' connected to the garage door. Before the 2020.1 version I would get a notification from the Home app that the door was opened or closed. After the update the notification isn't send anymore because the sensor status is not updated in Homebridge/Home app. Only when I open the home the status of the sensor is updated and I receive a notification at that moment. Problem is I want a notification even when I do not have the home app active in my screen. This used to work great and now it doesn't anymore.
Because I really need the notifications from the garage door I tried my previous setup with Domoticz and Pilot app on iphone. The sensors and status get updated immediately in that app when they change (via virtual switch in Domoticz web or via manual switch in room). So That is working and it really seems to be a Domoticz -> eDomoticz -> Homebridge -> Homekit issue. Or MQTT related as I have some MQTT devices and a Zigbee2MQTT setup. The sensors are Tasmota RF bridge connected to Domoticz.
When connecting the dots I think it is due to the loopback which got deleted and therefor the mqtt status doesn't get send through anymore to update the status in Homebridge/homekit.

Have you tried it with this setup, and from a manual, physical switch?

Edit: I see you responded to the issue (#3863) on Github as well. Extra info: I do not have nodered installed. And Jumbotroll replied that he doesn't have time to make a new package with the beta, so I am stuck at the moment with the package of the latest stable for Synology. Do not want to change from Synology to raspberry pi of to Docker because I am afraid I will lose all settings, history, scripts and setup.
Last edited by Stormryder on Thursday 16 April 2020 20:19, edited 1 time in total.
JonyBCN
Posts: 39
Joined: Tuesday 24 May 2016 22:39
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by JonyBCN »

I also use homebridge with eDomoticz and no problem. I do not have those messages and the devices update practically instantly.

The only problem (it was happening before) is that one of the devices, a fibaro dimmer, works perfectly with the App but with siri once in 20 (approximately) it does not work. I mean, I tell siri to turn on the light and it doesn't. But if I open the app it appears as on. It's weird.
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

ok. regarding the home app i noticed that sometimes the app did not update. i had to start the eve app(which updated the status) and after that the home app displayed the correct status again.

i experienced this functionality way before all the MQTT changes and thought this is a home app bug(which it is)

so in my current setup i started up the home app

light veranda shows off (this is a fibaro plug so no native homekit support)
i switch the light on through a browser on my pc
the light turns on instantly in the home app
the same when turning off

did the same with a physical fibaro dimmer switch
it reacts instantly, als when i change the dimming level the level changes instantly in homekit.

i have a 433mhz temperature sensor(also configured through edomoticz)
this also shows the value and changes instantly through

so the things you are experiencing are not good, especially if they changed this functionality because somebody complaint about it(i think the inital complaint was about looping data)

so my setup is

synology 718+
2020.1.839 build from Jumbotroll running as package
latest homebridge with edomoticz(running as a container in docker)
iphone xs with homekit
homebridge.JPG
homebridge.JPG (172.38 KiB) Viewed 2618 times
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

JonyBCN wrote: Thursday 16 April 2020 20:15 I also use homebridge with eDomoticz and no problem. I do not have those messages and the devices update practically instantly.
Gravityz wrote: Thursday 16 April 2020 20:29
so the things you are experiencing are not good, especially if they changed this functionality because somebody complaint about it(i think the inital complaint was about looping data)
This really feels weird, but I was experiencing trouble right after I updated to version 2020.1. Switches were not working in Home-app.
I found the loopback issue and someone mentioned that disabling MQTT in edomoticz would be a workaround for as long as the loopback was not available. I tried disabling MQTT and the switches worked again.

The strange thing is that I just enabled MQTT again in eDomoticz (as a test) and now the switches work again and status of switch (and sensors) get updated instantly. So the problem seems to be solved.
So it seems everything is fine now, even though I didn't change Domoticz and no Homebridge/eDomoticz update has been deployed afaik.

Edit; everything is not fine anymore, see next post
Last edited by Stormryder on Thursday 16 April 2020 21:17, edited 1 time in total.
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

I am giving up again, I thought everything was working correct now.
But I restarted Homebridge (and delete CachedAccessoiries) because I had to exclude a newly added device.
After the restart the trouble started again;
- With MQTT enabled in eDomoticz the switch in Home-app doesn't work, but the status changes when I use a manual switch or use Domoticz web interface to flip the switch.
- With MQTT disabled in eDomoticz the switch in Home-app does work, but status of switches and sensors do not get updated.

So I am really lost here, because with the Pilot app everything keeps working. So the issue really seems to lie inside eDomoticz or Homebridge.
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

these are my settings.

some time ago i had problems with homebridge. things were outdated but upon updating it rebooted and rebooted.
after a while everything got updated and was stable again
homebridge.JPG
homebridge.JPG (172.38 KiB) Viewed 2608 times
plugins.JPG
plugins.JPG (61.68 KiB) Viewed 2608 times
homebridgeconfig.JPG
homebridgeconfig.JPG (66.89 KiB) Viewed 2608 times
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Dug a little deeper and found the following (do not know if related or if I am messing everything up :))

When I use Home-app the command reaches Domoticz, but doesn't actually change the setting of the switch.
I tested with the switches from Home-app, Pilot-app and Domoticz web and found out that it might depend on the 'User' that is sending the message:
Schermafbeelding 2020-04-16 om 22.40.05.jpg
Schermafbeelding 2020-04-16 om 22.40.05.jpg (169.66 KiB) Viewed 2602 times
The messages that have 'Gebruiker' (User) Admin actually change the status of the light and the messages of the 'Gebruiker' MQTT do not do anything.

Device/platform -> Gebruiker
Home app Apple -> MQTT
Homebridge web -> MQTT
Pilot app -> Admin
Domoticz web -> Admin

These are my settings of eDomoticz:
Schermafbeelding 2020-04-16 om 22.49.21.png
Schermafbeelding 2020-04-16 om 22.49.21.png (132.97 KiB) Viewed 2602 times
Is there some way to get MQTT to use Admin as a User?
These are my settings of MQTT in Domoticz:
Schermafbeelding 2020-04-16 om 22.51.46 1.jpg
Schermafbeelding 2020-04-16 om 22.51.46 1.jpg (279.14 KiB) Viewed 2602 times
Edit: When I try to control a Zigbee device it works with all switches (Home-app, physical switch, Pilot app, Domoticz web). And the user is 'MQTT'. So this is strange to me. MQTT server plugin installed in Domoticz, Mosquitto on Synology and Zigbee2MQTT in Docker. The devices that do not work are Tasmota devices. So looks like the 'loopback' issue; https://github.com/domoticz/domoticz/issues/4035 and https://github.com/domoticz/domoticz/issues/3971
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

strange.

so the light received the MQTT command as shown in the log but does not switch the light on/off

if you check my settings you can see you can specifiy user and password for edomoticz.

now i really do not know what this does when you fill in admin as the user and leave the password blank but maybe you can try.
if a blank pasword does not work then you might set a password in domomitcz for user admin and user that same password in edomoticz

the domoticz settings seem to default to admin so leave those clear.
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

@Stormryder.

some time ago i wanted to have a MQTT broadcast script which waaren made

this script sends, when triggered, the status of all lights, switches and dimmers through MQTT.

maybe you can use it to solve the the problem you have with the tasmota switches until it is fixed in domoticz(or until Jumbotroll compiles a new version)
it needs you to install mosquitto_pub on your synology because this tool sends out the MQTT command

here it is

Code: Select all

--[[ dumpMQTT for all switch type devices 

    this dzVents script sends current status of all switch-type devices with ()optionally) the string mqttsync in the descriptionfield to 
    the defined mqtt broker with the topic domoticz/out in the same json format as domoticz mqtt hardware sends it on a device update
    it uses the mosquitto_pub OS program for this.

    configurable items: 
                            MQTTBokerhost   // hostnamae or IP where MQTT Broker is active   ; defaults to localhost
                            mosquitto_pub   // location of full /path/executable             ; defaults to /usr/bin/moquitto_pub
                            MQTTTopic       // MQTT topic to be used                         ; defaults to domoticz/out
                            MQTTSyncTrigger // string in description-field of device to enable syncTrigger ; defaults to All switch type devices           

    Thanks to @Gravityz for the initial idea, testing-, and feedback.
    
]]--

local scriptTrigger = 'MQTT-Broadcast' -- this is a virtual dummy switch in domoticz which triggers the script

return 
{
    on = 
    {
        -- timer = {'every 10 minutes'}, -- when frequent broadcasts are required
        devices = { scriptTrigger },
    },

    logging = { level = domoticz.LOG_ERROR }, -- switch to LOG_DEBUG when not eoowrking as expected

    execute = function(dz)
        
       -- ************** Your settings (when not default) below this line *********************

        local MQTTBrokerHost  
        local MQTTTopic 
        local mosquitto_pub = "/volume1/@appstore/mosquitto/bin/mosquitto_pub"                  -- set to '/volume1/@appstore/mosquitto/bin/mosquitto_pub' for Synology NAS
        local MQTTSyncTrigger = "MQTTSyncTrigger"                                               -- only sync switches with this string in description field leave as nil when all need to be syncd

      -- ************** No changes required below this line ************************
      
       -- set defaults 
        _G.logMarker =  _G.logMarker or _G.moduleLabel -- set logmarker to scriptname
             
        local MQTTBrokerHost = MQTTBokerHost or 'localhost'
        local MQTTTopic = MQTTTopic or 'domoticz/out'
        local mosquitto_pub = mosquitto_pub or '/usr/bin/mosquitto_pub'
        local MQTTSyncTrigger = MQTTSyncTrigger ~= nil and MQTTSyncTrigger  
       
       local function osCommand(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

        local function composeMQTT(dv)
            local json = {}

           if dv.deviceSubType == 'Selector Switch' then
                json.LevelActions = dv.levelActions
                json.LevelNames = table.concat(dv.levelNames,'|')
                json.LevelOffHidden = dv.levelOffHidden
                json.SelectorStyle = "0"
                json.svalue1 = dv.levelVal
            elseif dv.switchType == 'Dimmer' then
                json.Level = dv.level 
            end

            if dv.color ~= '' then
                json.Color = dz.utils.fromJSON(dv.color) 
            end

            if not(json.svalue1) then
                for key, value in ipairs(dv.rawData) do
                    json['svalue' .. key] = value:match('%d+%.*%d*') 
                end 
            end

            json.battery = (dv.batteryLevel or 255)
            json.RSSI = (dv.signalLevel or 12) 
            json.description = (dv.description):sub(0,99)
            json.dtype = dv.deviceType
            json.id = dv.deviceId
            json.idx = dv.id
            json.name = dv.name
            json.nvalue = dv.nValue
            json.stype = dv.deviceSubType
            json.switchType = dv.switchType
            json.unit = (dv.unit or 1)
            unit = dv.unit

            json = dz.utils.toJSON(json) 
            local jsonFormatted = json:gsub(',',',\n  '):gsub('}','\n } \n'):gsub('{','\n { \n  '):gsub(':',' : ')                                      --origineel
 
            
            
          -- replace.gsub(/#{year}","1/, %{#{year}","b})
            dz.log('Composed json: ' .. jsonFormatted,dz.LOG_DEBUG)
            return ( mosquitto_pub ..  ' -h '.. MQTTBrokerHost  .. ' -t '  .. MQTTTopic .. " -m '" .. jsonFormatted .. "'")
        end

        -- Main code
        dz.devices().forEach(function(dv) -- loop over all devices in domoticz instance
            -- All switch like devices ( with ( optionally ) MQTTSyncTrigger in description )  
            if (dv.deviceType or ''):find('witch') and ( not(MQTTSyncTrigger) or dv.description:find(MQTTSyncTrigger)) then 
                dz.log(dv.id .. ', ' .. dv.name .. ', ' .. dv.deviceSubType,dz.LOG_DEBUG) 
                osCommand(composeMQTT(dv))
            end
            end)
       end
}
Last edited by Gravityz on Saturday 18 April 2020 18:58, edited 1 time in total.
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Gravityz wrote: Friday 17 April 2020 16:25
maybe you can use it to solve the the problem you have with the tasmota switches until it is fixed in domoticz(or until Jumbotroll compiles a new version)
it needs you to install mosquitto_pub on your synology because this tool sends out the MQTT command
Thanks, I do have Mosquitto installed on my Synology so that should work.
Where do I install the script? Task manager or Domoticz and if in Domoticz how should it be triggered?

I tried adding/changing username of MQTT in eDomoticz to Admin but after changing and restarting Homebridge the user who changes the switch in Domoticz stays MQTT. So this does not seem to be related. But was worth the check.

Edit; I just noted that the Zigbee devices do work when I control them with Home-app/Homebridge. So looks like only the Tasmota devices do not work. When I watch the console of the Tasmota device there is no command coming in when I use the Home-app switch. So the command seems to stop in Domoticz.
Tasmota Device -> works with MQTT and Domoticz settings in Tasmota
Send from Home-app and does NOT turn device on/off (user = MQTT):
2020-04-18 10:03:28.971 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":40,"switchcmd":"On"}
2020-04-18 10:03:28.974 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:03:39.761 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":40,"switchcmd":"Off"}
2020-04-18 10:03:39.764 (Inverter virtual) Light/Switch (Bloemstuk)

Send from Pilot-app or Domoticz webinterface and turns device on/off (user = Admin):
2020-04-18 10:03:49.458 Status: User: Admin initiated a switch command (40/Bloemstuk/On)
2020-04-18 10:03:49.460 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:03:50.632 Status: User: Admin initiated a switch command (40/Bloemstuk/Off)
2020-04-18 10:03:50.635 (Inverter virtual) Light/Switch (Bloemstuk)

Zigbee Device-> works with Zigbee2MQTT
Send from Home-app and turns on/off device (user = MQTT):
2020-04-18 09:59:08.444 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":61,"switchcmd":"On"}
2020-04-18 09:59:10.012 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":61,"switchcmd":"Off"}

Send from Pilot-app or Domoticz webinterface and turns device on/off (user = Admin):
2020-04-18 10:30:06.855 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:30:06.852 Status: User: Admin initiated a switch command (40/Bloemstuk/On)
2020-04-18 10:30:11.468 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:30:11.465 Status: User: Admin initiated a switch command (40/Bloemstuk/Off)
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

you go to setup-more options-events
there you create a new dzvents script by pushing the + dzvents-minimal.

delete everything which is generated in the default script and paste the MQTT script in it.

it runs on my synology so i think everything should work out right away

on top it says

Code: Select all

local scriptTrigger = 'MQTT-Broadcast'
the MQTT-Broadcast is a virtual switch which i flip from node-red, espeasy or whatever.

the timer functionality is commented out. if you want to run the script every 10 minutes remove the --

now the last thing to look at is this.

the script initially scans every switch or dimmer you have and broadcasts those
you can avoid this by labeling the switches you want to trigger with a special label (MQTTSyncTrigger in this script)
So you put this word in the desciption field of every switch you want to send out an MQTT command
mqttsync.JPG
mqttsync.JPG (32.43 KiB) Viewed 2530 times
if you want every switch to respond it is easier to just remove the triggerword in the script

Code: Select all

local MQTTSyncTrigger = ""  
so if your tasmota switch flips in domoticz but aparently domoticz does not send out an MQTT command then you need to put the names of your tasmota switches in the trigger devices section on top

change this

Code: Select all

       devices = { scriptTrigger }
to this

Code: Select all

        devices = {
            'tasmotaswitch1',
            'tasmotaswitch2'
        }
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Thanks for the extra information.
Although the status of the switch is not an issue. As soon as I flip the switch in e.g. Domoticz web, the status gets updated in Home-app.
The only real problem is that I can not control the switches (Tasmota) from within the Home-app. The MQTT command stops in Domoticz and doesn't get send to the Tasmota device (as it seems).

I edited my previous post with some extra findings:
I just noted that the Zigbee devices do work when I control them with Home-app/Homebridge. So looks like only the Tasmota devices do not work. When I watch the console of the Tasmota device there is no command coming in when I use the Home-app switch. So the command seems to stop in Domoticz.
Tasmota Device -> MQTT and Domoticz settings in Tasmota
Send from Home-app and does NOT turn device on/off (user = MQTT):
2020-04-18 10:03:28.971 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":40,"switchcmd":"On"}
2020-04-18 10:03:28.974 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:03:39.761 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":40,"switchcmd":"Off"}
2020-04-18 10:03:39.764 (Inverter virtual) Light/Switch (Bloemstuk)

Send from Pilot-app or Domoticz webinterface and turns device on/off (user = Admin):
2020-04-18 10:03:49.458 Status: User: Admin initiated a switch command (40/Bloemstuk/On)
2020-04-18 10:03:49.460 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:03:50.632 Status: User: Admin initiated a switch command (40/Bloemstuk/Off)
2020-04-18 10:03:50.635 (Inverter virtual) Light/Switch (Bloemstuk)

Zigbee Device-> Zigbee2MQTT
Send from Home-app and turns on/off device (user = MQTT):
2020-04-18 09:59:08.444 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":61,"switchcmd":"On"}
2020-04-18 09:59:10.012 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":61,"switchcmd":"Off"}

Send from Pilot-app or Domoticz webinterface and turns device on/off (user = Admin):
2020-04-18 10:30:06.855 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:30:06.852 Status: User: Admin initiated a switch command (40/Bloemstuk/On)
2020-04-18 10:30:11.468 (Inverter virtual) Light/Switch (Bloemstuk)
2020-04-18 10:30:11.465 Status: User: Admin initiated a switch command (40/Bloemstuk/Off)
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

yes but your problem is that commands coming from an app, flip a switch in domoticz (which they do) but in the past every switch update did send out a mqtt command over domoticz/out.

so if you flipped a switch in domoticz or flipped it by using MQTT, every time a mqtt domoticz/out command was generated.

your problem is that it is not doing that anymore so you need something else(the script) to send out that command.


so in the new situation with the script

you flip a switch in domoticz:should still work the same, MQTT is send out

you flip a switch by using an app/mqtt command:
switch flips in domoticz but does not send out mqtt command
the flipped switch triggers the script which sends out the status of every switch including the tasmota switch
this is a mqtt command so if received by the tasmota device it should turn on/off

in case i do not understand your problem, please be patient we will figur this one out but i am sure the script can fix things.

i use the script in node-red with all my lights which connect to google.
i noticed that when i chance things in node red the complete status of all my google lights was lost(since google does not turn on a light which is already on this is a problem)
so by initiating this script all devices are updated instantly.
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Ah, that seems to make sense.
Just to be sure; I need to make a virtual switch called 'MQTT-Broadcast' (type; on/off or stateless?) before the script will work?
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

only if you want to centrally broadcast every time.
in your case it is easier to do the

Code: Select all

        devices = {
            'tasmotaswitch1',
            'tasmotaswitch2'
        }
this will only trigger the script when one of your tasmota devices gets triggered.
if you have a use for 1 switch which broadcasts the status the you can always add a virtual switch to that lineup so you can do it both

Code: Select all

        devices = {
            'tasmotaswitch1',
            'tasmotaswitch2',
            'MQTT-Broadcast'
        }
the MQTT-Broadcast can be an on type since it always switches something on
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

I installed the script and changed devices (only added one Tasmota device called 'Verlichting Achtertuin'

Code: Select all

--[[ dumpMQTT for all switch type devices 

    this dzVents script sends current status of all switch-type devices with ()optionally) the string mqttsync in the descriptionfield to 
    the defined mqtt broker with the topic domoticz/out in the same json format as domoticz mqtt hardware sends it on a device update
    it uses the mosquitto_pub OS program for this.

    configurable items: 
                            MQTTBokerhost   // hostnamae or IP where MQTT Broker is active   ; defaults to localhost
                            mosquitto_pub   // location of full /path/executable             ; defaults to /usr/bin/moquitto_pub
                            MQTTTopic       // MQTT topic to be used                         ; defaults to domoticz/out
                            MQTTSyncTrigger // string in description-field of device to enable syncTrigger ; defaults to All switch type devices           

    Thanks to @Gravityz for the initial idea, testing-, and feedback.
    
]]--

local scriptTrigger = 'MQTT-Broadcast' -- this is a virtual dummy switch in domoticz which triggers the script

return 
{
    on = 
    {
        -- timer = {'every 10 minutes'}, -- when frequent broadcasts are required
        devices = { 'Verlichting Achtertuin' },
    },

    logging = { level = domoticz.LOG_ERROR }, -- switch to LOG_DEBUG when not eoowrking as expected

    execute = function(dz)
        
       -- ************** Your settings (when not default) below this line *********************

        local MQTTBrokerHost  
        local MQTTTopic 
        local mosquitto_pub = "/volume1/@appstore/mosquitto/bin/mosquitto_pub"                  -- set to '/volume1/@appstore/mosquitto/bin/mosquitto_pub' for Synology NAS
        local MQTTSyncTrigger = "MQTTSyncTrigger"                                               -- only sync switches with this string in description field leave as nil when all need to be syncd

      -- ************** No changes required below this line ************************
      
       -- set defaults 
        _G.logMarker =  _G.logMarker or _G.moduleLabel -- set logmarker to scriptname
             
        local MQTTBrokerHost = MQTTBokerHost or 'localhost'
        local MQTTTopic = MQTTTopic or 'domoticz/out'
        local mosquitto_pub = mosquitto_pub or '/usr/bin/mosquitto_pub'
        local MQTTSyncTrigger = MQTTSyncTrigger ~= nil and MQTTSyncTrigger  
       
       local function osCommand(cmd)
            dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               dz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message ' ) ,dz.LOG_ERROR)
            else -- all is fine!!
                dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode
        end

        local function composeMQTT(dv)
            local json = {}

           if dv.deviceSubType == 'Selector Switch' then
                json.LevelActions = dv.levelActions
                json.LevelNames = table.concat(dv.levelNames,'|')
                json.LevelOffHidden = dv.levelOffHidden
                json.SelectorStyle = "0"
                json.svalue1 = dv.levelVal
            elseif dv.switchType == 'Dimmer' then
                json.Level = dv.level 
            end

            if dv.color ~= '' then
                json.Color = dz.utils.fromJSON(dv.color) 
            end

            if not(json.svalue1) then
                for key, value in ipairs(dv.rawData) do
                    json['svalue' .. key] = value:match('%d+%.*%d*') 
                end 
            end

            json.battery = (dv.batteryLevel or 255)
            json.RSSI = (dv.signalLevel or 12) 
            json.description = (dv.description):sub(0,99)
            json.dtype = dv.deviceType
            json.id = dv.deviceId
            json.idx = dv.id
            json.name = dv.name
            json.nvalue = dv.nValue
            json.stype = dv.deviceSubType
            json.switchType = dv.switchType
            json.unit = (dv.unit or 1)
            unit = dv.unit

            json = dz.utils.toJSON(json) 
            local jsonFormatted = json:gsub(',',',\n  '):gsub('}','\n } \n'):gsub('{','\n { \n  '):gsub(':',' : ')                                      --origineel
 
            
            
          -- replace.gsub(/#{year}","1/, %{#{year}","b})
            dz.log('Composed json: ' .. jsonFormatted,dz.LOG_DEBUG)
            return ( mosquitto_pub ..  ' -h '.. MQTTBrokerHost  .. ' -t '  .. MQTTTopic .. " -m '" .. jsonFormatted .. "'")
        end

        -- Main code
        dz.devices().forEach(function(dv) -- loop over all devices in domoticz instance
            -- All switch like devices ( with ( optionally ) MQTTSyncTrigger in description )  
            if (dv.deviceType or ''):find('witch') and ( not(MQTTSyncTrigger) or dv.description:find(MQTTSyncTrigger)) then 
                dz.log(dv.id .. ', ' .. dv.name .. ', ' .. dv.deviceSubType,dz.LOG_DEBUG) 
                osCommand(composeMQTT(dv))
            end
            end)
       end
}


Unfortunately it still doesn't work when using the Home-app.
I does seem that the script gets called, but nothing happens.

Log when using Pilot-app and the light does come on.
2020-04-18 15:04:01.939 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:01.462 Status: EventSystem: Script event triggered: PowerConsumptie
2020-04-18 15:04:01.936 Status: User: Admin initiated a switch command (41/Verlichting Achtertuin/On)
2020-04-18 15:04:01.993 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "On"
2020-04-18 15:04:04.215 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:04.212 Status: User: Admin initiated a switch command (41/Verlichting Achtertuin/Off)
2020-04-18 15:04:04.270 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "Off"

Log when using Home-app and light doesn't come on.
2020-04-18 15:04:21.675 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":41,"switchcmd":"On"}
2020-04-18 15:04:21.678 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:21.731 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "On"
2020-04-18 15:04:30.423 MQTT: Topic: domoticz/in, Message: {"command":"switchlight","idx":41,"switchcmd":"Off"}
2020-04-18 15:04:30.426 (Inverter virtual) Light/Switch (Verlichting Achtertuin)
2020-04-18 15:04:30.479 Status: dzVents: Info: Handling events for: "Verlichting Achtertuin", value: "Off"
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

i see you have this specified

Code: Select all

local MQTTSyncTrigger = "MQTTSyncTrigger"
did you also put MQTTSyncTrigger in the description field of device Verlichting Achtertuin?

if not change this(the top ones where it says your settings)

Code: Select all

local MQTTSyncTrigger


also check the domoticz log that is is sending out the mqtt command.

if the script gets called without errors and you change the MQTTSyncTrigger then it must broadcast the status of all your switches
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Sorry, missed that one.
I made it nil and now it works. Thanks for all your help and patience with me :).
Even though it feels like a patch instead of a definite solution, I am really happy it is all working now.
Gravityz
Posts: 587
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2022.2
Location: Netherlands
Contact:

Re: domoticz in synology as package! Available now.

Post by Gravityz »

nice that you got it working.

maybe it feels like a patch but this script definately will be usefull in the future.
like you noticed they are going to kill that loopback feature in the future because it is not compliant to standards.
with this script you can still have the same functionality and sync device statusses between multiple packages like node-red, openhab or whatever package is using MQTT

i needed my watermeter to report back to an ESPEasy unit and made a seperate script and also another virtual switch for trigger.
after some thoughts i just added 3 lines of extra code to this MQTT script and now it is also broadcasting my watermeter value
Stormryder
Posts: 31
Joined: Friday 23 August 2019 9:43
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: domoticz in synology as package! Available now.

Post by Stormryder »

Gravityz wrote: Saturday 18 April 2020 17:31 maybe it feels like a patch but this script definately will be usefull in the future.
like you noticed they are going to kill that loopback feature in the future because it is not compliant to standards.
with this script you can still have the same functionality and sync device statusses between multiple packages like node-red, openhab or whatever package is using MQTT
You are absolutely right about that. In the end the loopback is going to be deleted and this script will not.
Great to see the sensors are working again via Home-app, even without having your iPhone active.
I added all my non-zigbee devices in the script, hopefully I will not forget to add this step when I add a new non-zigbee device, or when I change the name of a switch :).
Thanks again!
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests