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

int error, why ? #734

Answered by ALERTua
melmager asked this question in Q&A
Jul 1, 2025 · 2 comments · 2 replies
Discussion options

how to work with Integer ?

Exception in <file.akku_steuerung.grid_strom> line 5:
akku = int(state.get('sensor.ms_a2_akku_ladung')) ^ ValueError: invalid literal for int() with base 10: '-199.9'

akku = int(state.get('sensor.ms_a2_akku_ladung'))
or
akku = int(sensor.ms_a2_akku_ladung)

both i get the same literal error ...

You must be logged in to vote
sensor_state = state.get('sensor.ms_a2_akku_ladung')
sensor_state_float = float(sensor_state)
sensor_state_round = round(sensor_state_float, 0)
sensor_state_int = int(sensor_state_round)
# or just
akku = int(round(float(state.get('sensor.ms_a2_akku_ladung')), 0))

Replies: 2 comments 2 replies

Comment options

sensor_state = state.get('sensor.ms_a2_akku_ladung')
sensor_state_float = float(sensor_state)
sensor_state_round = round(sensor_state_float, 0)
sensor_state_int = int(sensor_state_round)
# or just
akku = int(round(float(state.get('sensor.ms_a2_akku_ladung')), 0))
You must be logged in to vote
2 replies
Comment options

so pyscript is not fully Python.
Are there any documentation I could read?
That was my second simple question because I stumbled upon some inconsistencies regarding Python.

Comment options

You are trying to convert a negative float string to int. You will have the exact same error in your Python interpreter.

>>> int('-199.9')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '-199.9'

My code converts this string into float, then rounds it, then converts it to int. Which would work both in your Python interpreter and in pyscript scripts.

Answer selected by melmager
Comment options

ok my fault
in Python i must also
x = "-199.4"
y = float(x)
print(int(y))
Memo to me: sensor data are strings :-)
so pretest in PyCharm with String as Input !

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
Category
Q&A
Labels
None yet
2 participants

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