Try (untested);
The below python script. For this example I call it modbustemp.py and I use that further below
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import minimalmodbus, serial, time, sys, system
### Settings
# Can be https as well. Also: if you use another port then 80 or 443 do not forget to add the port number.
pimatic_server = 'localhost'
# user and password. I prefer a special posting user having a role of varposter with only "variables": "write" in the varposter role. Rest to "none" or false.
pimatic_user = 'username'
pimatic_pass = 'password'
instrument = minimalmodbus.Instrument('/dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0', 1) # port name, slave address (in decimal)
instrument.serial.baudrate = 9600 # Baud
instrument.serial.bytesize = 8
instrument.serial.parity = serial.PARITY_EVEN
instrument.serial.stopbits = 1
instrument.serial.timeout = 1
instrument.address = 10 # this is the slave address number
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
### Initialisation
pim_user_pass = pimatic_user + ':' + pimatic_pass
curl_prefix = 'curl --insecure -X PATCH --header "Content-Type:application/json" --data \'{"type": "value", "valueOrExpression": "'
pim_server_url = pimatic_server + '/api/variables/'
instrument.write_register(307, 3, 1, 6)
time.sleep(2)
### Actions
if (len(sys.argv) < 2): # python script is first argument
print("\n\nError!! You did not specify a temperature\n\n")
sys.exit()
print("temperature specified: " + str(sys.argv[1]))
# first make it a float to be able to also pass values like 24.5
# if not wanted or possible use new_temp = int(sys.argv[1])
set_temp = float(sys.argv[1])
instrument.write_register(205, set_temp, 0, 6) # Registernumber, value, number of decimals for storage
time.sleep(2)
instrument.write_register(205, set_temp, 0, 6) # Registernumber, value, number of decimals for storage
time.sleep(5)
check_temp = instrument.read_register(185, 1) # Registernumber, number of decimals
print(check_temp)
os.system(curl_prefix + check_temp + '"}\' --user "' + pim_user_pass + '" ' + pim_server_url + 'check_temp')
you need to change the variables pimatic_server, pimatic_user and pimatic_pass to your local settings.
in linux do a chmod a+x modbustemp.py
I assume you already have rules and buttons but I wanted to make this example as complete as possible (lot of copy/paste from my config for a likewise situation
)
In your config.json you make 2 devices
{
"id": "ModBus",
"\\": "This variables Device displays the set_temp and the check_temp",
"name": "ModBus",
"class": "VariablesDevice",
"variables": [
{
"name": "settemp",
"expression": "$set_temp",
"type": "number",
"discrete": false,
"acronym": "Set temp"
},
{
"name": "checktemp",
"expression": "$check_temp",
"type": "number",
"discrete": false
"acronym": "Check temp"
}
]
},
{
"id": "PlusMinButtons",
"\\": "This device shows two buttons, one with -1 and one with +1",
"name": "Adapt ModBus temperature",
"class": "ButtonsDevice",
"buttons": [
{
"id": "OneDown",
"text": "-1"
},
{
"id": "OneUp",
"text": "+1"
}
]
},
You create 2 rules:
(and of course set the /<path_to_python_script>/
to the correct path
{
"id": "OneUp-button-pressed",
"name": "OneUp-button-pressed",
"rule": "if OneUp is pressed then set $set_temp to $set_temp+1 and execute \"/<path_to_python_script>/modbustemp.py $set_temp\"",
"active": true,
"logging": true
},
{
"id": "OneDown-button-pressed",
"name": "OneDown-button-pressed",
"rule": "if OneDown is pressed then set $set_temp to $set_temp-1 and execute \"/<path_to_python_script>/modbustemp.py $set_temp\"",
"active": true,
"logging": true
},
And 2 variables
{
"name": "set_temp",
"value": 0
},
{
"name": "check_temp",
"value": 0
},
If you press one of the buttons the temperature will be raised by one or lowered by one and sent to the modbus. The check_temp will return.
what you could do is set a default value to set_temp upon startup like:
{
"id": "set_modbus_set_temp_to_default",
"name": "set_modbus_set_temp_to_default",
"rule": "if pimatic is starting then set $set_temp to 24",
"active": true,
"logging": true
},