i have defined a level template in my light, but i always get this error :
example :
ValueError: invalid literal for int() with base 10: ‘170.0’
i think is has something todo with formatting? although if i do it manually in the HA interface, like example 170.0 it works, also 170 works
the value_template is working
also, i know the command is working, because if i do it in a curl script, its working
explenation of the code:
i have a long sensor string, character 36 to 38 , is representing a HEX value of my dimmer, it goes from 00 to 90 in hex, representing 0 to 255 in brightness level, that formula is working…
this is also working : curl state_dimmer1_on: "curl -k POST -d '{\"state\": \"on\",\"attributes\": {\"brightness\": {{states.sensor.Prog.state[36:38]|int(base=16)/90*255}}, \"assumed_state\": true,\"friendly_name\": \"Eethoek Raam\",\"icon\": \"mdi:spotlight\", \"supported_features\": 1}}' https://127.0.0.1:8123/api/states/light.dimmer1?api_password=xx
when accessing the object directly, you can run into issues if the object doesn’t exist. This can happen during startup.
When using the method, that will handle the case of the object not existing and return ‘unknown’ instead of throwing a python error.
Generally using the method is safer. But using the method does not give you access to attributes or items outside the attributes. There is a method to access attributes state_attr('sensor.Prog','friendly_name'). But if you wanted to access last_changed or last_updated you’d need to access the object directly: states.sensor.Prog.last_updated.
Also side note, You should rename your sensor.Prog entity_id to sesnor.prog. The capitalization could cause issues in some components as home assistant treats everything in lowercase.
This isn’t python this is jinja. The int filter can take any string without issue. non-numerical strings return 0 with an int filter applied. There are no try/excepts in jinja either.
Also, please keep in mind that 2 year old posts are typically dead.
Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: invalid literal for int() with base 10 . With Python2.x , int(str(3/2)) gives you “1”. With Python3.x , the same gives you (“1.5”): ValueError: invalid literal for int() with base 10: “1.5”.