-
Notifications
You must be signed in to change notification settings - Fork 58
-
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 ...
Beta Was this translation helpful? Give feedback.
All reactions
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
-
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))
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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.
Beta Was this translation helpful? Give feedback.
All reactions
-
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 !
Beta Was this translation helpful? Give feedback.