Firing event from Node-RED: Event data?

Hi,

I have a sensor A that contains data of heating energy used. The format is:

energy,timestamp energy,timestamp, …

The energy data is available for past 14 hours and I’m trying to insert this “consumption history data” to another sensor’s (B) history. So I have the history in these data pairs, but I would like to put them into sensor states for being able to see them as normal history data of a sensor. Uh, that was difficult to explain, hopefully you understood!

I have successfully parsed the data pairs in NodeRed using SPLIT node which basically initiates flow for each pair of data. For each pair of data I would like to fire state_changed event with the consumption values and timestamps (that are in the past). I just cannot figure out the input values for the FIRE-EVENT node. Could someone help with this? This is what I have currently:

msg.topic = "state_changed";
msg.event_type = "state_changed";
msg.payload.event = "state_changed";
msg.payload.entity_id = "sensor.lattian_energiakulutus";
msg.data.entity_id = "sensor.lattian_energiakulutus";
msg.data.old_state.entity_id = "sensor.lattian_energiakulutus";
msg.data.old_state.state = 0;
msg.data.new_state.state = 1;
msg.data.new_state.entity_id = "sensor.lattian_energiakulutus";
return msg;

And this is what comes out of it:

{"topic":"state_changed","payload":{"event":"state_changed","data":null},"data":{"entity_id":"sensor.lattian_energiakulutus","old_state":{"entity_id":"sensor.lattian_energiakulutus","state":1,"attributes":{"unit_of_measurement":"","friendly_name":"themo_energia_raw"},"last_changed":"2022-10-19T15:01:12.038924+00:00","last_updated":"2022-10-19T15:01:12.038924+00:00","context":{"id":"01GFRC6GQ68ZPN6MMT8Q9YQBDC","parent_id":null,"user_id":null},"original_state":"0.0,1666144800.0 0.0,1666148400.0 0.0,1666152000.0 0.0,1666155600.0 0.0,1666159200.0 0.0,1666162800.0 0.0,1666166400.0 0.0,1666170000.0 0.0,1666173600.0 0.0,1666177200.0 0.0,1666180800.0 0.0,1666184400.0 0.0,1666188000.0 0.0,1666191600.0","timeSinceChangedMs":3182927},"new_state":{"entity_id":"sensor.lattian_energiakulutus","state":1,"attributes":{"unit_of_measurement":"","friendly_name":"themo_energia_raw"},"last_changed":"2022-10-19T15:01:12.038924+00:00","last_updated":"2022-10-19T15:01:12.038924+00:00","context":{"id":"01GFRC6GQ68ZPN6MMT8Q9YQBDC","parent_id":null,"user_id":null},"original_state":"0.0,1666144800.0 0.0,1666148400.0 0.0,1666152000.0 0.0,1666155600.0 0.0,1666159200.0 0.0,1666162800.0 0.0,1666166400.0 0.0,1666170000.0 0.0,1666173600.0 0.0,1666177200.0 0.0,1666180800.0 0.0,1666184400.0 0.0,1666188000.0 0.0,1666191600.0","timeSinceChangedMs":3182927}},"parts":{"id":"fbcc5a3f300aa8a2","type":"string","ch":" ","index":11,"count":14,"date":"10/19/2022, 4:00:00 PM","value":0},"_msgid":"ff59cd9d9ba6e47e","event_type":"state_changed"}

There are some fields that are not probably needed, but which one? What are the parameters that are needed at minimum. The node sends the event (above), but it does not end up to sensor B history.

FIRE-EVENT node says:

Inputs:

If the incoming message has a payload property with event set it will override any config values if set. If the incoming message has a payload.datathat is an object or parsable into an object these properties will be merged with any config values set. If the node has a property value in its config for Merge Context then the flow and globalcontexts will be checked for this property which should be an object that will also be merged into the data payload.

payload.event string
Event to fire

payload.data Object
Event data to send

I suppose my problem is payload.data. Any ideas what should be in that?

I think it’s more likely that Home Assistant sends the message based on the history being updated, rather than updating the history as a result of the message. If this is the case, you will not be able to update the history this way. Therefore all you can do is update the state of B based on A, and wait for the history to be populated over time.

Others have asked how to populate historical state for an entity, and I’ve only ever seen the answer “you can’t”.

The problem is that the sensor with multiple data pairs may not update only after the hour is “ready”, so in practice even the latest data may be incomplete. For example if the data updates always at xx:30, data available at xx+1:30 will give additional 30 minutes consumption to the count. So, this is why I want to update the whole history according to the latest data.

The idea of doing this is from: https://community.home-assistant.io/t/batch-receive-time-series-data/171915/9?u=rantaki3

So based on that, it should be possible.

I succeeded to fire an event from Node-RED with an arbitrary timestamp for the new sensor value. For the example below to work you have to set the fire event node to accept JSON (JSonata is the default setting).
I pasted the following code into a function node which is then connected to a fire event node:

msg = {};
msg.payload = {};
msg.payload.event = "state_changed";

msg.payload.data = {
  "entity_id":"sensor.energy_kwh_test",
  "old_state":
  {
    "entity_id": "sensor.energy_kwh_test",
    "state": "0",
  },
  "new_state": 
  {
    "entity_id":"sensor.energy_kwh_test",
    "state": "6.4",
    "last_updated":"2023-03-05T14:10:25+01:00"
  }
};
return msg;

Note also that it seems as the value of old_state.state does not really matter.

1 Like