I have integrated the NSPanel (with Tasmota) into Domoticz and I finally got it working.
Most trouble I experienced with the mirroring of the states of the NSPanel widgets: how to trigger on a specific widget ID and retrieve its action On/Off at the same time? Funny thing is that the widgets return their state in capitals (ON/OFF) and this doesn’t work!
Finally I came up with a rule running in the NSPanel that did the trick for a noob like me.
There might/must be many other and easier ways to achieve all this but below is how I managed and I hope it is of use to someone…
I’m always open for good suggestions or additions, e.g. how to optimize the code, add more pages to NSPanel as done with e.g. LoveLace UI, etc.
- This is the integration architecture I used
- Domoticz NSPanel integration.jpg (111.02 KiB) Viewed 2631 times
1. Flash NSPanel with latest Tasmota and correct settings, see the great work from Blakadder:
https://templates.blakadder.com/sonoff_NSPanel.html
2. Add 10 virtual sensors (Switch type On/Off) in Domoticz - Switches:
- NSPanel_1, e.g. idx101
NSPanel_2, e.g. idx102
NSPanel_Widget_1, e.g. idx1001
NSPanel_Widget_2, e.g. idx1002
NSPanel_Widget_3, e.g. idx1003
NSPanel_Widget_4, e.g. idx1004
NSPanel_Widget_5, e.g. idx1005
NSPanel_Widget_6, e.g. idx1006
NSPanel_Widget_7, e.g. idx1007
NSPanel_Widget_8, e.g. idx1008
- Added Virtual Switches
- Domoticz NSPanel Switches_crop.jpg (173.15 KiB) Viewed 2631 times
For Widgets 1 to 8 = idx 1001 to 1008, add in Switches - NSPanel_Widget_# - Edit:
Code: Select all
on-action: [url]http://<IPADDR>/cm?cmnd=nspsend%20{"ctype":"group","id":"n","params":{"switch":"on","switches":[{"switch":"on","outlet":0}]}}[/url]
off-action: [url]http://<IPADDR>/cm?cmnd=nspsend%20{"ctype":"group","id":"n","params":{"switch":"off","switches":[{"switch":"off","outlet":0}]}}[/url]
with n = 1 to 8 and <IPADDR> is your local NSPanel IP-address
And you can enable “protected” if you like as you never will turn them on/off yourself
- Switches - Edit - NSPanel_widget
- Domoticz widget settings.jpg (79.68 KiB) Viewed 2631 times
3. In NSPanel – Configuration – Domoticz fill in idx1 = 101 and idx2 = 102 to control the two HW buttons + relays
4. Add a rule in NSPanel – Console:
Code: Select all
Rule 1 on NSPanel#id<1 do Var1=1000+%value% break on NSPanel#id>8 do Var1=1000+%value% break on NSPanel#id do Var1=1000+%value% endon on NSPanel#params#switch=ON do Publish domoticz/in {"command": "switchlight", "idx": %Var1%, "switchcmd": "On"} endon on NSPanel#params#switch=OFF do Publish domoticz/in {"command": "switchlight", "idx": %Var1%, "switchcmd": "Off"} endon
Rule1 1
Restart 1
5. Add two scripts to Domoticz (Dzvents):
- Script 1: Mirror Domoticz devices/groups with Domoticz NSPanel Widgets 1-8. If Dz device/group changes update Dz NSPanel widget and v.v.
- Script 2: Mirror Domoticz NSPanel Widgets state to NSPanel_Widgets every 2 minutes --> In case NSPanel is rebooted
Code: Select all
-- Script 1 to mirror Domoticz devices/groups with Domoticz NSPanel Widgets 1-8. If Dz device/group changes update Dz NSPanel widget and v.v.
return
{
on =
groups = {'Lights Living'},
devices = {'NSPanel_1', -- Lights Living (HW button 1 on NSPanel)
'NSPanel_2', -- (HW button 2 on NSPanel)
'Light Kitchen', 'NSPanel_Widget_1', 'NSPanel_Widget_2', 'NSPanel_Widget_3', 'NSPanel_Widget_4', 'NSPanel_Widget_5', 'NSPanel_Widget_6', 'NSPanel_Widget_7', 'NSPanel_Widget_8'}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'NSPanel - Mirror states Dz_Devices and Dz_NSPanel_Widgets',
},
execute = function(domoticz, item)
-- NSPanel_1: Lights Living
if (item.isGroup and item.name == 'Lights Living' and item.state == 'Off' and domoticz.devices('NSPanel_1').state ~= 'Off') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('Lights Living group Off so turn NSPanel_1 Off', domoticz.LOG_INFO)
domoticz.devices('NSPanel_1').switchOff()
elseif (item.isGroup and item.name == 'Lights Living' and item.state == 'On' and domoticz.devices('NSPanel_1').state ~= 'On') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('Lights Living group On so turn NSPanel_1 On', domoticz.LOG_INFO)
domoticz.devices('NSPanel_1').switchOn()
elseif (item.isDevice and item.name == 'NSPanel_1' and item.state == 'Off' and domoticz.groups('Lights Living').state ~= 'Off') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('NSPanel_1 Off so turn Lights Living group Off', domoticz.LOG_INFO)
domoticz.groups('Lights Living').switchOff() -- domoticz group, not Hue
elseif (item.isDevice and item.name == 'NSPanel_1' and item.state == 'On' and domoticz.groups('Lights Living').state ~= 'On') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('NSPanel_1 On so turn Lights Living group On', domoticz.LOG_INFO)
domoticz.groups('Lights Living').switchOn() -- domoticz group, not Hue
else
-- do nothing
end
-- NSPanel_2: todo
-- Widget_1: Light Kitchen
if (item.isDevice and item.name == 'Light Kitchen' and item.state == 'Off' and domoticz.devices('NSPanel_Widget_1').state == 'On') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('Light Kitchen was turned Off so mirror Dz NSPanel_Widget_1', domoticz.LOG_INFO)
domoticz.devices('NSPanel_Widget_1').switchOff()
elseif (item.isDevice and item.name == 'Light Kitchen' and item.state ~= 'Off' and domoticz.devices('NSPanel_Widget_1').state == 'Off') then -- mirror state from Dz_device/group to Dz_NSPanel Widget
domoticz.log('Light Kitchen was turned On so mirror Dz NSPanel_Widget_1', domoticz.LOG_INFO)
domoticz.devices('NSPanel_Widget_1').switchOn()
elseif (item.isDevice and item.name == 'NSPanel_Widget_1' and item.state == 'Off' and domoticz.devices('Light Kitchen').state ~= 'Off') then -- mirror state from Dz_NSPanel Widget to Dz_device/group
domoticz.log('Dz NSPanel_Widget_1 was turned Off so mirror Light Kitchen', domoticz.LOG_INFO)
domoticz.devices('Light Kitchen').switchOff()
elseif (item.isDevice and item.name == 'NSPanel_Widget_1' and item.state == 'On' and domoticz.devices('Light Kitchen').state == 'Off') then -- mirror state from Dz_NSPanel Widget to Dz_device/group
domoticz.log('Dz NSPanel_Widget_1 was turned On so mirror Light Kitchen', domoticz.LOG_INFO)
--domoticz.devices('Light Kitchen').switchOn()
else
-- do nothing
end
-- Widget_2: todo
-- Widget_3: todo
-- Widget_4: todo
-- Widget_5: todo
-- Widget_6: todo
-- Widget_7: todo
-- Widget_8: todo
end -- execute
}
Code: Select all
--Script 2 to mirror Domoticz NSPanel Widgets state to NSPanel_Widgets every 2 minutes --> In case NSPanel is rebooted
return {
on = {
timer = {
'every 2 minutes',
}
},
logging = {
level = domoticz.LOG_INFO,
marker = 'NSPanel - Mirror states Dz_NSPanel_Widgets to NSPanel_Widgets',
},
execute = function(domoticz, timer)
local NSPanel_Widgets =
{
'NSPanel_Widget_1', 'NSPanel_Widget_2', 'NSPanel_Widget_3', 'NSPanel_Widget_4', 'NSPanel_Widget_5', 'NSPanel_Widget_6', 'NSPanel_Widget_7', 'NSPanel_Widget_8', --idx 1001 .. 1008
}
local function MirrorState(dv)
local n = tonumber(dv.idx - 1000) -- as idx Dz widget = 1001, - 1008 = 1 = idx NSPanel Widget, etc.
domoticz.log( 'The '.. dv.name ..' is set to ' .. domoticz.devices(dv.name).state, domoticz.LOG_INFO)
-- Fill in NSPanel IP-address in <IPADDR>
if (domoticz.devices(dv.name).state == 'Off') then -- off. Send http command to NSPanel Widgets to mirror state
domoticz.openURL('http://<IPADDR>/cm?cmnd=nspsend%20{%22ctype%22:%22group%22,%22id%22:%22'.. n ..'%22,%22params%22:{%22switch%22:%22off%22,%22switches%22:[{%22switch%22:%22off%22,%22outlet%22:0}]}}')
elseif (domoticz.devices(dv.name).state == 'On') then -- on. Send http command to NSPanel Widgets to mirror state
domoticz.openURL('http://<IPADDR>/cm?cmnd=nspsend%20{%22ctype%22:%22group%22,%22id%22:%22'.. n ..'%22,%22params%22:{%22switch%22:%22on%22,%22switches%22:[{%22switch%22:%22on%22,%22outlet%22:0}]}}')
end
end
-- domoticz.log('Mirror Widget script was triggered', domoticz.LOG_INFO)
domoticz.devices().filter(NSPanel_Widgets).forEach(function(dv)
MirrorState(dv)
end)
end -- execute
}
Adapt the scripts with your specific information such as devices/groups to control, IP-address NSPanel, etc.
Later I also added a ‘PING NSPanel IPADDR’ to only read/write to NSPanel if it is online:
Domoticz – Setup – Hardware –> Add System Alive Checker (Ping), select NSPanel as Node and e.g. 300 sec Poll interval
In both scripts add after “execute = function(domoticz)”:
Code: Select all
local Ping_NS = domoticz.devices(‘Ping NSPanel’)
if (Ping_NS.state == 'On') then
-- Put all code here
end
This works for me so I hope it is of use to someone...