Blockly

From Domoticz
Jump to: navigation, search

Introduction

Blockly is an simple way to get started with automation and scripting in Domoticz. It's not as powerful as LUA and the other types of scripting that can also be used but an easy start
Blockymonday.PNG
This is how a script could look.
Blockly framework is developed by google. Wikipedia

Getting started

First you need to have set up your system with some sensors, switches and such.

Go to Setup -> More Options -> Events
Events empty.PNG
You should see something like this now (maybe not any saved events)

In top right corner

  • Give your script a name
  • Choose Blockly as scripting language
  • Select when this script should be triggered. (choose device for now)
    Blockly trigger choice.png

From top left corner

  • Start scripting by dragging in an "If/Do" block from the control area. The "If" part is your set of criteria that will trigger what happens under "Do".
  • Drag a compare block to the "If" part of your block. (top one under logic).
  • Drag a device from the devices area to the first part of the compare block. (You can then change the device from the dropdown.)
  • Drag the the "On" block to the second.

In the "Do" connection you can either place a set block from the logic area and/or a notification block from the messages area.

  • So drag a "Set block to the "do" section
  • Drag a device and On/Off block to the set block.

Blockly example duplicate on switch.PNG
You should have a block looking something like this now. Quite unusable but a start

  • Choose Event active and push Save.

Now you probably want to turn off the device too with some other event. Well there is an "Else-if statement for that where you can create multiple If statements.


Logic blocks

Control

Control block to contain a action/reaction
Blockly control.PNG

If Else

If you need more than one "if" this is what you use. You can't stack "If" control blocks but you can make an extended block by using the Else-if.
Open the Blue gear popup and drag the "Else if" to the bottom of "If" and you get an extended control block.
Blockly control elsif conf.PNG

Notes!
Else-if just works as stacked "If". All of the comparison rules are executed and not as in other languages as one or the other.
You can not stack multiple "If"-statements after eachother. You need to use an "Else-if" block.

Logic

Logic blocks are used to compare and set values in for example an "If" block
Blockly Logic.PNG

Compare
Blockly logic comparator.PNG
This block setup checks if Temperature sensor "Badrum" is 10 or above and if either "Nexa ch3" is on or "Nexa ch4" is not at level 0 to be true.
Pseudo code. (Badrum >= 10) AND ( (Nexa_Ch3==On) OR (Nexa_ch4!=0) )
This can be used in the "If-Block" under "If"
Set
Blockly logic set.PNG
This block sets "Nexa ch4" to 23% dim level and after 10 seconds "Nexa ch3" to On This can be used in the "If-Block" under "Do"
Only use "Set" for switchable/dimmable devices.

Time

Check if time events are true
Blockly time.PNG

Compare
First row triggers on Sunrise.
Second row triggers all day between 00:00 and 15:00. it should be combined with another condition if you don't want it to trigg continuously.
Blockly time compare.PNG

Messages

Blockly messages.PNG

Send message
Blockly message send.PNG
You can use expressions in your notifications/email and SMS messages like: (Where 1234 is the Idx of the sensor)
Example. "My temperature is {{temperaturedevice[1234]}} degrees, and the humidity is {{humiditydevice[1234]}} %"
Possible expressions: temperaturedevice, dewpointdevice, humiditydevice, barometerdevice, utilitydevice, weatherdevice, raindevice, rainlasthourdevice, uvdevice, winddirdevice, windspeeddevice, windgustdevice, variable

Security

Blockly security.PNG
The Security panel is only an advanced button with a couple of states. The rest of the logic for an alarm needs to be written by you. This device gives you the status of the security panel.

User Variables

You can set up User_variables (in Setup -> More options -> User Variables) and access them in blockly.)
To use a variable as an On/Off type set it up as String and give it a current value of "Off"
Blockly user vars.PNG

Debug/Log

This block is very useful for debug. Put a text block in the debug block or a switch/sensor or so and print it to the log to see what it does or what the status of a sensor is.
Blockly log.PNG

Devices

Switches

Blockly devices switch.PNG
Switch devices can be used with the "Set" block and also compare.

Temperature

Blockly devices temp.PNG

Humidity

Blockly devices hum.PNG

Dewpoint

Barometer

Weather

Utility

Scenes/groups

Blockly devices scene.PNG

Cameras

Blockly devices cameras.PNG

Setpoints

Blockly devices setpoint.PNG

Zwave Alarms

Examples

Set a dummy switch called "Post" to On only when magnet sensor "$POSTBOX" changes to On
Post indicator needs to be reset manually when postbox is emptied. (Using the $-sign in the device name hides the magnetic device from the switches view) Set trigger to Device
Blockly example postbox.PNG
Set a Dummy Dawn Sensor(called Natt) based on Sunset/Sunrise
Set trigger to Time
Blockly example dawn.PNG
Let Kodi control some lights depending on if it's Paused, playing Video or Audio
Set trigger to Device
Blokly example kodi.PNG
Utilize S2 momentary switch on fibaro dimmer2($KökS2) to toggle a group of lights
Set trigger to Device
Blockly example s2 switch group.PNG
Script uses a variable called kitchenswitch (string) to toggle between states.

More examples