Level_template (light) => ValueError: invalid literal for int() with base 10: '170.0'


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}}'

here is my template light

  friendly_name: "Eethoek Raam"
  value_template: "{{ states.sensor.Prog.state[36:38] != '00' }}" 
  level_template: "{{ states.sensor.Prog.state[36:38]|int(base=16)/90*255 }}"

yes, that gives the same error

Might just need a | int at the end to remove the decimal place.

EDIT: Just tested it out. you definitely just need the | int. The decimal place then 0 is causing your issues.

  level_template: "{{ ( states('sensor.Prog')[36:38] | int(base=16) / 90 * 255 ) | int }}"

huh, thats indeed working, ?..

why is this code not working then?

level_template: "{{ states.sensor.Prog.state[36:38]|int(base=16)/90*255 | int }}"

Because the int is being applied to the 255 only, not the entire equation.

ok, and would this be working then ? changed the ( )

level_template: "{{ (states.sensor.Prog.state[36:38]|int(base=16)/90*255) | int }}"

yep, it should return 170 as a string instead of 170.0 as a string

ok, and can you tell me why you write : or sometimes other people


and i use : states.sensor.Prog.state[36:38]

one access the object directly:


The other uses a method to access the object


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.

ok, gonna change it, thnx for the explenation

and , thnx, its confirmed working now

1 Like

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”.