Template not working

I have this automation:

- alias: Cubo - Cambia Volume
  trigger:
    platform: event
    event_type: cube_action
    event_data:
      entity_id: binary_sensor.cube_158d00010df2f4_2
      action_type: rotate
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.salotto_audio
        volume_level: >
          {% if trigger.event.data.action_value | float > 0 %}
            {{  states.media_player.salotto_audio.attributes.volume_level | float + 0.5 }}
          {% else %}
            {{  states.media_player.salotto_audio.attributes.volume_level | float - 0.5 }}
          {% endif %}
    - service: input_number.increment
      entity_id: input_number.radio_volume

I use it to change radio volume with the cube actions, but i really don’t know how to change it to increment the volume value when rotating to the right and lower the volume when rotating to the left.
Can somebody help me ?

I think you are going to have to track the binary_sesnor.cube_158d00010df2f4_2.attributes.action_value as a template then compare the previous value to the new value:

sensor:
  - platform: template
    sensors:
      cube_rotation_action_value:
        value_template: "{{ state_attr('binary_sensor.cube_158d00010df2f4_2','action_value') }}"

then automate off that:

- alias: Cubo - Cambia Volume
  trigger:
    platform: state
    entity_id: sensor.cube_rotation_action_value
  action:
    - service: media_player.volume_set
      data_template:
        entity_id: media_player.salotto_audio
        volume_level: > 
          {% if trigger.from_state.state | float < trigger.to_state.state | float %}
            {{  states.media_player.salotto_audio.attributes.volume_level | float + 0.5 }}
          {% else %}
            {{  states.media_player.salotto_audio.attributes.volume_level | float - 0.5 }}
          {% endif %}
    - service: input_number.increment
      entity_id: input_number.radio_volume

This is assuming alot. First i’m assuming that you actually have an attribute ‘action_value’ in the object itself (and not just in the event). Second it assumes that you are only using that action.

Ok, i’ll add that sensor, but then how to change the automation also for the service “input_number.decrement” so to have the input_number value of the volume will decrease or increase depending from the rotation?

I’m not sure, I’d have to see the object and how it works. What does the binary_sensor.cube_158d00010df2f4_2 look like on the states page?


Here it is

Ouch, ok, this might get difficult then. That sensor doesn’t store the event data. so you’ll probably need to make an automation based on the event and store the information in an MQTT sensor so that you can reference the last event data.

Pretty much you need to know where it was last in order to determine the rotation direction. At least that’s my assumption. Then you could do math to find out if the rotation direction is negative or positive.

Hmm… really too complicated for me… I think i will stay as i am now…without complicating too much. :sweat_smile:
BTW thanks always for your support…

I mean you could also do it with a python script. I guess a question I should have asked: Is the rotation in the event based on the previous value or does it just give a position it’s moving to?

I checked and it says only “rotate” as last_action and not in which direction, so i think it will be impossible to do what i was asking.

Yeah, i get that, but I"m talking about this value:

trigger.event.data.action_value

in the event data. Does that value have a numerical value that is related to a position 0 to 360 or is it related to the last rotation position?

Example: We are at position 52 (But the user doesn’t know this). The rotation moved 40 degrees clockwise. FYI clockwise in math is typically a negative rotation.

does trigger.event.data.action_value return

12 (The next position)

or

-40 (The move to the next position)

Tell me how to check it and i’ll do…

I mean, that would be seen in your event data with your original automation that you were using. I have no idea how the device works so i’m taking stabs in the dark here.

Ok… i will do a check and let you know…