Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Read modbus on USB0 & add output to a variable #280

Unanswered
IoTPlay asked this question in General
Discussion options

Hi, complete newbie, installed pyscript tonight, point me in the right direction, and go easy please!

I have a Python scrpt that talks via a USB dongle to a rs485 / modbus network, to an 8CH relay, with a script to switch a relay on/off, and another script to read if it did happen. I registered an app in pyscript, I think the modbus_read and modbus_write shold be seperate apps, but not sure yet, could possibly be combined - if I can write a variable to the .py that first sends a command for a CH to be switched on/off, and then read the USB port, but not that far yet. (Right now I have .py for writing, and another for reading which works). Currently the read python script runs contiously - and reports back the state if any write happened.

I guess I need to register an entity_id in HASS, where the state of the modbus device register is populated into. I am unclear which of this would be a @state_trigger, or @service, and do not find examples. Could someone point me in the right direction please?

You must be logged in to vote

Replies: 2 comments 1 reply

Comment options

You're entering in to the realm of things that pyscript doesn't really make any easier than a regular integration, as, I imagine, ideally, these would be in the switch domain.

With that being said, you can use state.set() to create a sensor that shows the most recently read state of the relay. And you can use the @service decorator to register service calls to handle turning the relays on and off.

The trickiest part will be, I assume, that both read and writing need to happen on the same device, and I don't think you can have two processes accessing the same device file. So... you're likely going to want to do this in one "script" so that you can read constantly and write when needed. Additionally, you'll, ideally, want to find a library that can open the device in a non-blocking way and use a callback when new data is available. This way, it can run within Home Assistant's event loop.

Good luck. And be sure to share the final result with us!

You must be logged in to vote
1 reply
Comment options

Okay, getting there! Got "state.set" working reading the USB port, into an entity_id "pyscript.modbus1" - which works. Also got both the writing and the reading in one python script.
In the stand-alone python script, I used to have command line variables to state which a) device, b) which relay I wanted triggered, and c) open or closed variable.

How can pass say an array with these variables to the python script? I assume with some action that has the array, that the python script reads? Gonna see how I can do it with the @service decorator.

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /