How to force an entity state change in Node Red?

Hi guys,

I currently have a flow integrating camera motion and human detection to avoid false triggers from my cats . The goal is to integrate this with my current alarm system, and for that reason I need to have a sensor that is basically the condition of human detection (on when condition of human detection is met and off when condition not met) .

Since it’s not really a physical sensor I figured the best way to go about this would be to add a fake motion sensor in my config and then make node red trigger the on and off state of my entity depending on whether there was human detection or not .

I have seen one topic mention manual injection but have no idea how to work this out and which payload / nodes to use .

Anyone ?

Thanks !

Hello,
Here is a partial example. You need to put the values you want in the two variable in the function node.
Thanks to HA the sensor will be automagically created.
GV

[{"id":"b7190b52.d38028","type":"function","z":"d59703d6.87ebf","name":"HA-API","func":"var value = msg.payload.value;\nvar sensor = msg.payload.sensor;\nmsg.url = \"https://127.0.0.1:8123/api/states/sensor.\" + sensor;\nmsg.method = \"POST\";\nmsg.payload = {\n       'state': value\n}; \nmsg.headers = {\n    'Authorization': 'Bearer Your_long_lived_token',\n    'content-type': 'application/json',\n} ;\nreturn msg;\n","outputs":1,"noerr":0,"x":440,"y":1200,"wires":[["db0ed653.1d44f8"]]},{"id":"db0ed653.1d44f8","type":"https-node","z":"d59703d6.87ebf","name":"HA OUT","method":"use","ret":"txt","url":"","authorized":false,"agent":true,"x":640,"y":1200,"wires":[[]]}]
1 Like

Thanks for the flow, the solution surely seems to be something along those lines .

I am however not familiarized enough with Node Red and have some doubts about how to replace the variable for my purpose .

I tried to replace this :

var value = msg.payload.value;
var sensor = "msg.payload.sensor";

With this :

var value = "on";
var sensor = "binary_sensor.fdcperson";

However, after running the flow and adding my LongLived Token the state of the sensor did not change !

What am I doing wrong ?

In my case the “sensor” needs to be a variable because I have multiple sensors I want to update with this method.

So my api call is:

msg.url = "https://127.0.0.1:8123/api/states/sensor." + sensor;

If you only one sensor no need to use a variable something like:

msg.url = "https://127.0.0.1:8123/api/states/binary_sensor.fdcperson" ;

should do the trick.

In order to debug this kind of things (I am to fairly new to nodered), I am using postman https://chrome.google.com/webstore/detail/tabbed-postman-rest-clien/coohjcphdfgbiolnekdpbcijmhambjff

So, I can easily tweak the api call and send include this into nodered. Splitting the problem is two make this easier to solve!

GV

1 Like

Thanks, didn’t get it working but it’s probably some other issue related to the API as the external Curl call also gets me a 401 even though I’m using a valid long lived token .

Are your nodered and HA using httpS ? In my example it is the case. So, if you are just using HTTP, don’t forget to change that.
Debugging with curl is an option too!!
GV

Yes for both ! Duckdns + Let’s Encrypt, still getting 401 error when I make the api call … Perhaps need to enable the API in my config in order for it to accept the calls !

AFAIK API is enabled, by default.
Error 401 is “unauthorized”. Are you 100% sure of your token and the way you use it?
It must be with the header “Authorization” and the value is “Bearer TheToken”…
GV

If you’re already using Node-RED with the Home Assistant Websocket integration look here:

https://community.home-assistant.io/t/update-entity-from-http-get-request/126751/4?u=kermit

1 Like