Need help with a input number automation

basically when my LG TV volume changes I want the Sonos Beam Soundbar to change too.

- alias: 'Sonos Playbar Volume Changed'
  trigger:
    platform: state
    entity_id: media_player.livingroom_tv
  condition:
    - condition: template
      value_template: '{{ states.input_number.living_room_volume.state != states.media_player.livingroom_tv.attributes.volume_level}}'
  action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.living_room_volume
        value: "{{ states(media_player.livingroom_tv.attributes.volume_level * 100) | int }}"        
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.tv_sonos
        volume_level: "{{ states.input_number.living_room_volume.state }}"

input_number.living_room_volume changes with the volume on the TV but has a floating point and I think this is why the Sonos never updates. how do I make the number a integer with no decimal places?

I think this is the problem. The “* 100” is inside the states call. Should be:

{{ (state_attr('media_player.livingroom_tv', 'volume_level') | float * 100) | round(0) | int }}

Edit: Not sure if the int filter rounds, or just truncates, so I added a round(0) in there too. So this line will take the ‘volume_level’ attribute from the media_player.livingroom_tv entity, and first interpret it as a float, then multiply by 100 to get a number > 1, then round to the nearest integer, and finally make sure it’s typed as an integer.

Configuration validation fails with

{{ (state_attr('media_player.livingroom_tv', 'volume_level') | float * 100) | round(0) | int }}

So I added “” around the lot but still the value is not an integer, 3.0 or 99.0 etc

"{{ (state_attr('media_player.livingroom_tv', 'volume_level') | float * 100) | round(0) | int }}"

The template seems sound. You may have to divide by 100 again when you set the volume level of the other media_player. All media_player components have a volume level that ranges from 0 - 1.

Upon reflection, you may be able to get rid of the input_number altogether, if you don’t want it, and just set one media player volume level to the other directly.

don’t worry turn out the sonos needed it as 0.99 not 99. The issue was further down in the code

- service: media_player.volume_set
      data_template:
        entity_id: media_player.tv_sonos
        volume_level: "{{ states.input_number.living_room_volume}}"

when it should have been

- service: media_player.volume_set
      data_template:
        entity_id: media_player.tv_sonos
        volume_level: "{{ states.input_number.living_room_volume.state }}"

states(‘input_number.living_room_volume’)
Is shorter still, and is the recommended / preferred method as this avoids some issues at start-up with invalid values

thanks for this, when I have the will power | wi|I will be changing my code.