strandborg / AlsavoCtrl

Alsavo Heat Pump control application
Other
11 stars 1 forks source link

MQTT publish and listen #7

Closed gllmlbrt closed 2 years ago

gllmlbrt commented 3 years ago

Hi, I have been looking for something like this. Would it be possible and not too complicated to run this as a docker container (ideally home assistant add-on) which publishes via MQTT at regular intervals all the parameters? And maybe listens to mqtt payload on various topics for control ?

strandborg commented 3 years ago

In my own setup I am publishing the status values to MQTT by periodically running the following script:

AlsavoCtrl --serial=$ALSAVO_SERIAL --password=$ALSAVO_PASS --address=$ALSAVO_IP --port=$ALSAVO_PORT | python mqpublish.py

The mqpublish.py script:

import time
import sys
import re

def main():
  client = mqtt.Client()
  client.connect("localhost", 1883 , 60)
  client.loop_start()

  for line in sys.stdin:
    m = re.match('(.*)=(.*)', line)
    if m:
      client.publish(m.group(1), payload=m.group(2))

#  time.sleep(2)
  client.loop_stop()

if __name__ == "__main__":
    main()

This does not support setting the status via MQTT, but pull requests are welcome =).

KasperEdw commented 3 years ago

So to get this working in Home Assistant, I would set up the Mosquitto broker and have mqpublish.py publish to it, and set it up as an mqtt sensor? (sorry for asking stupid questions, this is new and im learning as I go)

strandborg commented 3 years ago

My own setup is built using OpenHAB, and I'm not familiar with how Home Assistant works, but that sounds about right. In my OpenHAB setup I use JSON transform to extract the data fields from the MQTT payload, btw.

gllmlbrt commented 3 years ago

So managed to run as advised by KasperEdw inside Nodered and publish all items to MQTT. Works fine. I am confused however aboud the usage of the bitmask 1 and 2, to turn on and off ? What would the command look like ?

strandborg commented 3 years ago

So config params 4 and 5 are bitfields that control multiple settings in one value. For example, if the 2 least significant bits of sys config 1 are '01', then the pump is in heat mode (as opposed to auto, or cooling mode). When setting or clearing one or more bits, you'll have to take the previous value and do logical OR or AND operations on the value before writing it back. For example, in order to turn the heat pump on (bit 5), you'd do something like this:

newVal = prevVal | (1 << 5)

and so on.

gllmlbrt commented 3 years ago

Thanks for taking the time, but unfortunately I am not sure I understand. The command using bitmaks, is dependant on the previous command sent ?

What would the command line look like ?

As example to set the temperature set to say 27deg, I run ./AlsavoCtrl/AlsavoCtrl -s "serialnumber -l "password" 1 270

strandborg commented 3 years ago

You'll have to take the current value of the config and only change the bits you want to modify before writing it back. This is how I scripted On/Off in OpenHAB2 rules. For reference, 0x30 is 110000 in binary and 0xFFFFFFCF is its inverse. So this command switches both bits 5 and 6 on or off depending on the OpenHAB switch setting (even though bit 5 is the on/off switch, the Alsavo Pro app seems to also set bit 6 to zero when turning off the device, so I'm doing the same here).

when
    Item AlsavoOnOff received command
then
    var oldFlags = (AlsavoPoolHeatPump_ConfigFlags.state as Number).intValue
    if(receivedCommand == ON)
    {
        var newFlags = oldFlags.bitwiseOr(0x30)
        executeCommandLine("AlsavoCtrl -s <serialnumber> -l <password> 4 " + newFlags, 15000)       
    }
    else
    {
        var newFlags = oldFlags.bitwiseAnd(0xFFFFFFCF)
        executeCommandLine("AlsavoCtrl -s <serialnumber> -l <password> 4 " + newFlags, 15000)
    }
end
26196008 commented 3 years ago

Is it possible that "4 = System config bitmask 1 (See below)" the bitmask is listed incorrectly? When the pump is off, I get the value 1. When I switch the pump on, I get the value 33. Accordingly, on/off would be bit 6 and not bit 5. Or do I have a mistake in my thinking?

strandborg commented 3 years ago

As you can see from the bit listing, the list uses 0-based indexing, so the first bit is at index 0, and the sixth bit is at index 5.

initB10r commented 2 years ago

I have tested the script "mqpublish.py" from above, but I get the following error: python3 publish.py Traceback (most recent call last): File "publish.py", line 20, in <module> main() File "publish.py", line 7, in main client = mqtt.Client() NameError: name 'mqtt' is not defined

What am I doing wrong?

strandborg commented 2 years ago

I have tested the script "mqpublish.py" from above, but I get the following error: python3 publish.py Traceback (most recent call last): File "publish.py", line 20, in <module> main() File "publish.py", line 7, in main client = mqtt.Client() NameError: name 'mqtt' is not defined

What am I doing wrong?

You are likely missing the mqtt python library, you can install it with pip install paho-mqtt