How to set value for virtual sensor?

Hi. I need to create a sensor where I can store and update data.
I use the ‘hass-virtual’ integration to create virtual sensors.

sensors

In the Node-Red environment, I pass data to the node’s ‘sensor’ and assign a value to the state of my sensor.

NR1

NR2

But if I watch a sensor value from Developer Tools → States, I see that my sensor doesn’t change state.

What did I do wrong?

No need to use hass-virtual integration.
Node Red have the ability to create sensors directly and it can also be done dynamically.

This is an example on a dynamically created sensor.

[{"id":"25ef1c9c74581477","type":"ha-api","z":"be03f9025a1f137a","name":"sensor","server":"541ade28.b4a62","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":800,"y":660,"wires":[[]]},{"id":"06ac8e03a8a85876","type":"function","z":"be03f9025a1f137a","name":"make final payload","func":"msg.entity_id = `sensor.test_entity`;\n\nmsg.payload = {\n    data: {\n        state: 'test1',\n        attributes: {\n            'name': 'test_entity2',\n            'test1': '111',\n            'test2': '222',\n            friendly_name: 'Test Entity 2',\n            icon: 'mdi:pulse'\n        }\n    }\n};\n\nreturn msg;\n\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":660,"wires":[["25ef1c9c74581477","fee8ca78ceac8578"]]},{"id":"39963ed0628d85b4","type":"inject","z":"be03f9025a1f137a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":450,"y":660,"wires":[["06ac8e03a8a85876"]]},{"id":"541ade28.b4a62","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"numeric","statusMonth":"2-digit","statusDay":"2-digit","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

The Node Red built-in sensor creations is self-explained.

1 Like

As someone who has found very little of working with HA / Node-RED self explanatory, and to answer your OP question

What did I do wrong?

I will attempt to provide an answer (and a correct one I hope).

Home Assistant has no entities of it’s own. Entities, including things like sensors, are all created by integrations. True, things like sun.sun and time. and home assistant all arrive ‘out of the box’, but everything ultimately is created by an integration.

The integration ‘registers’ the entity(ies) it wants with HA (usually at start-up time) and they then exist.
You have used an integration (hass-virtual) which has created two new sensor entities under the platform ‘virtual’.

HA has a very basic rule - only the integration that created an entity can manage that entity. Or, to put it another way, you can’t change a sensor state belonging to one integration from another.

There are, as always, exceptions to all of this.
The developer tools > states tool allows for any entity to have the state changed, but only for testing purposes, and the integration will overwrite this the next time the owning integration updates the sensor state.

Some integrations also provide services, and these may allow an update to a sensor state, and only by using the correct service call.

Node RED is (mostly used as) an addon, and the HA websocket nodes work as another ‘integration’.

The HA sensor node that you have used works just like other integrations.
It first registers (creates) the sensor
It then updates the sensor.state based on values you pass to it from the message payload etc.

The fact that your Entity config is called ‘sensor 1e33d75a8c1edc56’ is suggests that you have created the sensor node (the blue node ‘price buy’) without setting up the configuration node. Such names only appear if you do not give the sensor’s configuration node a name.

When you set up a HA sensor node,
You must also set up an Entity configuration node
This configuration node does the work of creating the sensor, naming it, and then passing the updated state values when you use the sensor node.

If you look in the Node-RED debug window, under configuration nodes, you will find the configuration nodes you have set up behind your sensor nodes.

Simply put, there is no way for you to connect your Node-RED created sensor to your HA config virtual platform created sensor. They exists on different integrations - all the Node-RED entities created in HA appear under the Node-RED Companion integration.

So how do you update your virtual sensor?
Well, if you have read down this far, there is great news!

I see that the creator of the ‘virtual’ integration has done a good job, and this integration includes a service that allows you to change the state of your sensors using a service call. Bingo (or whatever that would be in your native language).

https://github.com/twrecked/hass-virtual#sensors

Use the virtual.set service to manipulate the sensor value.

I don’t have this integration myself, but I would expect you to be able to find a service call available in HA called ‘virtual.set’. Check in Developer Tools > Services.

If you find and test this service call in HA, and it works, then you can use the HA websocket ‘service call’ node. This node is not without challenges to get the data field set correctly, but yes it should all work for you. You have choices!

Good luck with your project work.

1 Like

Thanks to both of you! Now, everything is working fine.