LUA - Functions for hw control

Moderator: Telldus

Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

LUA - Functions for hw control

Post by Kalarne »

I know you are working on documentation for LUA, but I am a little bit impatient.
Is there currently a list of calls that can be done from LUA to manage sensors and devices that you have connected?

In the upcoming documentation, it is important that the architecture and overall philosophy is described. Not only documentation in a detailed leval.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

Not a full documentation but here is a list of functions that is called in the script.

onDeviceAdded(device)
Called every time a device is added/created

onDeviceRemoved(deviceId)
Called every time a device is removed. The parameter deviceId is the old device id. The ref to the device is no longer available

onDeviceStateChanged(device, state, stateValue)
Called every time the state of a device is changed.

onInit()
Called when the script is loaded

onRf433RawData(msg)
Signal send on any raw data received from 433 receiver. Please note that the TellStick must contain a receiver for this signal to be sent. Not all models contains a receiver.

onSensorValueUpdated(device, valueType, value, scale)
Called every time a sensors value is updated.

onZwaveMessageReceived(device, rxStatus, cmdClass, cmd, data)
Called for every Z-Wave message received from a node in the network. For documentation of the command classes and data, please see official documentation from Sigma Designs (currently only available under NDA).
Micke Prag
Software
Telldus Technologies
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

Thanks Micke,

I used this for an event handler that works well and could take care of incoming data and events.
The only thing I miss now is knowledge about how to change setpoints on my Danfoss Zwave thermostat valves. I get four different messages from them, but don’t know how to update them with Lua. Is there anyone out there who can help me with this?
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

You can try something like this:

Code: Select all

local COMMAND_CLASS_THERMOSTAT_SETPOINT = 0x43
local SETPOINT_TYPE_HEATING = '1'
local deviceManager = require "telldus.DeviceManager"

function onInit()
	local device = deviceManager:findByName("Danfoss")
	setDanfossTemperature(device, 25)
end

function setDanfossTemperature(device, temperature)
	if (device:typeString() ~= 'zwave') then
		print("Device %s is not a Z-Wave device", device:name())
		return
	end
	-- Get the raw zwave node
	local zwaveNode = device:zwaveNode()
	-- Extract the thermostat setpoint command class
	local cmdClass = zwaveNode:cmdClass(COMMAND_CLASS_THERMOSTAT_SETPOINT)
	if (cmdClass == nil) then
		print("Device %s does not support THERMOSTAT_SETPOINT", device:name())
		return
	end
	-- Set new value to be sent the next time the device is awake
	cmdClass:setSetpoint(SETPOINT_TYPE_HEATING, temperature)
end
Micke Prag
Software
Telldus Technologies
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

Thanks,
This worked fine!

Another question;

By the "onZwaveMessageReceived" I get four messages every time the Danfoss themostat wakes up. One of them contains battery status.
To get battery status of the thermostat, do I have to wait for the next time the device wakes up, or is this stored somewhere else and can be retrieved by some "get" call?
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

Test this code:

Code: Select all

function printBattery(device)
	local battery = device:battery()
	print("Battery level: %s", battery.level)
end
A warning though. Unfortunately this API is not stable and will break in future firmware versions.
Micke Prag
Software
Telldus Technologies
kamsvag
Posts: 67
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by kamsvag »

Ok, so I'm new to LUA. Could we build a lua script that when a device state is changed we send a message containing device Name and new current state to a server?

I've been working on a python script that pulls a list of all devices and their states every 2 seconds and compares it to previous. This is not very efficient though. Would be better to have the Tellstick send the info at update.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

@kamsvag: Yes and no. Its not currently possible but this is on our roadmap.
Micke Prag
Software
Telldus Technologies
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

Look at what Micke Prag wrote 7 november above.
"onDeviceStateChanged" will do what you want, but as far as I know, there is for the moment no function to send a message about whats happening to outside the sandbox in tellstick. Waiting for that.
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

When I do "http://192.168.1.184/lua?script=Testis.lua", my program Testis doesn't start. I just got the program opened in the editor and I have to start it manually.
Is there a way to start the program, and to send a parameter to the program using the http call?
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

Scripts are always running. They cannot be started or stopped.
Micke Prag
Software
Telldus Technologies
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

Thanks,
I understood that event handling scripts where waiting in affinity, but thought that scripts for special actions, e.g. turning on the heating, could be started from outside, do its duty and then die.
But obviously, all scripts have to be running and waiting to be awakened.
When can we test signals from outside to lua?
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

Kalarne wrote:When can we test signals from outside to lua?
What do you mean? Could you elaborate?
Micke Prag
Software
Telldus Technologies
Kalarne
Posts: 26
Joined: Fri Mar 17, 2023 9:45 am

Re: LUA - Functions for hw control

Post by Kalarne »

For example send a message with a new setpoint value from my PC or IPhone that could be catched up by a lua script for changing temperature setpoint on my eight Danfoss Zwave thermostat valves. Or ask all battery driven Zwave units about battery status and send a report back to the PC/Mobile.
micke.prag
Site Admin
Posts: 2243
Joined: Fri Mar 17, 2023 9:45 am
Location: Lund
Contact:

Re: LUA - Functions for hw control

Post by micke.prag »

Sorry. I don't know when this will be available.
Micke Prag
Software
Telldus Technologies
Post Reply