Python and ups 18650

Help to find the error! I can not display the battery charge on the site map!

ups_18650.py

#!/usr/bin/env python
import struct
import smbus
import sys
import time


def readVoltage(bus):

        "This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
        address = 0x36
        read = bus.read_word_data(address, 2)
        swapped = struct.unpack("<H", struct.pack(">H", read))[0]
        voltage = swapped * 1.25 /1000/16
        return voltage


def readCapacity(bus):
        "This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
        address = 0x36
        read = bus.read_word_data(address, 4)
        swapped = struct.unpack("<H", struct.pack(">H", read))[0]
        capacity = swapped/256
        return capacity


bus = smbus.SMBus(1)  # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

while True:

 print "++++++++++++++++++++"
 print "Voltage:%5.2fV" % readVoltage(bus)

 print "Battery:%5i%%" % readCapacity(bus)

 if readCapacity(bus) == 100:

        print "Батарея ЗАРЯЖЕНА"

 if readCapacity(bus) < 20:

        print "Батарея РАЗРЯЖЕНА"
 print "++++++++++++++++++++"
 time.sleep(2)

.things

exec:command:ups_18650     [command="sudo python /home/openhabian/scripts/UPS_18650.py"]

.sitemap

Text item=ups_18650

.items

String ups_18650 "Voltage: [%5.2f]"    {channel="exec:command:ups_18650:run"}

These filenames do not match

  1. use output channel for result of command.
  2. either use a second item to trigger the command (through run channel) or use interval parameter:

thing:

exec:command:ups_18650     [ command="sudo python /home/openhabian/scripts/ups_18650.py", interval=120 ]

item:

String ups_18650 "Voltage: [%5.2f]"    {channel="exec:command:ups_18650:output"}

fixed the location of the file, but not working.

.things

exec:command:ups_18650     [command="sudo python /home/openhabian/UPS_18650.py, interval=120"]

.items

String ups_18650 "Voltage: [%5.2f V]"    {channel="exec:command:ups_18650:output"}

log

2019-03-24 20:01:48.428 [.ItemChannelLinkAddedEvent] - Link 'ups_18650-exec:command:ups_18650:output' has been added.

Please take a look at the thing, yours:

exec:command:ups_18650     [command="sudo python /home/openhabian/UPS_18650.py, interval=120"]

vs. mine:

exec:command:ups_18650     [ command="sudo python /home/openhabian/scripts/ups_18650.py", interval=120 ]

исправил, но не заработало. в лог теперь вот такая ошибка.

2019-03-27 13: 07: 26.616 [WARN] [hab.binding.exec.handler.ExecHandler] - Не удалось преобразовать ответ, так как услуга translationService типа 'REGEX' недоступна

That’s strange as I can’t see any REGEX…

sorry

2019-03-27 13:12:32.022 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value 'python: can't open file '/home/openhabian/UPS_18650.py': [Errno 13] Permission denied

Ah.
Well, I guess you did not setup sudo to allow the user openhab to execute commands without password yet.

As sudo with NOPASSWD set is a nasty hack :wink: and a bad idea, maybe you could allow openhab to access the bus?

2019-03-27 13:13:35.169 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn't transform response because transformationService of type 'REGEX' is unavailable

2019-03-27 13:13:35.178 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified' of item ups_18650 with format '%5i%%': Conversion = 'i'

==> /var/log/openhab2/events.log <==

2019-03-27 13:13:35.181 [vent.ItemStateChangedEvent] - ups_18650 changed from python: can't open file '/home/openhabian/UPS_18650.py': [Errno 13] Permission denied

python: can't open file '/home/openhabian/UPS_18650.py': [Errno 13] Permission denied to 

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

sudo: no tty present and no askpass program specified

what should be done? please tell me!

Yep, as stated before, it does not suffice to write sudo in front of a command.

GNU/Linux sudo is used to extend permissions to root, so sudo will ask for a password every time it’s executed. The user openhab (this is the one that openHAB is using) needs permission to use the sudo command without typing a password.

added in visudo openhabian ALL=(ALL) NOPASSWD: ALL, but the problem is not solved

NOT openhabian, the user is openhab!
User openhabian is a common user to get command shell access.
User openhab is a user without regular shell access. this user is used only to run openHAB.

the user openhab also did not give a result…