i have a modbus tcp/ip calorimeter from which i get values 1 or 0..
Each time calorimeter reaches 10kw it signals "0" otherwise its 1
In python i want script to run all the time and record / calculate each 10 kilowats.
Example:
Current kw: 260000 (manually read it from calorimeter and enter it to python)
Signal "0" (reached 10kw) - time 10:23
Signal "0" (reached 10kw) - time 10:35
Signal "0" (reached 10kw) - time 10:41
current kw: 260030
times reached 10kw: 3x
Problem is that when calorimeter reaches 10 kilowats it signals multiple zeros at same time and python sums all of them instead of only 1
Example:
1
1
1
1
0 (calorimeter reaches 10kw)
0
0
0
0
1
1
1
What should i do so i could read only first zero for each signal or how else could i calculate this?
Current code:
from pymodbus.client import ModbusTcpClient
from datetime import datetime
IP = '192.168.22.95'
client = ModbusTcpClient(IP)
current_kw = 260000
while True:
a = client.read_holding_registers(0, 1)
b = a.registers
if b[0]==0:
date = datetime.now()
current_kw += 10
print (current_kw, "kW")
Good way to solve this would be by keeping the status from previous reading and adding 10 kW only if previous state was 1.
Set the new variable
client = ModbusTcpClient(IP)
last_state = 1
current_kw = 260000
And check for change in status in the if
if b[0]==0 and last_state==1:
Don't forget to set the variable on the end of while loop to last read state.
last_state=b[0]
Final code would look like this.
from pymodbus.client import ModbusTcpClient
from datetime import datetime
IP = '192.168.22.95'
client = ModbusTcpClient(IP)
last_state = 1
current_kw = 260000
while True:
a = client.read_holding_registers(0, 1)
b = a.registers
if b[0]==0 and last_state==1:
date = datetime.now()
current_kw += 10
print (current_kw, "kW")
last_state=b[0]