Automation Template with TRADFRI wireless dimmer

I’m trying to configure my TRADFRI wireless dimmer to control the volume on my Chromecast, however, I’m having a bit of difficulty with the data template.

How I want it to work is if the dimmer “brightness” is set to ‘0’, it mutes the media player, but if it’s got a value it sets the volume.
The tricky part on top of that is that the dimmers value is from 0-255, but the volume value needs to be from 0.0-1.0, so I’ve got it getting the value and dividing it by 255, I’m setting it to float, and I’m setting it to round to 2 decimal places (I think. I can’t get it to work to test it).

Could someone please take a peek at my following code and see if you can suggest what I need to do to get it to work how I’m after? I know it’s not a great dimmer, but I’m just playing around to learn some stuff. :smile:

- alias: "Set The-Ultra Volume"
 trigger:
   platform: state
   entity_id: sensor.0x000d6ffffe2ac296_brightness
 condition:
   # Condition #1: The-Ultra is on
   condition: template
   value_template: "{{ not is_state('media_player.the_ultra', 'off') }}"
 action:
   # Action 1: Mute The-Ultra
   # Action 2: Set The-Ultra Volume
   service_template: >
     {% if trigger.to_state.state|int == 0 %}
       media_player.volume_mute
     {% else %}
       media_player.volume_set
     {% endif %}
   # Action 1: Mute The-Ultra
   # Action 2: Set The-Ultra Volume
   data_template: >
     {% if trigger.to_state.state|int == 0 %}
       entity_id: media_player.the_ultra
       is_volume_muted: True
     {% else %}
       entity_id: media_player.the_ultra
       volume_level: "{{ (trigger.to_state.state|float / 255) | round(2) }}"
     {% endif %}

Ok so apparently you can’t put 2 things in a data template line like I did. The following code got my volume working, and I need to set a second automation for muting

- alias: "Set The-Ultra Volume"
  trigger:
    platform: state
    entity_id: sensor.0x000d6ffffe2ac296_brightness
  condition:
    # Condition #1: The-Ultra is on
    condition: template
    value_template: "{{ not is_state('media_player.the_ultra', 'off') }}"
  action:
    # Action 1: Set The-Ultra Volume
    service: media_player.volume_set
    data_template:
      entity_id: media_player.the_ultra
      volume_level: "{{ (trigger.to_state.state|float / 255)|round(2) }}"

So to do this with 2 automations, this is my code:

### Mute The-Ultra ################################################
- alias: "Mute The-Ultra"
  trigger:
    platform: state
    entity_id: sensor.0x000d6ffffe2ac296_brightness
  condition:
    condition: and # link with and
    conditions: # Multiple conditions
      # Condition #1: Trigger state is 0
      - condition: template
        value_template: "{{ trigger.to_state.state|int == 0 }}"
      # Condition #2: The-Ultra is on
      - condition: template
        value_template: "{{ not is_state('media_player.the_ultra', 'off') }}"
  action:
    service: media_player.volume_mute
    data:
      entity_id: media_player.the_ultra
      is_volume_muted: True

### Set The-Ultra Volume ###########################################
- alias: "Set The-Ultra Volume"
  trigger:
    platform: state
    entity_id: sensor.0x000d6ffffe2ac296_brightness
  condition:
    condition: and # link with and
    conditions: # Multiple conditions
      # Condition #1: Trigger state is not 0
      - condition: template
        value_template: "{{ trigger.to_state.state|int != 0 }}"
      # Condition #2: The-Ultra is on
      - condition: template
        value_template: "{{ not is_state('media_player.the_ultra', 'off') }}"
  action:
    # Action 1: Set The-Ultra Volume
    service: media_player.volume_set
    data_template:
      entity_id: media_player.the_ultra
      volume_level: "{{ (trigger.to_state.state|float / 255)|round(2) }}"

But having only the first one works fine.