How do I pass a variable within Node-Red? Either from a system variable/state, or from a msg.payload? I swear I’ve done this before but I’ve only passed it directly to an iOS notification which works differently.
See my example below
How do I pass a variable within Node-Red? Either from a system variable/state, or from a msg.payload? I swear I’ve done this before but I’ve only passed it directly to an iOS notification which works differently.
See my example below
I use myself a function node in front of the service call (see example below). However, maybe there is a better way by using templates or somthing like that and avoiding the function node… I would be happy to learn a better way !
This is what I’m currently doing:
In the function node I’ve put:
And then for the service call:
You can use "/config/www/{{time}}..."
in your template. The sidebar docs in Node Red say:
You can use templates in the
Domain
,Service
,Entity Id
, anddata
fields. When using templates the top level is a property of the message object:msg.payload
would be{{payload}}
. You can also access theflow, global and states
contexts{{flow.foobar}}
{{global.something}}
. For thestates
context you can use the{{entity.domain.entity_id}}
to just get the state or drill further down like{{entity.light.kitchen.attributes.friendly_name}}
.{{entity.light.kitchen}}
and{{entity.light.kitchen.state}}
are equivalent.
So you just needed to drop the msg.
part and I think you are good.
Wow, this is fantastic !
Note that while you can print state (which is quite cool), there’s no conditional stuff in the mustache templates.
I had to use a ‘template’ node to make a template with the node red variables that I pass to a ‘get template’ node for home assistant to process and the result is the message I send. A bit ugly, but it works well.
The ‘template’ node’s template is:
{{=<% %>=}}
{{ state_attr("<% topic %>", 'friendly_name') }}
{%- if "<% payload %>" == "home" %} arrived home {%- else %} has left the house{%- endif %}.
(The first line changes the delimiters of mustache to <% %>
so I can easily use {{ }}
for home assistant templates).
So that generates a home assistant template that get template
then passes through home assistant to process.
-ben
Interesting. I’ve run into an error however.
I have a service call node that needs to replace the data for a media_player entity with a value for volume stored in a flow variable. The location of the data is at flow.mediaPlayer.kitchen.volume_snapshot
.
If I add
{"volume_level","{{flow.mediaPlayer.kitchen.volume_snapshot}}"}
to the DATA field, I get a "Call-service API error. Error Message: extra keys not allowed @ data['0']"
in my debug node.
I could easily alter the msg.payload to contain this information and just use {{payload}} in the data field, but I’m trying to learn this method and reduce the number of nodes I’m using.
Possible?
Try this, you were missing the colons
Lol…I just figured that out when you responded.