Help with "volume level" in Node Red

I want to transform this automation:

- alias: Set Radio Volume
  initial_state: true
  trigger:
    platform: state
    entity_id: input_number.volume
  action:
    service: media_player.volume_set
    data_template:
      entity_id: '{% if is_state("input_select.radio_player", "Birou") %} media_player.bedroom_speaker
        {% elif is_state("input_select.radio_player", "Dormitor") %} media_player.dormitor_4
        {% elif is_state("input_select.radio_player", "Baie") %} media_player.wifi_icybox
        {% elif is_state("input_select.radio_player", "Living") %} media_player.blu_ray_home_theatre_system
        {% elif is_state("input_select.radio_player", "Peste tot") %} group.media
        {% elif is_state("input_select.radio_player", "PC") %} media_player.squeezelite_3
        {% endif %}

        '
      volume_level: '{{  states.input_number.volume.state  }}'

from yaml in NR

I tried something simple

[{“id”:“233f0dcd.0c5962”,“type”:“inject”,“z”:“470a6334.f5c6fc”,“name”:“”,“props”:[{“p”:“payload”},{“p”:“topic”,“vt”:“str”}],“repeat”:“”,“crontab”:“”,“once”:false,“onceDelay”:0.1,“topic”:“”,“payload”:“”,“payloadType”:“date”,“x”:180,“y”:1540,“wires”:[[“391b3805.56b338”]]},{“id”:“391b3805.56b338”,“type”:“api-call-service”,“z”:“470a6334.f5c6fc”,“name”:“Vol. set”,“server”:“60dbe2c9.20830c”,“version”:1,“debugenabled”:true,“service_domain”:“media_player”,“service”:“volume_set”,“entityId”:“media_player.bedroom_speaker”,“data”:“{ "volume_level": ‘{{ states.input_number.volume.state }}’ }”,“dataType”:“json”,“mergecontext”:“”,“output_location”:“”,“output_location_type”:“none”,“mustacheAltTags”:false,“x”:460,“y”:1540,“wires”:[]},{“id”:“60dbe2c9.20830c”,“type”:“server”,“name”:“Home Assistant”,“legacy”:false,“addon”:true,“rejectUnauthorizedCerts”:true,“ha_boolean”:“y|yes|true|on|home|open”,“connectionDelay”:true,“cacheJson”:true}]

but I get an error:
image

I’m interested in the player taking the value of an input number. This way I could control the volume very easily.


I tried different combinations in the “data” field … without effect.
{“volume_level”:states.input_number.volume.state } …

I had a couple of automations using the ‘volume_set’ service which also broke recently.

I can set values in developer tools if I simply set a number (e.g. volume_level: 0.5), but I get errors if I try to use any kind of template (even as simple as volume_level: ‘{{ 0.5 }}’ ). I was using it before to add/subtract 0.2 from the current level, which was why a template was needed.

Are you able to run it in NR if you simply set a decimal value and don’t use an input number? If so, it might be a bug in the volume_set service.

This is how it works: {“volume_level”: 0.2}

Kermit

8h

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/guide/call-service.html#doing-arithmetic

Copy to clipboard

{"volume_level":  $number($entities("input_number.volume").state) }

Hey guys, I am trying something similar.

I am getting the input from a Moe’s Rotary Switch, identifying how much spins and mapping the signal to:

0.00 ~ 1.00 if rotating clockwise
–1.00 ~ 0.00 if counter-clockwise

Now I want to input that number into the call service node and increase or reduce the volume of my Echo on that amount

I tried using {"volume_level": data.attributes.volume_level + {{payload}} } for the volume set, but am getting the following errors:

“HomeAssistantError: required key not provided @ data[‘volume_level’]”
“JSONataError: Expected “:”, got “}””

I am not sure how to proceed, could someone give me a hand?

Thank

Remove the mustache from payload when using jsonata(Jexpession), use it when the data field is set to json.

Whats the difference? In this case nothing but jsonata exposes functions that you can use to manipulate messages.

Hey, @Mikefila, thanks for the quick response!
You mean I should have like this (below)?

{"volume_level": data.attributes.volume_level + payload }

I tried that and keep getting the same error

As you can see, the correct values are reaching the debug Nodes (0.05, 0.1, -0.1, etc), so the problem is on the Call Service node.

If it helps, I’m running HAOS on a Home Assistant Green:

  • Core 2024.1.5
  • Supervisor 2023.12.1
  • OS 11.4
  • Frontend 20240104.0

In jsonata we need to tell it specifically where the data is at.

{"volume_level": ($entity().data.attributes.volume_level + payload) }

Because of jsonata we can send this data right from an event state or a trigger by changing the output options. Set the node to the volume dial and leave the state blank


using

{"volume_level": $entities(media_player.big_echo).data.attributes.volume_level + $entity().state }

Sometimes numbers are stored as text.

I don’t have an echo, so if an entity stores a number as text you would use $number()

{"volume_level": $number($entities(media_player.big_echo).data.attributes.volume_level) + $entity().state }