Hi guys,
I’ve been working on my first flow as a demo to web scrape data and present it as a sensor in Home assistant.
In this flow I’m pulling cruise ship data from a table, converting each row to an array object, finding the record where the date equals today and then transmit that data to my MQTT server as 4 sensors - Cruise ship name, arrival date, arrival time and departure time.
Ill then extend it to handle no ship in port and the next ship in port.
I’m currently stuck with getting the structure right for the MQTT messages - My understanding is I have to send some to setup the message structure (shows as Config Below) and then a message sending the actual consents.
Can anyone see what wrong?
Im not sure about the value_template: "{{ value_json… stuff as I dont think im using json but the demo code I stole had it.
Message Config
var config1 = {
payload: {
name: “Name”,
state_topic: “homeassistant/sensor/cruiseship_current_name/state”,
value_template: “{{ value_json.Name }}”
},
topic: “homeassistant/sensor/cruiseship_current_name/config”
};
var config2 = {
payload: {
name: “Cruiseship Arrival Date”,
state_topic: “homeassistant/sensor/cruiseship_current_arrivedate/state”,
value_template: “{{ value_json.arrivedate }}”
},
topic: “homeassistant/sensor/cruiseship_current_arrivedate/config”
};
var config3 = {
payload: {
name: “Cruiseship Arrival Time”,
state_topic: “homeassistant/sensor/cruiseship_current_arrivetime/state”,
value_template: “{{ value_json.arrivetime }}”
},
topic: “homeassistant/sensor/cruiseship_current_arrivetime/config”
};
var config4 = {
payload: {
name: “Cruiseship Departure Time”,
state_topic: “homeassistant/sensor/cruiseship_current_departtime/state”,
value_template: “{{ value_json.departtime }}”
},
topic: “homeassistant/sensor/cruiseship_current_departtime/config”
};
return [config1, config2, config3, config4];
Message
//Convert text to date
var msec = Date.parse(msg.payload[4]);
var d = new Date(msec);
var Currdate = new Date(Date.now());
//compare ship date to todays date
if (d.toDateString() == Currdate.toDateString())
{
//if it matches then build and send this message
var CurrentCruiseship = {
topic: "homeassistant/sensor/cruiseship_current/state",
payload: {cruiseship_current_name:msg.payload[0], cruiseship_current_arrivedate: msg.payload[4], cruiseship_current_arrivetime:msg.payload[5], cruiseship_current_departtime:msg.payload[6]}
};
return CurrentCruiseship;
}
else
{
return null;
}